一、下载引用
下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2007版需要此dll)。
二、excel转datatable类
- usingSystem;
- usingSystem.Data;
- usingSystem.IO;
- usingNPOI.SS.UserModel;
- usingNPOI.XSSF.UserModel;
- usingNPOI.HSSF.UserModel;
- namespaceNPOIOprateExcel
- {
- publicclassExcelUtility
- {
- ///<summary>
- ///将excel导入到datatable
- ///</summary>
- ///<paramname="filePath">excel路径</param>
- ///<paramname="isColumnName">第一行是否是列名</param>
- ///<returns>返回datatable</returns>
- publicstaticDataTableExcelToDataTable(stringfilePath,boolisColumnName)
- {
- DataTabledataTable=null;
- FileStreamfs=null;
- DataColumncolumn=null;
- DataRowdataRow=null;
- IWorkbookworkbook=null;
- ISheetsheet=null;
- IRowrow=null;
- ICellcell=null;
- intstartRow=0;
- try
- {
- using(fs=File.OpenRead(filePath))
- {
- //2007版本
- if(filePath.IndexOf(".xlsx")>0)
- workbook=newXSSFWorkbook(fs);
- //2003版本
- elseif(filePath.IndexOf(".xls")>0)
- workbook=newHSSFWorkbook(fs);
- if(workbook!=null)
- {
- sheet=workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
- dataTable=newDataTable();
- if(sheet!=null)
- {
- introwCount=sheet.LastRowNum;//总行数
- if(rowCount>0)
- {
- IRowfirstRow=sheet.GetRow(0);//第一行
- intcellCount=firstRow.LastCellNum;//列数
- //构建datatable的列
- if(isColumnName)
- {
- startRow=1;//如果第一行是列名,则从第二行开始读取
- for(inti=firstRow.FirstCellNum;i<cellCount;++i)
- {
- cell=firstRow.GetCell(i);
- if(cell!=null)
- {
- if(cell.StringCellValue!=null)
- {
- column=newDataColumn(cell.StringCellValue);
- dataTable.Columns.Add(column);
- }
- }
- }
- }
- else
- {
- for(inti=firstRow.FirstCellNum;i<cellCount;++i)
- {
- column=newDataColumn("column"+(i+1));
- dataTable.Columns.Add(column);
- }
- }
- //填充行
- for(inti=startRow;i<=rowCount;++i)
- {
- row=sheet.GetRow(i);
- if(row==null)continue;
- dataRow=dataTable.NewRow();
- for(intj=row.FirstCellNum;j<cellCount;++j)
- {
- 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)
- {
- caseCellType.Blank:
- dataRow[j]="";
- break;
- caseCellType.Numeric:
- shortformat=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;
- caseCellType.String:
- dataRow[j]=cell.StringCellValue;
- break;
- }
- }
- }
- dataTable.Rows.Add(dataRow);
- }
- }
- }
- }
- }
- returndataTable;
- }
- catch(Exception)
- {
- if(fs!=null)
- {
- fs.Close();
- }
- returnnull;
- }
- }
- }
- }
三、结果
四、写入excel类
- publicstaticboolDataTableToExcel(DataTabledt)
- {
- boolresult=false;
- IWorkbookworkbook=null;
- FileStreamfs=null;
- IRowrow=null;
- ISheetsheet=null;
- ICellcell=null;
- try
- {
- if(dt!=null&&dt.Rows.Count>0)
- {
- workbook=newHSSFWorkbook();
- sheet=workbook.CreateSheet("Sheet0");//创建一个名称为Sheet0的表
- introwCount=dt.Rows.Count;//行数
- intcolumnCount=dt.Columns.Count;//列数
- //设置列头
- row=sheet.CreateRow(0);//excel第一行设为列头
- for(intc=0;c<columnCount;c++)
- {
- cell=row.CreateCell(c);
- cell.SetCellValue(dt.Columns[c].ColumnName);
- }
- //设置每行每列的单元格,
- for(inti=0;i<rowCount;i++)
- {
- row=sheet.CreateRow(i+1);
- for(intj=0;j<columnCount;j++)
- {
- cell=row.CreateCell(j);//excel第二行开始写入数据
- cell.SetCellValue(dt.Rows[i][j].ToString());
- }
- }
- using(fs=File.OpenWrite(@"D:/myxls.xls"))
- {
- workbook.Write(fs);//向打开的这个xls文件中写入数据
- result=true;
- }
- }
- returnresult;
- }
- catch(Exceptionex)
- {
- if(fs!=null)
- {
- fs.Close();
- }
- returnfalse;
- }
- }
结果如下: