.net上的POI外掛程式提供強大的產生Excel函數庫,有圖有真相
使用方法:
首先複製一下檔案到網站Lib檔案夾下
添加以下引用
建立一個一般處理常式: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; } }}