NPOI是否支持.xlsx格式?

NPOI DLL能识别.xlsx文件吗?

目前,我正在使用Microsoft Excel 97-2003的NPOI 1.2.5版DLL,但是我还需要访问扩展名为.xlsx Excel表单。

NPOI是否支持上述?

代码片段:

 static void Main(string[] args) { XSSFWorkbook xssfwb; using(FileStream file=new FileStream( @"C:\Users\347702\Desktop\Hello.xlsx", FileMode.Open, FileAccess.Read)) { xssfwb=new XSSFWorkbook(file); } ISheet sheet=xssfwb.GetSheet("sheet1"); sheet.GetRow(1048576); Console.WriteLine(sheet.GetRow(1048576).GetCell(0).StringCellValue); } 

是的,它确实。 NPOI 2.0testing版的作品。 下面是一个示例代码,让你开始:

 class Program { static XSSFWorkbook hssfworkbook; static DataSet dataSet1 = new DataSet(); static void Main(string[] args) { InitializeWorkbook(@"E:\Docs\HoursWidget_RTM.xlsx"); xlsxToDT(); DisplayData(dataSet1.Tables[0]); Console.ReadLine(); } static void InitializeWorkbook(string path) { using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read)) { hssfworkbook = new XSSFWorkbook(file); } } static void xlsxToDT() { DataTable dt = new DataTable(); ISheet sheet = hssfworkbook.GetSheetAt(1); IRow headerRow = sheet.GetRow(0); IEnumerator rows = sheet.GetRowEnumerator(); int colCount = headerRow.LastCellNum; int rowCount = sheet.LastRowNum; for (int c = 0; c < colCount; c++) { dt.Columns.Add(headerRow.GetCell(c).ToString()); } bool skipReadingHeaderRow = rows.MoveNext(); while (rows.MoveNext()) { IRow row = (XSSFRow)rows.Current; DataRow dr = dt.NewRow(); for (int i = 0; i < colCount; i++) { ICell cell = row.GetCell(i); if (cell != null) { dr[i] = cell.ToString(); } } dt.Rows.Add(dr); } hssfworkbook = null; sheet = null; dataSet1.Tables.Add(dt); } static void DisplayData(DataTable table) { foreach (DataRow row in table.Rows) { foreach (DataColumn col in table.Columns) { Console.WriteLine("{0} = {1}", col.ColumnName, row[col]); } Console.WriteLine("-------------------------------------------"); } } } 

您可以使用NPOI以.xls和.xlsx扩展名读取Excel文件,只需要在使用部分中添加下一个

 using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; 

主要是在打开文件时,必须区分扩展名,以便使用合适的组件,并使用ISheet界面,以便可以独立于文件扩展名引用表单

 //We get the file extension fileExt = Path.GetExtension(fileName); //Declare the sheet interface ISheet sheet; //Get the Excel file according to the extension if (fileExt.ToLower() == ".xls") { //Use the NPOI Excel xls object HSSFWorkbook hssfwb; using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { hssfwb = new HSSFWorkbook(file); } //Assign the sheet sheet = hssfwb.GetSheet(sheetName); } else //.xlsx extension { //Use the NPOI Excel xlsx object XSSFWorkbook hssfwb; using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { hssfwb = new XSSFWorkbook(file); } //Assign the sheet sheet = hssfwb.GetSheet(sheetName); } 

一旦你有了excel对象,你只需要读取它(在NPOI中,行和列是基于零的)

 //Loop through the rows until we find an empty one for (int row = 0; row <= sheet.LastRowNum; row++) { //Get the cell value string cellValue = sheet.GetRow(row).GetCell(0).ToString().Trim(); //In the method GetCell you specify the column number you want to read, in the method GetRow you spacify the row string cellValue2 = sheet.GetRow(row).GetCell(0).StringCellValue.Trim(); } 

要读取单元格值,可以使用.ToString()方法或StringCellValue属性,但要小心StringCellValue只能用于string单元格,数字和date单元格会引发exception。

当原始答案被提供时,可能是图书馆没有这个function,但是现在你可以使用相同的代码库来处理xls和xlsx而不检查文件扩展名。

诀窍是使用WorkbookFactory类来透明地加载这两种types的文件。 只要您不使用特定于任一版本的特殊function,这将工作。

 using (FileStream fileStream = File.OpenRead(fullPathToExcelFile)) //fullPathToExcelFile can hold either a xls or xlsx, we don't care { IWorkbook workbook = WorkbookFactory.Create(fileStream); ISheet worksheet = workbook.GetSheet("SampleSheet"); //Now read from the worksheet anyway you like var value = worksheet.GetRow(1).GetCell(1); }