下面介绍下我根据网上学习C#中导出EXCEL的几种方法:
一、asp.net导出Excel
1.将整个html全部输出到Excel
此方法会将html中所有的内容,如按钮、表格、图片等全部输出
View Code
1 Response.Clear();2 Response.Buffer= true;3 Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");4 Response.ContentEncoding=System.Text.Encoding.UTF8;5 Response.ContentType = "application/vnd.ms-excel";6 this.EnableViewState = false;
2.将DataGrid控件中的数据导出到Excel(也可以是其他控件)
此方法若使用分页,则导出当前页DataGrid中显示的信息。
View Code
1 public voidDGToExcel(System.Web.UI.Control ctl)2 {3 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");4 HttpContext.Current.Response.Charset ="UTF-8";5 HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;6 HttpContext.Current.Response.ContentType ="application/ms-excel";7 ctl.Page.EnableViewState =false;8 System.IO.StringWriter tw = newSystem.IO.StringWriter() ;9 System.Web.UI.HtmlTextWriter hw = newSystem.Web.UI.HtmlTextWriter (tw);10 ctl.RenderControl(hw);11 HttpContext.Current.Response.Write(tw.ToString());12 HttpContext.Current.Response.End();13 }
用法DGToExcel(datagrid1);
3.将DataSet中的数据导出到Excel
View Code
1 public void CreateExcel(DataSet ds,stringFileName)2 {3 HttpResponse resp;4 resp =Page.Response;5 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");6 resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);7 string colHeaders= "", ls_item="";8
9 //定义表对象与行对象,同时用DataSet对其值进行初始化
10 DataTable dt=ds.Tables[0];11 DataRow[] myRow=dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
12 int i=0;13 int cl=dt.Columns.Count;14
15 //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
16 for(i=0;i
19 {20 colHeaders +=dt.Columns[i].Caption.ToString() +"n";21 }22 else
23 {24 colHeaders+=dt.Columns[i].Caption.ToString()+"t";25 }26
27 }28 resp.Write(colHeaders);29 //向HTTP输出流中写入取得的数据信息30
31 //逐行处理数据
32 foreach(DataRow row inmyRow)33 {34 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
35 for(i=0;i
38 {39 ls_item +=row[i].ToString()+"n";40 }41 else
42 {43 ls_item+=row[i].ToString()+"t";44 }45
46 }47 resp.Write(ls_item);48 ls_item="";49
50 }51 resp.End();
以ms-excel的格式response到http流,参数ds为填充有数据表的DataSet,文件名是全名,包括后缀名,如Excel2006.xls。
4.将dataview导出到Excel
View Code
1 public void OutputExcel(DataView dv,stringstr)2 {3 //dv为要输出到Excel的数据,str为标题名称
4 GC.Collect();5 Application excel;//= new Application();
6 int rowIndex=4;7 int colIndex=1;8
9 _Workbook xBk;10 _Worksheet xSt;11
12 excel= newApplicationClass();13
14 xBk = excel.Workbooks.Add(true);15
16 xSt =(_Worksheet)xBk.ActiveSheet;17
18 //
19 //取得标题20 //
21 foreach(DataColumn col indv.Table.Columns)22 {23 colIndex++;24 excel.Cells[4,colIndex] =col.ColumnName;25 xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
26 }27
28 //
29 //取得表格中的数据30 //
31 foreach(DataRowView row indv)32 {33 rowIndex ++;34 colIndex = 1;35 foreach(DataColumn col indv.Table.Columns)36 {37 colIndex ++;38 if(col.DataType == System.Type.GetType("System.DateTime"))39 {40 excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");41 xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
42 }43 else
44 if(col.DataType == System.Type.GetType("System.String"))45 {46 excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();47 xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
48 }49 else
50 {51 excel.Cells[rowIndex,colIndex] =row[col.ColumnName].ToString();52 }53 }54 }55 //
56 //加载一个合计行57 //
58 int rowSum = rowIndex + 1;59 int colSum = 2;60 excel.Cells[rowSum,2] = "合计";61 xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment =XlHAlign.xlHAlignCenter;62 //
63 //设置选中的部分的颜色64 //
65 xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();66 xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种67 //
68 //取得整个报表的标题69 //
70 excel.Cells[2,2] =str;71 //
72 //设置整个报表的标题格式73 //
74 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;75 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;76 //
77 //设置报表表格为最适应宽度78 //
79 xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIn