NPOI从数据库中导出到Excel
2012-02-24 11:04
190 查看
一,如何把数据库的数据导入到Excel?
(1)可以使用多种方式,但是较好的一种是使用NPOI。
(2)NPOI的缺陷:只能在Office2003中使用,Office2007无法使用NPOI,同时对于WPS也不能使用。
(3)使用是要引入NPOI的dll外部文件,下面的代码使用了简单三层的思想。
二,把数据库中的数据导入到Excel的具体步骤:
protected void Button2_Click(object sender, EventArgs e)
{
try
{
//使用NPOI创建Excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
HSSFRow row;
//使用三层得到所有的数据,数据量小时,可以这么做,当数据量很大时,要使用DataReader
UserInfoBLL userBll = new UserInfoBLL();
IEnumerable<UserInfo> list = userBll.GetAll();
//写入Excel的第一行,标题
row = sheet.CreateRow(0);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("姓名");
row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue("年龄");
row.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue("邮箱");
row.CreateCell(3, HSSFCell.CELL_TYPE_STRING).SetCellValue("手机");
row.CreateCell(4, HSSFCell.CELL_TYPE_STRING).SetCellValue("时间");
row.CreateCell(5, HSSFCell.CELL_TYPE_STRING).SetCellValue("地址");
row.CreateCell(6, HSSFCell.CELL_TYPE_STRING).SetCellValue("备注");
//依次遍历结果集,并赋值
int i=1;
foreach(var userinfo in list)
{
row = sheet.CreateRow(i);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.UserName);
row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(Convert.ToInt32(userinfo.Age));
row.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Email);
row.CreateCell(3, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Telephone);
row.CreateCell(4, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(Convert.ToDateTime( userinfo.AddDate));
row.CreateCell(5, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Address);
row.CreateCell(6, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Remarks);
i++;
}
//操作文件流,写入到Excel中
using (Stream stream = new FileStream(@"G:\userInfoOut.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
Response.Write("导出成功");
}
catch (Exception ex)
{
Response.Write("错误:" + ex.Message);
}
}
三 ,参考
(1),此种做法只能针对数据量较小的数据,对于数据很多的导出,则不能使用这种方式。
(2),如何使用NPOI把Excel中的数据导入到数据库,请参考本博客相关文章。
(1)可以使用多种方式,但是较好的一种是使用NPOI。
(2)NPOI的缺陷:只能在Office2003中使用,Office2007无法使用NPOI,同时对于WPS也不能使用。
(3)使用是要引入NPOI的dll外部文件,下面的代码使用了简单三层的思想。
二,把数据库中的数据导入到Excel的具体步骤:
protected void Button2_Click(object sender, EventArgs e)
{
try
{
//使用NPOI创建Excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
HSSFRow row;
//使用三层得到所有的数据,数据量小时,可以这么做,当数据量很大时,要使用DataReader
UserInfoBLL userBll = new UserInfoBLL();
IEnumerable<UserInfo> list = userBll.GetAll();
//写入Excel的第一行,标题
row = sheet.CreateRow(0);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("姓名");
row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue("年龄");
row.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue("邮箱");
row.CreateCell(3, HSSFCell.CELL_TYPE_STRING).SetCellValue("手机");
row.CreateCell(4, HSSFCell.CELL_TYPE_STRING).SetCellValue("时间");
row.CreateCell(5, HSSFCell.CELL_TYPE_STRING).SetCellValue("地址");
row.CreateCell(6, HSSFCell.CELL_TYPE_STRING).SetCellValue("备注");
//依次遍历结果集,并赋值
int i=1;
foreach(var userinfo in list)
{
row = sheet.CreateRow(i);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.UserName);
row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(Convert.ToInt32(userinfo.Age));
row.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Email);
row.CreateCell(3, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Telephone);
row.CreateCell(4, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(Convert.ToDateTime( userinfo.AddDate));
row.CreateCell(5, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Address);
row.CreateCell(6, HSSFCell.CELL_TYPE_STRING).SetCellValue(userinfo.Remarks);
i++;
}
//操作文件流,写入到Excel中
using (Stream stream = new FileStream(@"G:\userInfoOut.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
Response.Write("导出成功");
}
catch (Exception ex)
{
Response.Write("错误:" + ex.Message);
}
}
三 ,参考
(1),此种做法只能针对数据量较小的数据,对于数据很多的导出,则不能使用这种方式。
(2),如何使用NPOI把Excel中的数据导入到数据库,请参考本博客相关文章。
相关文章推荐
- 如何使用NPOI 导出到excel和导入excel到数据库
- NPOI从数据库中调取数据直接导出到EXCEL中
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- NPOI从数据库中导出数据到Excel
- NPOI从数据库中导出Excel
- NPOI从数据库中调取数据直接导出到EXCEL中
- asp.net怎样使用NPOI 导出到excel和从excel导入到数据库
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
- NPOI从数据库中导出数据到excel中
- 4000 struts2页面下载,从数据库把数据以excel表格形式导出
- winform做的excel与数据库的导入导出
- MVC NPOI Linq导出Excel通用类
- 将excel中数据导入到sql数据库,,从数据库导出到excel中
- 利用NPOI操作excel导出
- asp.net Mvc Npoi 导出导入 excel
- asp.net从数据库导出数据到word、excel、txt文本文件
- NET使用NPOI组件将数据导出Excel-通用方法 【推荐】
- NPOI 导入导出excel 支持 03 07
- c#关于EXCEL导出数据库的做法
- 从数据库导出数据到word、excel、.txt