asp.net中导出excel数据的方法汇总_4

上传人:bin****86 文档编号:59370279 上传时间:2018-11-06 格式:DOCX 页数:21 大小:22.31KB
返回 下载 相关 举报
asp.net中导出excel数据的方法汇总_4_第1页
第1页 / 共21页
asp.net中导出excel数据的方法汇总_4_第2页
第2页 / 共21页
asp.net中导出excel数据的方法汇总_4_第3页
第3页 / 共21页
asp.net中导出excel数据的方法汇总_4_第4页
第4页 / 共21页
asp.net中导出excel数据的方法汇总_4_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《asp.net中导出excel数据的方法汇总_4》由会员分享,可在线阅读,更多相关《asp.net中导出excel数据的方法汇总_4(21页珍藏版)》请在金锄头文库上搜索。

1、我真正系统地接触和学习党的基本知识是在这次中级党校的培训班上。通过学习,了解了党的发展历程,对党的性质、宗旨、任务等基本知识有了进一步的了解中导出excel数据的方法汇总1、由dataset生成代码如下:public void CreateExcel(DataSet ds,string typeid,string FileName)HttpResponse resp;resp = Page.Response;resp.ContentEncoding = System.Text.Encoding.GetEncoding(GB2312);resp.AppendHeader(Content-Disp

2、osition, attachment;filename= + FileName);string colHeaders= , ls_item=;int i=0;/定义表对象与行对像,同时用DataSet对其值进行初始化DataTable 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()+

3、t;colHeaders +=dt.Columnsi.Caption.ToString() +n;/向HTTP输出流中写入取得的数据信息resp.Write(colHeaders);/逐行处理数据foreach(DataRow row in myRow)/在当前行中,逐列获得数据,数据之间以t分割,结束时加回车符nfor(i=0;i ls_item +=rowi.ToString() + t;ls_item += rowi.ToString() +n;/当前行数据写入HTTP输出流,并且置空ls_item以便下行数据resp.Write(ls_item);ls_item=;elseif(typ

4、eid=2)/从DataSet中直接导出XML数据并且写到HTTP输出流中resp.Write(ds.GetXml();/写缓冲区中的数据到HTTP头文件中resp.End();2、由datagrid生成代码如下:public void ToExcel(System.Web.UI.Control ctl)HttpContext.Current.Response.AppendHeader(Content-Disposition,attachment;filename=Excel.xls);HttpContext.Current.Response.Charset =UTF-8;HttpContex

5、t.Current.Response.ContentEncoding =System.Text.Encoding.Default;HttpContext.Current.Response.ContentType =application/ms-excel;/image/JPEG;text/HTML;image/GIF;vnd.ms-excel/mswordctl.Page.EnableViewState =false;System.IO.StringWriter tw = new System.IO.StringWriter() ;System.Web.UI.HtmlTextWriter hw

6、 = new System.Web.UI.HtmlTextWriter (tw);ctl.RenderControl(hw);HttpContext.Current.Response.Write(tw.ToString();HttpContext.Current.Response.End();用法:ToExcel(datagrid1);3、这个用dataview代码如下:public void OutputExcel(DataView dv,string str)/ TODO: 在此处添加构造函数逻辑/dv为要输出到Excel的数据,str为标题名称GC.Collect();Applicati

7、on excel;/ = new Application();int rowIndex=4;int colIndex=1;_Workbook xBk;_Worksheet xSt;excel= new ApplicationClass();xBk = excel.Workbooks.Add(true);xSt = (_Worksheet)xBk.ActiveSheet;/取得标题/foreach(DataColumn col in dv.Table.Columns)colIndex+;excel.Cells4,colIndex = col.ColumnName;xSt.get_Range(ex

8、cel.Cells4,colIndex,excel.Cells4,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置标题格式为居中对齐/取得表格中的数据/foreach(DataRowView row in dv)rowIndex +;colIndex = 1;foreach(DataColumn col in dv.Table.Columns)colIndex +;if(col.DataType = System.Type.GetType(System.DateTime)excel.CellsrowIndex,colInde

9、x = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd);xSt.get_Range(excel.CellsrowIndex,colIndex,excel.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置日期型的字段格式为居中对齐elseif(col.DataType = System.Type.GetType(System.String)excel.CellsrowIndex,colIndex = +row

10、col.ColumnName.ToString();xSt.get_Range(excel.CellsrowIndex,colIndex,excel.CellsrowIndex,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置字符型的字段格式为居中对齐elseexcel.CellsrowIndex,colIndex = rowcol.ColumnName.ToString();/加载一个合计行/int rowSum = rowIndex + 1;int colSum = 2;excel.CellsrowSum,2 = 合计;

11、xSt.get_Range(excel.CellsrowSum,2,excel.CellsrowSum,2).HorizontalAlignment = XlHAlign.xlHAlignCenter;/设置选中的部分的颜色/xSt.get_Range(excel.CellsrowSum,colSum,excel.CellsrowSum,colIndex).Select();xSt.get_Range(excel.CellsrowSum,colSum,excel.CellsrowSum,colIndex).Interior.ColorIndex = 19;/设置为浅黄色,共计有56种/取得整个

12、报表的标题/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;/设置报表表格为最适应宽度/xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,colIndex).Select();xSt.get_Range(excel.Cells4,2,excel.CellsrowSum,colIndex).C

13、olumns.AutoFit();/设置整个报表的标题为跨列居中/xSt.get_Range(excel.Cells2,2,excel.Cells2,colIndex).Select();xSt.get_Range(excel.Cells2,2,excel.Cells2,colIndex).HorizontalAlignment = 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;/设置上边线加粗xS

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号