2017-10-03 11 views
1

DocumentFormat.openxml nuget pkgを使用して.xlsxファイルを読み込んでいます。私は、日付セルを読みしようとしています[G2]からのデータ[B5]に[M5]openxmlを使用してExcelファイルのセルデータを読み取る方法

Sample excel screenshot is attached私はあなたがこれを行うことができ、細胞によって行ごと、その後、セルを移動したい

static void Main(string[] args) 
     { 
      string sFileTypeError = string.Empty; 
      string myFilePath = @"C:\Landing\file.xlsx"; 
      //string ext = Path.GetExtension(myFilePath); 

      var fileName = Path.GetFileName(myFilePath); 
      var fileExtension = Path.GetExtension(fileName); 
      if ((fileExtension != ".xlsx") && (fileExtension != ".xls")) 
       sFileTypeError = "Invalid file. \n\nPlease browse a correct Excel file to upload."; 
      else 
      { 

       using (SpreadsheetDocument doc = SpreadsheetDocument.Open(myFilePath, false)) 
       { 
        IEnumerable<Sheet> sheets = doc.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().Elements<Sheet>(); 
        WorksheetPart worksheetPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheets.First().Id.Value); 
        IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Descendants<Row>(); 

       } 

      } 
     } 

Please let me know how can read this. Thanks 

答えて

1

foreach (Row actualRow in rows) 
{ 
    IEnumerable<Cell> cells = actualRow.Descendants<Cell>(); 
    foreach (Cell actualCell in cells) 
    { 
     // It gets the good value if it's not in the shared string table 
     string value = actualCell.CellValue.Text; 
    } 
} 
関連する問題