[ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4743字,纯文字阅读大概需要7分钟。
内容图文
![[ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版)](/upload/InfoBanner/zyjiaocheng/716/951f3434b3a341fe81a08f9dabac632f.jpg)
读取DB并导出EXCEL
附档须先下载配合使用,附档中有说明文档。
黄色部分为读取DB并导出EXCEL的程序,其余无背景色的为ado.net(datareader)
//----自己写的(声明) ----一般必写
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
//== 自己写的(声明) === Excel用
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel; ?
protected void Button1_Click(object sender, EventArgs e)
? ? {
?//* for Exporting to a Excel file
? ? ? ? HSSFWorkbook workbook = new HSSFWorkbook();
//== 新增试算表 Sheet名称。使用 NPOI.SS.UserModel命名空间。(v.1.2.4版)
? ? ? ? ISheet u_sheet = (ISheet)workbook.CreateSheet("My Sheet_124");
//== 插入数据值,我先建立excel的表头,从数据库捞出不会有表头
? ? ? ? // CreateRow()方法
? ? ? ? //同列第1格开始须先用“CreateRow”
? ? ? ? u_sheet.CreateRow(0).CreateCell(0).SetCellValue("此栏可删");
? ? ? ? //同列第2格开始须用“GetRow”,否则在excel中只会出现最后1格
? ? ? ? u_sheet.GetRow(0).CreateCell(1).SetCellValue("年月");
? ? ? ? u_sheet.GetRow(0).CreateCell(2).SetCellValue("姓名");
? ? ? ? u_sheet.GetRow(0).CreateCell(3).SetCellValue("应享特休");
? ? ? ? u_sheet.GetRow(0).CreateCell(4).SetCellValue("剩余特休");
? ? ? ? u_sheet.GetRow(0).CreateCell(5).SetCellValue("特");
? ? ? ? u_sheet.GetRow(0).CreateCell(6).SetCellValue("事");
? ? ? ? u_sheet.GetRow(0).CreateCell(7).SetCellValue("病");
? ? ? ? u_sheet.GetRow(0).CreateCell(8).SetCellValue("婚");
? ? ? ? u_sheet.GetRow(0).CreateCell(9).SetCellValue("丧");
? ? ? ? u_sheet.GetRow(0).CreateCell(10).SetCellValue("产");
? ? ? ? u_sheet.GetRow(0).CreateCell(11).SetCellValue("公");
? ? ? ? u_sheet.GetRow(0).CreateCell(12).SetCellValue("加班时数");
? ? ? ? u_sheet.GetRow(0).CreateCell(13).SetCellValue("上班天数");
? ? ? ? u_sheet.GetRow(0).CreateCell(14).SetCellValue("上班时数");
? ? ? ? u_sheet.GetRow(0).CreateCell(15).SetCellValue("迟到次数");
? ? ? ? u_sheet.GetRow(0).CreateCell(16).SetCellValue("未打卡次数");
? ? ? ? u_sheet.GetRow(0).CreateCell(17).SetCellValue("逾时打卡");
? ? ? ? u_sheet.GetRow(0).CreateCell(18).SetCellValue("备注");
//以下为ADO.NET (datareader)
//声明连线字符串
? ? ? ? string ds = WebConfigurationManager.ConnectionStrings["workerConnectionString"].ConnectionString;
? ? ? ? SqlConnection conn = new SqlConnection(ds);
? ? ? ? SqlDataReader dr3 = null;
? ? ? ??string dc = "select ?* from mytable";
? ? ??SqlCommand cmd3 = new SqlCommand(Session["dc"].ToString(), conn);
try
? ? ? {
? ? ? ? ? //== 第一,连结数据库。
? ? ? ? ? conn.Open();
? ? ? ? ? //== 第二,执行SQL命令。
? ? ? ? ? dr3 = cmd3.ExecuteReader();
//由此开始处理导出excel===================================
//== 利用循环,把数据写入 Excel各个保存格里面。
int k = 1;
//k从1开始代表是excel的第2行开始建立,因为第1行已给表头使用了
while (dr3.Read())
{
// 先建好一列(Row),才能去作格子(Cell)
IRow u_Row = u_sheet.CreateRow(k);
for (int i = 0; i < dr3.FieldCount; i++)
{ //-- FieldCount是指 DataReader每一列纪录里面,有几个字段。
u_Row.CreateCell(i).SetCellValue(dr3.GetValue(i).ToString());
//== .CreateCell() 可设定为同一列(Row)的 [第几个格子]
//补充:设定每一个字段(格子)的保存格型态,如:字符串。
//u_Row.CreateCell(i).SetCellType(CellType.STRING);
}
k++;
}
//结束处理导出excel===================================
? ? ? }
? ? ? catch (Exception ex) ?//---- 如果程序有错误或是例外状况,将执行这一段
? ? ? {
? ? ? ? ? Response.Write("ERROE----" + ex.ToString() + "
");
? ? ? ? ? throw;
? ? ? }
? ? ? // == 第四,释放资源、关闭数据库的连结。
? ? ? finally
? ? ? {
? ? ? ? ? if (dr3 != null)
? ? ? ? ? {
? ? ? ? ? ? ? cmd3.Cancel();
? ? ? ? ? ? ? dr3.Close();
? ? ? ? ? }
? ? ? ? ? if (conn.State == ConnectionState.Open)
? ? ? ? ? {
? ? ? ? ? ? ? conn.Close();
? ? ? ? ? ? ? conn.Dispose();
? ? ? ? ? }
? ? ? }
?//* for Exporting to a Excel file
MemoryStream ms = new MemoryStream(); //==需要 System.IO命名空间
workbook.Write(ms);
//== Excel文件名,请写在最后面 filename的地方
Response.AddHeader("Content-Disposition", "attachment; filename=出勤加班表_" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
Response.BinaryWrite(ms.ToArray());
//== 释放资源
workbook = null;
ms.Close();
ms.Dispose();
? ? }
附档下载
(以上参考mis2000的教学后实践整理出来的)
?
--
强烈建议购物网店或实例店家都必须使用关键字广告or原生广告来将Yahoo上与联播网的广大流量导至自己的网站!
●Yahoo关键字广告/原生广告
◆Yahoo广告方案介绍 : https://goo.gl/5k8FHW
◆Yahoo广告剖析与运用 : http://goo.gl/4xjUJD
??
原文:大专栏 [ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版)
内容总结
以上是互联网集市为您收集整理的[ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版)全部内容,希望文章能够帮你解决[ASP.net](note)读取DB并导出EXCEL (NPOI之 v.1.2.4版)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。