《页面数据导出excel表方法》由会员分享,可在线阅读,更多相关《页面数据导出excel表方法(5页珍藏版)》请在金锄头文库上搜索。
1、/1、由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) HttpResponse resp; resp = Page.Response; resp.ContentEncoding = (GB2312); resp.AppendHeader(Content-Disposition, attachment;filename= + FileName); string colHeaders= , ls_item=; int i=0; /定义表对象与行对像,同时用DataSet对其值进行初始化 Dat
2、aTable dt=ds.Tables0; DataRow myRow=dt.Select(); / typeid=1时导出为EXCEL格式文件;typeid=2时导出为XML格式文件 if(typeid=1) /取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符 for(i=0;i colHeaders+=dt.Columnsi.Caption.ToString()+t; colHeaders +=dt.Columnsi.Caption.ToString() +n; /向HTTP输出流中写入取得的数据信息 resp.Write(colHeaders); /逐行处理数据 fore
3、ach(DataRow row in myRow) /在当前行中,逐列获得数据,数据之间以t分割,结束时加回车符n for(i=0;i ls_item +=rowi.ToString() + t; ls_item += rowi.ToString() +n; /当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 resp.Write(ls_item); ls_item=; else if(typeid=2) /从DataSet中直接导出XML数据并且写到HTTP输出流中 resp.Write(ds.GetXml(); /写缓冲区中的数据到HTTP头文件中 resp.End();
4、/2、由datagrid生成 public void ToExcel( ctl) HttpContext.Current.Response.AppendHeader(Content-Disposition,attachment;filename=Excel.xls); =UTF-8; =; =application/ms-excel;/image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword =false; tw = new () ; hw = new (tw); ctl.RenderControl(hw); (tw.ToString(); ();
5、用法:ToExcel(datagrid1); /3、这个用dataview public void OutputExcel(DataView dv,string str) / / TODO: 在此处添加构造函数逻辑 / /dv为要输出到Excel的数据,str为标题名称 GC.Collect(); Application excel;/ = new Application(); int rowIndex=4; int colIndex=1; _Workbook xBk; _Worksheet xSt; excel= new ApplicationClass(); xBk = (true); x
6、St = (_Worksheet)xBk.ActiveSheet; / /取得标题 / foreach(DataColumn col in ) colIndex+; excel.Cells4,colIndex = col.ColumnName; xSt.get_Range(excel.Cells4,colIndex,excel.Cells4,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置标题格式为居中对齐 / /取得表格中的数据 / foreach(DataRowView row in dv) rowIndex +; co
7、lIndex = 1; foreach(DataColumn col in ) colIndex +; if(col.DataType = (System.DateTime) excel.CellsrowIndex,colIndex = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd); xSt.get_Range(excel.CellsrowIndex,colIndex,excel.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAli
8、gnCenter;/设置日期型的字段格式为居中对齐 else if(col.DataType = (System.String) excel.CellsrowIndex,colIndex = +rowcol.ColumnName.ToString(); xSt.get_Range(excel.CellsrowIndex,colIndex,excel.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置字符型的字段格式为居中对齐 else excel.CellsrowIndex,colIndex = r
9、owcol.ColumnName.ToString(); / /加载一个合计行 / int rowSum = rowIndex + 1; int colSum = 2; excel.CellsrowSum,2 = 合计; xSt.get_Range(excel.CellsrowSum,2,excel.CellsrowSum,2).HorizontalAlignment = XlHAlign.xlHAlignCenter; / /设置选中的部分的颜色 / xSt.get_Range(excel.CellsrowSum,colSum,excel.CellsrowSum,colIndex).Sele
10、ct(); xSt.get_Range(excel.CellsrowSum,colSum,excel.CellsrowSum,colIndex).Interior.ColorIndex = 19;/设置为浅黄色,共计有56种 / /取得整个报表的标题 / excel.Cells2,2 = str; / /设置整个报表的标题格式 / xSt.get_Range(excel.Cells2,2,excel.Cells2,2).Font.Bold = true; xSt.get_Range(excel.Cells2,2,excel.Cells2,2).Font.Size = 22; / /设置报表表格
11、为最适应宽度 / xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,colIndex).Select(); xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,colIndex).Columns.AutoFit(); / /设置整个报表的标题为跨列居中 / xSt.get_Range(excel.Cells2,2,excel.Cells2,colIndex).Select(); xSt.get_Range(excel.Cells2,2,excel.Cells2,colIndex).HorizontalAlig
12、nment = XlHAlign.xlHAlignCenterAcrossSelection; / /绘制边框 / xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,colIndex).Borders.LineStyle = 1; xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,2).BordersXlBordersIndex.xlEdgeLeft.Weight = XlBorderWeight.xlThick;/设置左边线加粗 xSt.get_Range(excel.Cells4,2,excel.Cells4,colIndex).BordersXlBordersIndex.xlEdgeTop.Weight = XlBorderWeight.xlThick;/设置上边线加粗 xSt.get_Range(excel.Cells4,colIndex,excel.CellsrowSum,colIndex).BordersXlBordersIndex.xlEdgeRight.Weight = XlBorderWeight.xlThick;/设置右边线加粗 xSt.ge