.net上的POI插件提供强大的生成Excel函数库,有图有真相
使用方法:
添加以下引用
创建一个一般处理程序:CreateExcel.ashx
<%@ WebHandler Language="C#" Class="CreateExcel" %>
using System;
using System.Web;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HPSF;
using NPOI.HSSF.Util;
public class CreateExcel : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/x-excel";//设置输出类型excel
string filename = HttpUtility.UrlEncode("动态生成.xls");//文件名中文要编码
context.Response.AddHeader("Content-Disposition","attachment;filename="+filename);//设置HTTP协议头输出流类型,及默认文件名
HSSFWorkbook hwb = new HSSFWorkbook();//创建一个excel对象
HSSFSheet sheet1 = hwb.CreateSheet("sheet1");//创建一张表(sheet)对象
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();//文档摘要信息
dsi.Company = "小江工作室";//公司
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();//文档摘要信息
si.Subject = "www.xiaojiang-design.com";//文档主题
si.Title = "测试用excel";//标题
hwb.DocumentSummaryInformation = dsi;//添加到execl对象里
hwb.SummaryInformation = si;
//HSSFRow row1 = sheet1.CreateRow(0);//创建一个行
//row1.CreateCell(0,HSSFCell.CELL_TYPE_STRING).SetCellValue("2011-7-26");//在行里创建一个单元格,并赋值
//row1.GetCell(0).SetCellValue(3.14);//修改一个单元格的值
HSSFRow row = sheet1.CreateRow(0);
HSSFCell cell = row.CreateCell(0);
cell.SetCellValue("销售统计表");//标题名
HSSFCellStyle style = hwb.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;//字体
HSSFFont font = hwb.CreateFont();
font.FontHeight = 20 * 20;//大小
style.SetFont(font);
cell.CellStyle = style;
sheet1.AddMergedRegion(new Region(0, 0, 0, 5));//合并单元格
sheet1.CreateRow(1).CreateCell(0).SetCellValue("这是第二行的第一个单元格");//简写
//sheet1.GetRow(1).CreateCell(0).SetCellValue("修改第二行的第一个单元格的值");
hwb.Write(context.Response.OutputStream);//将内容以二进制流输出
}
public bool IsReusable {
get {
return false;
}
}
}
在需要下载的地方创建一个超链接到这个CreateExcel.ashx,即可!
付NPOI教程:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html
从数据库中读数据生成excel
<%@ WebHandler Language="C#" Class="CreateExcel" %>
using System;
using System.Web;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using System.Data;
using System.Data.OleDb;
public class CreateExcel : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/x-excel";
string filename = HttpUtility.UrlEncode("用户列表.xls");//文件名中文要编码
context.Response.AddHeader("Content-Disposition","attachment;filename="+filename);//设置HTTP协议头输出流类型,及默认文件名
HSSFWorkbook hwb = new HSSFWorkbook();//创建一个excel对象
HSSFSheet sheet1 = hwb.CreateSheet("sheet1");//创建一张表(sheet)对象
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();//文档摘要信息
dsi.Company = "小江工作室";//公司
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();//文档摘要信息
si.Subject = "www.xiaojiang-design.com";//文档主题
si.Title = "测试用excel";//标题
hwb.DocumentSummaryInformation = dsi;//添加到execl对象里
hwb.SummaryInformation = si;
//HSSFRow row1 = sheet1.CreateRow(0);//创建一个行
//row1.CreateCell(0,HSSFCell.CELL_TYPE_STRING).SetCellValue("2011-7-26");//在行里创建一个单元格,并赋值
//row1.GetCell(0).SetCellValue(3.14);//修改一个单元格的值
HSSFRow row = sheet1.CreateRow(0);
HSSFCell cell = row.CreateCell(0);
cell.SetCellValue("会员用户列表");//标题名
HSSFCellStyle style = hwb.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;//字体
HSSFFont font = hwb.CreateFont();
font.FontHeight = 20 * 20;//大小
style.SetFont(font);
cell.CellStyle = style;
sheet1.AddMergedRegion(new Region(0, 0, 0, 4));//合并单元格
jiang_Db newdb = new jiang_Db();
newdb.Open();
DataTable dt = newdb.Re_DataTable("select * from [member_inf]");//从数据库取出数据
newdb.Close();
if (dt.Rows.Count>0)
{
HSSFFont font1 = hwb.CreateFont();//创建字体样式对象
font1.FontHeightInPoints = 13;//字号
font1.Boldweight = 50;//加粗
font1.FontName = "宋体";//字体
HSSFCellStyle style1 = hwb.CreateCellStyle();
style1.SetFont(font1);
style1.FillForegroundColor = HSSFColor.YELLOW.index;//行背景颜色
HSSFRow row_title = sheet1.CreateRow(1);//创建行
//row_title.HeightInPoints = 14;//行高
sheet1.SetColumnWidth(2,20*256);//指定第三列宽度为20个字符
sheet1.SetColumnWidth(4, 20 * 256);//指定第5列宽度为20个字符
HSSFCell cell0 = row_title.CreateCell(0);//创建列
cell0.CellStyle = style1;//列样式
HSSFCell cell1 = row_title.CreateCell(1);
cell1.CellStyle = style1;
HSSFCell cell2 = row_title.CreateCell(2);
cell2.CellStyle = style1;
HSSFCell cell3 = row_title.CreateCell(3);
cell3.CellStyle = style1;
HSSFCell cell4 = row_title.CreateCell(4);
cell4.CellStyle = style1;
cell0.SetCellValue("用户名");//标题列名
cell1.SetCellValue("性别");
cell2.SetCellValue("出身日期");
cell3.SetCellValue("年龄");
cell4.SetCellValue("注册日期");
for (int i = 0; i < dt.Rows.Count; i++)//把datatable数据写到行里
{
sheet1.CreateRow(i+2).CreateCell(0).SetCellValue(dt.Rows[i]["nickname"].ToString());
sheet1.CreateRow(i + 2).CreateCell(1).SetCellValue(dt.Rows[i]["sex"].ToString());
sheet1.CreateRow(i + 2).CreateCell(2).SetCellValue(dt.Rows[i]["birthday"].ToString());
sheet1.CreateRow(i + 2).CreateCell(3).SetCellValue(dt.Rows[i]["age"].ToString());
sheet1.CreateRow(i + 2).CreateCell(4).SetCellValue(dt.Rows[i]["time"].ToString());
}
}
//sheet1.CreateRow(1).CreateCell(0).SetCellValue("这是第二行的第一个单元格");//简写
//sheet1.GetRow(1).CreateCell(0).SetCellValue("修改第二行的第一个单元格的值");
hwb.Write(context.Response.OutputStream);//将内容以二进制流输出
}
public bool IsReusable {
get {
return false;
}
}
}