如何使用openxml读取excel文件的单元格数据
我正在使用DocumentFormat.openxml nuget pkg读取.xlsx文件。 我试图读取date单元格[G2]和数据从[b5]到[m5]
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
我想你一行一行,然后逐个单元格,你可以做到这一点:
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; } }
- Excel Worksheet.Usedrange返回什么types
- 如何在C#窗口应用程序窗体中实际不知道目录中的完整path文件而自动find文件
- 如何使用可再发行的PIA 2010在没有安装MS Office客户端的计算机上创buildexcel应用程序
- Epplus SetPosition图片问题
- 全局键盘挂钩(Excel)自动插件(不是VSTO)
- IMessageFilter无法处理0x800AC472(VBA_E_IGNORE)是否使实现IMessageFilter无关紧要?
- 在64位环境中使用Excel
- 在此之前确定VSTO 4中的Office版本。应用程序已设置
- Oracle服务器错误 – “命名pipe道提供程序,错误:40 – 无法打开连接到SQL Server”