ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:19.93KB ,
资源ID:7641884      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-7641884.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(导出Excel文件Word文档格式.docx)为本站会员(b****3)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

导出Excel文件Word文档格式.docx

1、 this.EnableViewState false;这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出)图片、word文档等。下面的方法,也均用了这个属性。2、将DataGrid控件中的数据导出excel上述方法虽然实现了导出的功能,但同时

2、把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。System.Web.UI.Control ctl=this.DataGrid1;/DataGrid1是你在窗体中拖放的控件HttpContext.Current.Response.AppendHeader(filename=Excel.xlsHttpContext.Current.Response.Charset =UTF-8HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;HttpCon

3、text.Current.Response.ContentType =application/ms-excelctl.Page.EnableViewState =false;System.IO.StringWriter tw = new System.IO.StringWriter() ;System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);ctl.RenderControl(hw);HttpContext.Current.Response.Write(tw.ToString();HttpContext.

4、Current.Response.End();如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。为方便使用,写成方法如下:public void DGToExcel(System.Web.UI.Control ctl) HttpContext.Current.Response.AppendHeader( HttpContext.Current.Response.Charset = HttpContext.Current.Response.ContentEncoding =System.Text.Encod

5、ing.Default; HttpContext.Current.Response.ContentType = ctl.Page.EnableViewState =false; System.IO.StringWriter System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString(); HttpContext.Current.Response.End(); 用法:DGT

6、oExcel(datagrid1);3、将DataSet中的数据导出excel有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如excel2006.xls public void CreateExcel(DataSet ds,string FileName) HttpResponse resp;resp = Page.Response;res

7、p.ContentEncoding = System.Text.Encoding.GetEncoding(GB2312resp.AppendHeader(, +FileName);string colHeaders= , ls_item=/定义表对象与行对象,同时用DataSet对其值进行初始化 DataTable dt=ds.Tables0;DataRow myRow=dt.Select();/可以类似dt.Select(id10)之形式达到数据筛选目的 int i=0; int cl=dt.Columns.Count;/取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符 fo

8、r(i=0;i if(i=(cl-1)/最后一列,加ncolHeaders +=dt.Columnsi.Caption.ToString() +nelsecolHeaders+=dt.Columnsi.Caption.ToString()+tresp.Write(colHeaders);/向HTTP输出流中写入取得的数据信息 /逐行处理数据foreach(DataRow row in myRow) /当前行数据写入HTTP输出流,并且置空ls_item以便下行数据ls_item +=rowi.ToString()+ls_item+=rowi.ToString()+resp.Write(ls_i

9、tem);ls_item=resp.End(); 4、将dataview导出excel 若想实现更加富于变化或者行列不规则的excel导出时,可用本法。public void OutputExcel(DataView dv,string str) /dv为要输出到Excel的数据,str为标题名称 GC.Collect(); Application excel;/ = new Application(); int rowIndex=4; int colIndex=1; _Workbook xBk; _Worksheet xSt; excel= new ApplicationClass(); x

10、Bk = excel.Workbooks.Add(true); xSt = (_Worksheet)xBk.ActiveSheet; / /取得标题 foreach(DataColumn col in dv.Table.Columns) colIndex+; excel.Cells4,colIndex = col.ColumnName; xSt.get_Range(excel.Cells4,colIndex,excel.Cells4,colIndex).HorizontalAlignment = XlVAlign.xlVAlignCenter;/设置标题格式为居中对齐 /取得表格中的数据 fo

11、reach(DataRowView row in dv) rowIndex +; colIndex = 1; colIndex +; if(col.DataType = System.Type.GetType(System.DateTime) excel.CellsrowIndex,colIndex = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd xSt.get_Range(excel.CellsrowIndex,colIndex,excel.CellsrowIndex,colIndex).Horiz

12、ontalAlignment = XlVAlign.xlVAlignCenter;/设置日期型的字段格式为居中对齐 else System.String excel.CellsrowIndex,colIndex = +rowcol.ColumnName.ToString();/设置字符型的字段格式为居中对齐 excel.CellsrowIndex,colIndex = rowcol.ColumnName.ToString(); /加载一个合计行 int rowSum = rowIndex + 1; int colSum = 2; excel.CellsrowSum,2 = 合计 xSt.get

13、_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种 /取得整个报表的标

14、题 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

15、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.

16、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,c

17、olIndex).BordersXlBordersIndex.xlEdgeRight.Weight = XlBorderWeight.xlThick;/设置右边线加粗 xSt.get_Range(excel.CellsrowSum,2,excel.CellsrowSum,colIndex).BordersXlBordersIndex.xlEdgeBottom.Weight = XlBorderWeight.xlThick;/设置下边线加粗 /显示效果 excel.Visible=true; /xSt.Export(Server.MapPath(.+this.xlfile.Text+,Sheet

18、ExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML); xBk.SaveCopyAs(Server.MapPath( ds = null; xBk.Close(false, null,null); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject

19、(excel); System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null; excel = null; xSt = null; string path = Server.MapPath(this.xlfile.Text+ System.IO.FileInfo file = new System.IO.FileInfo(path); Response.Charset= / 添加头信息,为文件下载/另存为对话框指定默认文件名 Response.AddHeader( filename= + Server.Url

20、Encode(file.Name); / 添加头信息,指定文件大小,让浏览器能够显示下载进度 Content-Length, file.Length.ToString(); / 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = / 把文件流发送到客户端 Response.WriteFile(file.FullName); / 停止页面的执行 Response.End(); 上面的方面,均将要导出的excel数据,直接给浏览器输出文件流,下面的方法是首先将其存到服务器的某个文件夹中,然后把文件发送到客户端。这样可以持久的把导出的文件存起来,以便实现其

21、它功能。5、将execl文件导出到服务器上,再下载。二、winForm中导出Execl的方法:1、方法1: SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettingsconn); SqlDataAdapter da=new SqlDataAdapter(select * from tb1,conn); DataSet ds=new DataSet(); da.Fill(ds,table1 DataTable dt=ds.Tables; string name=System

22、.Configuration.ConfigurationSettings.AppSettingsdownloadurl.ToString()+DateTime.Today.ToString()+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+.csv/存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数 FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write); StreamWriter sw=new St

23、reamWriter(fs,System.Text.Encoding.GetEncoding(gb2312); sw.WriteLine(自动编号,姓名,年龄 foreach(DataRow dr in dt.Rows) sw.WriteLine(drID+drvNameiAge sw.Close(); + Server.UrlEncode(name);/ 指定返回的是一个不能被客户端读取的流,必须被下载 Response.WriteFile(name);public void Out2Excel(string sTableName,string url)Excel.Application oExcel=new Excel.Application();Workbooks oBooks;Workbook oBook;Sheets oSheets;Worksheet oSheet;Range oCells;string sFile=,sTemplate=/

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2