zoukankan      html  css  js  c++  java
  • c# 生成excel并导出

    用c#的NPOI读写Excel 1、整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。 2、NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 3、POI读取Excel有两种格式一个是HSSF,另一个是XSSF。 HSSF和XSSF的区别如下: HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format. 即:HSSF适用2007以前的版本,XSSF适用2007版本及其以上的。 以下是生成excel并导出的代码: public void ExportErrorSongIds(long uid) { try { string fileName = "ErrorSongIds.xls"; // 文件名称 string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload") + "/" + fileName; // 1.检测是否存在文件夹,若不存在就建立个文件夹 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } // 2.解析单元格头部,设置单元头的中文名称 HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet("失败歌曲"); // 工作表 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue("原始ID"); row.CreateCell(1).SetCellValue("歌曲名称"); row.CreateCell(2).SetCellValue("歌手名"); row.CreateCell(3).SetCellValue("失败原因"); BsonArray array = _songListService.getErrorExcel(uid); int rowIndex = 1; BsonDocument bd = null; if (array != null && array.Count > 0) { for (int i = 0; i < array.Count; i++) { IRow rowTmp = sheet.CreateRow(rowIndex); bd = (BsonDocument)array[i]; if (bd != null) { rowTmp.CreateCell(0).SetCellValue(bd.GetValue("sourceId").ToString()); rowTmp.CreateCell(1).SetCellValue(bd.GetValue("songName").ToString()); rowTmp.CreateCell(2).SetCellValue(bd.GetValue("singerName").ToString()); rowTmp.CreateCell(3).SetCellValue(string.IsNullOrEmpty(bd.GetValue("errorReason").ToString()) ? "" : bd.GetValue("errorReason").ToString()); rowIndex++; } } } // 4.生成文件 FileStream file = new FileStream(filePath, FileMode.Create); workbook.Write(file); file.Close(); Response.AppendHeader("Content-Disposition", "attachment;filename=ErrorSongIds.xls"); Response.ContentType = "application/ms-excel"; Response.WriteFile(filePath); Response.Flush(); Response.End(); } catch (Exception ex) { throw ex; } }
  • 相关阅读:
    按单生产案例
    【转】linux中执行外部命令提示" error while loading shared libraries"时的解决办法
    【转】WARNING! File system needs to be upgraded. You have version null and I want version 7. Run the '${HBASE_HOME}/bin/hbase migrate' script. 的解决办法
    根据Rowkey从HBase中查询数据
    【转】在一个Job中同时写入多个HBase的table
    sqoop 使用
    给VMware下的Linux扩展磁盘空间(以CentOS6.3为例)
    chrome 版本 29.0.1547.76 m 解决打开新标签页后的恶心页面的问题
    tomcat7+jdk的keytool生成证书 配置https
    如何打包和生成你的Android应用程序
  • 原文地址:https://www.cnblogs.com/java-chanjuan/p/6687376.html
Copyright © 2011-2022 走看看