NPOI从数据库中调取数据直接导出到EXCEL中
2013-06-27 16:14
597 查看
关于NPOI
NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex.com/下 载到它的最新版本。它不使用Office COM组件(Microsoft.Office.Interop.XXX.dll),不需要安装Microsoft Office,支持对Office 97-2003的文件格式,功能比较强大。更详细的说明请看作者的博客或官方网站。它的以下一些特性让我相当喜欢:
支持对标准的Excel读写
支持对流(Stream)的读写 (而Jet OLEDB和Office COM都只能针对文件)
支持大部分Office COM组件的常用功能
性能优异 (相对于前面的方法)
使用简单,易上手
使用NPOI
本文使用的是它当前的最新版本1.2.4,此版本的程序集缩减至2个:NPOI.dll、Ionic.Zip.dll,直接引用到项目中即可。对于我们开发者使用的对象主要位于NPOI.HSSF.UserModel空间下,主要有HSSFWorkbook、HSSFSheet、 HSSFRow、HSSFCell,对应的接口为位于NPOI.SS.UserModel空间下的IWorkbook、ISheet、IRow、 ICell,分别对应Excel文件、工作表、行、列。
简单演示一下创建一个Workbook对象,添加一个工作表,在工作表中添加一行一列:
操作示例代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MemoryStream ms = RenderToExcel(GetTable()); string fileName = @"c:\2.xls"; SaveToFile(ms, fileName); Response.Write("成功"); } public MemoryStream RenderToExcel(DataTable table) { MemoryStream ms = new MemoryStream(); using (table) { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in table.Columns) headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value // handling value. int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } return ms; } private void SaveToFile(MemoryStream ms, string fileName) { using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); data = null; } } private DataTable GetTable() { SqlHelperXhf xhf = new SqlHelperXhf(); string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hname<>'无'and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc"; return xhf.ExecuteDataTable(sql); } private DataTable GetTable(int hid) { SqlHelperXhf xhf = new SqlHelperXhf(); string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hid=@hid and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc"; SqlParameter paramer = new SqlParameter("@hid", hid); return xhf.ExecuteDataTable(sql,paramer); } private DataTable GetTable(int hid1,int hid2,int hid3) { SqlHelperXhf xhf = new SqlHelperXhf(); string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hid=@hid1 or hid=@hid2 or hid=@hid3 and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc"; SqlParameter [] paramers = { new SqlParameter("@hid1", hid1), new SqlParameter("@hid2", hid2), new SqlParameter("@hid3", hid3) }; return xhf.ExecuteDataTable(sql, paramers); } }
参考资料:
简单演示一下创建一个Workbook对象,添加一个工作表,在工作表中添加一行一列:
?
?
使用NPOI导出
从DataTable读取内容来创建Workbook对象:?
?
?
使用NPOI导入
需要注意的是,sheet.LastRowNum = sheet.PhysicalNumberOfRows - 1,这里可能存在BUG:当没有数据或只有一行数据时sheet.LastRowNum为0,PhysicalNumberOfRows 表现正常。这里读取流中的Excel来创建Workbook对象,并转换成DataTable:
?
?
?
相关文章推荐
- NPOI从数据库中调取数据直接导出到EXCEL中
- 将数据库中的数据导出到Excel中(直接导出到磁盘,Java方式)
- java导出数据到excel里:直接导出和导出数据库数据
- NPOI从数据库中导出数据到excel中
- NPOI从数据库中导出数据到Excel
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
- NPOI将数据导出到Excel中
- oracle 数据库中数据导出到excel
- PHP----从数据库导出数据到表格:PHPExcel
- 使用NPOI导出数据到Excel
- Python从数据库导出数据至Excel
- NPOI Excel模板填充数据后导出
- MVC 实现表格数据导出Excel(NPOI方式)
- NPOI 导出数据到Excel(包括图片)
- webapi下使用npoi导出数据到excel,包含自动列宽和自动合并某一列单元格
- 导出EXCEL表格之三将数据库中查询出的数据原样导出
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
- DataGrid数据直接导出Excel在IE中打开。 (收集,还没有测试下)
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
- phpexcel导出数据库数据生成excel文件