2017-07-11 82 views
2

Excelの列インデックスがスプレッドシートにあり、この列インデックスを使用して列全体を削除する必要があります。私はOpen XML SDK 2.0を使用することに限定されています。Open XML:列インデックスを使用してExcelの列全体を削除する

using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Spreadsheet; 

namespace openXMLDemo 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      string fileFullPath = @"path to the excel file here"; 
      string sheetName = "excelsheet name here"; 

      using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true)) 
      { 
       Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault(); 
       if (sheet != null) 
       { 
        WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value); 

        // This is where I am struggling.... 
        // finding the reference to entire column with the use of column index 
        Column columnToDelete = sheet.GetFirstChild<SheetData>().Elements<Column>() 
       } 
      } 
     } 
    }  
} 
+0

列データを削除するか、列のレイアウト/スタイル情報を削除しますか? ( 'Column'要素はスプレッドシートの[range of]列のレイアウト/スタイリング情報のみを含み、実際のデータは' Cell'要素に格納され、 'SheetData'要素内の個々の' Row'要素に含まれます) – bassfader

答えて

3

がオープンXMLを使用して、データを削除するために内の各列とセルを反復処理する必要があるので、残念ながら列を選択する方法がありません:https://stackoverflow.com/a/182924/5309534

static void Main(string[] args) 
{ 
    string fileFullPath = @"C:\Book1.xlsx"; 
    string sheetName = "Sheet1"; 

    // Specify your column index and then convert to letter format 
    int columnIndex = 3; 
    string columnName = GetExcelColumnName(columnIndex); 

    using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true)) { 
     Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault(); 

     if (sheet != null) { 
      WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value); 

      // Get all the rows in the workbook 
      IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>(); 

      // Ensure that there are actually rows in the workbook 
      if (rows.Count() == 0){ 
       return; 
      } 

      // Select all the cells from each row where the column letter is equal to index 
      foreach (Row row in rows) { 
       var cellsToRemove = row.Elements<Cell>().Where(x => new String(x.CellReference.Value.Where(Char.IsLetter).ToArray()) == columnName); 

       foreach (var cell in cellsToRemove) 
        cell.Remove(); 
      } 
     } 
    } 
} 

ヘルパー関数礼儀:

static string GetExcelColumnName(int columnNumber) 
{ 
    int dividend = columnNumber; 
    string columnName = String.Empty; 
    int modulo; 

    while (dividend > 0) { 
     modulo = (dividend - 1) % 26; 
     columnName = Convert.ToChar(65 + modulo).ToString() + columnName; 
     dividend = (int)((dividend - modulo)/26); 
    } 

    return columnName; 
} 
関連する問題