最近,我做的统计功能用到了导出Excel的功能。刚开始我觉着,导出Excel是一个比较简单的功能,分分钟都能解决的,应该和自己当初做机房收费系统的时候一样,引用一下微软的SDK,再做一下相应的适应性处理就差不多了。后来,当我做出来的时候,测试人员说,自己做的功能,有点奇葩,没见过是这样导出Excel 的——打开Excel表格文件,然后将数据存放进去……
之后自己反思一下,发现自己的实现方法确实有点跟平常用的不一样,或许当初自己做的导出Excel也只是仅仅限于没有用户的练习方法吧。
后来一个同事告诉我,用NPOI来实现导出Excel。后来自己在网上查了查相关的资料,发现NPOI确实是一个不错的工具包,相比微软的SDK它的优点大致如下:
<span style="font-size:24px;"><span style="font-size:24px;"><span style="font-size:24px;">public class ExcelOperateInterface { //获取excel操作类对象 public static ExcelOperateInterface CreateInterfaceInstance() { ExcelOperateInterface eoi = null; //switch (EnvirMemory.excelOperateTool) //{ // case ExcelOperateToolType.Excel_Office: // { // eoi = new ExcelOffice(); // } break; // case ExcelOperateToolType.Excel_NPOI: // { eoi = new ExcelNPOI(); // } break; //case ExcelOperateToolType.Excel_Defalut: // { // eoi = new ExcelOffice(); // } break; //default: // { // eoi = new ExcelOffice(); // } break; //} return eoi; } public virtual void OpenFile(string file,int iSheetIndex) { } public virtual void CreateFile(string file, int iSheetIndex) { } public virtual void CloseFile(bool bsave) { } public virtual string GetCellValue(int iRowIndex, int iColumnIndex) { return ""; } public virtual void SetCellValue(string sValue, int iRowIndex, int iColumnIndex) { } public virtual string GetDateCellValue(int iRowIndex, int iColumnIndex) { return ""; } } </span></span></span>
<span style="font-size:24px;"><span style="font-size:24px;">public class ExcelNPOI : ExcelOperateInterface { private string filename; private IWorkbook m_iWorkbook; private ISheet m_iSheet; private void iInitEnvironment() { filename = ""; m_iWorkbook = null; m_iSheet = null; } private void SaveFile() { try { //NPOI保存文件时,最好是先删除以前的 再新建一个文件 //FilesManager.DeleteFile(filename); //新建文件流 using (FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) { int i = m_iWorkbook.NumberOfSheets; for (int ii = 0; ii < i; ++ii) { ISheet ish = m_iWorkbook.GetSheetAt(ii); ish.DisplayGridlines = true; ish.DisplayRowColHeadings = true; } m_iWorkbook.Write(stream); stream.Flush(); } } catch (Exception ex) { } } public override void CreateFile(string file,int iSheetIndex) { try { IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("sheet0"); for (int i = 0; i < 450; i++) { IRow row = sheet.CreateRow(i); for (int j = 0; j < 50; j++) { ICell cell = row.CreateCell(j); cell.SetCellValue("lcy"); } } using (FileStream fs = File.OpenWrite(file)) { workbook.Write(fs); } } catch (Exception ex) { throw; } } public override void OpenFile(string file, int iSheetIndex) { try { iInitEnvironment(); filename = file; using (FileStream sfile = new FileStream(file, FileMode.Open, FileAccess.Read)) { m_iWorkbook = WorkbookFactory.Create(sfile); if (m_iWorkbook != null) { m_iSheet = m_iWorkbook.GetSheetAt(iSheetIndex - 1); } } } catch (Exception ex) { } } public override void CloseFile(bool bsave) { try { if (bsave) { SaveFile(); } iInitEnvironment(); } catch { } } public override string GetCellValue(int iRowIndex, int iColumnIndex) { string sResult = ""; try { IRow irow = m_iSheet.GetRow(iRowIndex - 1); if (irow != null) { ICell icell = irow.GetCell(iColumnIndex - 1); if (icell != null) { sResult = icell.ToString(); } } } catch { } return sResult; } public override string GetDateCellValue(int iRowIndex, int iColumnIndex) { string sResult = ""; try { IRow irow = m_iSheet.GetRow(iRowIndex - 1); if (irow != null) { ICell icell = irow.GetCell(iColumnIndex - 1); if (icell != null) { DateTime dt; if (DateTime.TryParse(icell.ToString(), out dt)) { if (icell.CellType == CellType.Numeric) { sResult = icell.DateCellValue.ToString(); } else { sResult = icell.ToString(); } } } } } catch { } return sResult; } public override void SetCellValue(string sValue, int iRowIndex, int iColumnIndex) { try { //CreateFile("D:\\excel333.xls", 1); IRow irow = m_iSheet.GetRow(iRowIndex - 1); if (irow == null) { irow = m_iSheet.CreateRow(iRowIndex - 1); } ICell icell = irow.GetCell(iColumnIndex - 1); if (icell == null) { icell = irow.CreateCell(iColumnIndex - 1); } icell.SetCellType(CellType.String); if (sValue == "") { sValue = "/"; } icell.SetCellValue(sValue); CloseFile(true); } catch { } } }</span></span>最后就是调用了:方法很简单,我现在只用到了导出的功能,所以只用一个Create方法就可以实现了,打开、保存之类的方法是不需要的:
<span style="font-size:24px;"><span style="font-size:24px;"> string file = @"D:\\excel550.xls"; ExcelOperate.ExcelOperateInterface.CreateInterfaceInstance().CreateFile(file, 1);</span></span>