NPOI控件读EXCEL文件时老是少一行?
刚接触NPOI控件,表示很喜欢!
但也遇到问题,特在此向各位前辈请教,100分送上。
刚看了一下
http://msdn.microsoft.com/zh-tw/ee818993.aspx
这篇文章。基础易懂,
但是也发现了一几个问题和疑问!
版本是1.2.4版本!
先是操作类:
public static Stream RenderDataTableToExcel(DataTable SourceTable) { HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); ISheet sheet = workbook.CreateSheet(); IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in SourceTable.Columns) headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // handling value. int rowIndex = 1; foreach (DataRow row in SourceTable.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in SourceTable.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; sheet = null; headerRow = null; workbook = null; return ms; } public static void RenderDataTableToExcel(DataTable SourceTable, string FileName) { MemoryStream ms = RenderDataTableToExcel(SourceTable) as MemoryStream; FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write); byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); data = null; ms = null; fs = null; } public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex) { HSSFWorkbook workbook = new HSSFWorkbook(ExcelFileStream); ISheet sheet = workbook.GetSheet(SheetName); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(HeaderRowIndex); int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } int rowCount = sheet.LastRowNum; for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) if (row.GetCell(j) != null) dataRow[j] = row.GetCell(j).ToString(); } ExcelFileStream.Close(); workbook = null; sheet = null; return table; } public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex) { HSSFWorkbook workbook = new HSSFWorkbook(ExcelFileStream); ISheet sheet = workbook.GetSheetAt(SheetIndex); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(HeaderRowIndex); int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } int rowCount = sheet.LastRowNum; for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) dataRow[j] = row.GetCell(j).ToString(); } table.Rows.Add(dataRow); } ExcelFileStream.Close(); workbook = null; sheet = null; return table; }
protected void Button3_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) { DataTable table = ExcelHelper.RenderDataTableFromExcel(this.FileUpload1.FileContent, 0, 0); this.GridView1.DataSource = table; this.GridView1.DataBind(); } }protected void Button2_Click(object sender, EventArgs e) { DataTable table = new DataTable(); // 填充資料(由讀者自行撰寫) // 產生 Excel 資料流。 MemoryStream ms = ExcelHelper.RenderDataTableToExcel(table) as MemoryStream; // 設定強制下載標頭。 Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Download.xls")); // 輸出檔案。 Response.BinaryWrite(ms.ToArray()); ms.Close(); ms.Dispose(); }