1. 程式人生 > >NPOI 讀取Excel文件

NPOI 讀取Excel文件

tab con object ast file std error exceptio lean

      private void buttonExcel_Click(object sender, EventArgs e)
        { 
            FileStream fs = null;
            List<ISheet> lstISheet = new List<ISheet>();
            string Msg = "";
            string filePath = "";
            //初始化一個OpenFileDialog類
            OpenFileDialog fileDialog = new
OpenFileDialog(); //判斷用戶是否正確的選擇了文件 if (fileDialog.ShowDialog() == DialogResult.OK) { //獲取用戶選擇文件的後綴名 string extension = Path.GetExtension(fileDialog.FileName); //聲明允許的後綴名 string[] str = new string[] { ".xls
", ".xlsx" }; if (!((IList)str).Contains(extension)) { MessageBox.Show("僅能選擇Excel文件"); } else { filePath = fileDialog.FileName; } } if(filePath!=""
) { IWorkbook book2003; IWorkbook book2007; bool bl2003 = true; bool bl2007 = true; fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); try { book2003 = new HSSFWorkbook(fs);// 2003版本 int t = 0; while (true) { try { ISheet sheettemp = book2003.GetSheetAt(t); lstISheet.Add(sheettemp); t = t + 1; } catch (Exception ex) { break; } } } catch (Exception ex) { bl2003 = false; Msg = ex.Message.ToString() + "\r\n" + ex.StackTrace.ToString(); fs.Dispose(); fs.Close(); } // if(bl2003==false) { fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); try { book2007 = new XSSFWorkbook(fs);// 2007版本 int t = 0; while (true) { try { ISheet sheettemp = book2007.GetSheetAt(t); lstISheet.Add(sheettemp); t = t+1; } catch (Exception ex) { break; } } } catch (Exception ex) { bl2007 = false; Msg = ex.Message.ToString() + "\r\n" + ex.StackTrace.ToString(); } } ReLstDataTable(lstISheet); fs.Dispose(); fs.Close(); } } public List<DataTable> ReLstDataTable(List<ISheet> lstISheet) { List<DataTable> lstTable = new List<DataTable>(); for (int t = 0; t < lstISheet.Count; t++) { ISheet sheet = lstISheet[t]; int rowCount = sheet.LastRowNum;//總行數 if (rowCount > 0) { IRow firstRow = sheet.GetRow(0);//第一行 int cellCount = firstRow.LastCellNum;//列數 DataTable dt = new DataTable(); int startRow = 1;//如果第一行是列名,則從第二行開始讀取 for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { ICell cell = firstRow.GetCell(i); if (cell != null) { if (cell.StringCellValue != null) { DataColumn column = new DataColumn(cell.StringCellValue); dt.Columns.Add(column); } } } //------------------------------------------------ //填充行 for (int i = startRow; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; DataRow dataRow = dt.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { ICell cell = row.GetCell(j); if (cell == null) { dataRow[j] = ""; } else { //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,) switch (cell.CellType) { case CellType.Blank: dataRow[j] = ""; break; case CellType.Numeric: short format = cell.CellStyle.DataFormat; //對時間格式(2015.12.5、2015/12/5、2015-12-5等)的處理 if (format == 14 || format == 31 || format == 57 || format == 58) dataRow[j] = cell.DateCellValue; else dataRow[j] = cell.NumericCellValue; break; case CellType.String: dataRow[j] = cell.StringCellValue; break; } } } dt.Rows.Add(dataRow); } //------------------------------------------------ lstTable.Add(dt); } } return lstTable; }

NPOI 讀取Excel文件