我正在使用DocumentFormat.openxml nuget pkg读取.xlsx文件。我正在尝试读取[b5]到[m5]
的日期单元格[G2]和数据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
答案 0 :(得分: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;
}
}