asp.net学习笔记·将数据库中的数据保存在EXCEL文件中
生活随笔
收集整理的這篇文章主要介紹了
asp.net学习笔记·将数据库中的数据保存在EXCEL文件中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
提取數(shù)據(jù)庫中的數(shù)據(jù),將其保存在EXCEL文件中,并提供下載。
?
在一般處理程序中將數(shù)據(jù)庫數(shù)據(jù)保存在EXCEL文件中的代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; using NPOI.HSSF.UserModel; using good.DAl; using System.Data;namespace good {/// <summary>/// excel 的摘要說明/// </summary>public class excel : IHttpHandler{Sql sql = new Sql();IDataReader reader;public void ProcessRequest(HttpContext context){context.Response.ContentType = "application/x-excel"; //設(shè)置類型string filename = HttpUtility.UrlEncode("動態(tài)數(shù)據(jù).xls");context.Response.AddHeader("Content-Disposition","attachment;filename=" + filename);reader = sql.GetReader();HSSFWorkbook workbook = new HSSFWorkbook();//新建EXCEL文件HSSFSheet sheet = workbook.CreateSheet();//新建頁if (reader!= null ){while (reader.Read()){int i = 0;string userName = reader.GetString(reader.GetOrdinal("Name"));int id = (int)reader.GetValue(reader.GetOrdinal("ID"));HSSFRow row = sheet.CreateRow(i); //創(chuàng)建行row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(userName);//創(chuàng)建cell并給Cell設(shè)置數(shù)據(jù)類型以及數(shù)據(jù)row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(id);}workbook.Write(context.Response.OutputStream);//將生成的EXCEL文件流輸出到上下文中}else{}//HSSFWorkbook workbook = new HSSFWorkbook();//HSSFSheet sheet = workbook.CreateSheet();//HSSFRow row1 = sheet.CreateRow(0);//HSSFCell cell1 = row1.CreateCell(0, HSSFCell.CELL_TYPE_STRING);//cell1.SetCellValue("你好");}public bool IsReusable{get{return false;}}} }
操作數(shù)據(jù)庫的代碼
public IDataReader GetReader(){SqlConnection conn = new SqlConnection(strConn);try{conn.Open();string strCmd = "select * from Person";SqlCommand cmd = new SqlCommand(strCmd,conn);IDataReader reader = cmd.ExecuteReader();return reader;}catch (Exception ex){Console.WriteLine(ex.Message);return null;}}
轉(zhuǎn)載于:https://www.cnblogs.com/xuhongfei/archive/2012/12/25/2833075.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的asp.net学习笔记·将数据库中的数据保存在EXCEL文件中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kinect+OpenNI学习笔记之12
- 下一篇: asp.net ajax控件工具集 Au