| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> asp >> asp.net里导出excel表方法汇总 asp.net li dao chu excel biao fang fa hui zong
 

asp.net里导出excel表方法汇总

2008-10-25 09:18:29 编辑來源:互联网 繁體版 评论
 
 
本文为【asp.net里导出excel表方法汇总】的汉字拼音对照版显示拼音
  1youdatasetshengcheng

  public void CreateExcel(DataSet ds,string typeid,string FileName)

   {

   HttpResponse resp;

   resp = Page.Response;

   resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

   resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);

   string colHeaders= "", ls_item="";

   int i=0;

   //dingyibiaoduixiangyuxingduixiangtongshiyongDataSetduiqizhijinxingchushihua

   DataTable dt=ds.Tables[0];

   DataRow[] myRow=dt.Select("");

   // typeid=="1"shidaochuweiEXCELgeshiwenjiantypeid=="2"shidaochuweiXMLgeshiwenjian

   if(typeid=="1")

   {

   //qudeshujubiaogeliebiaotigebiaotizhijianyi"tfengezuihouyigeliebiaotihoujiahuichefu

   for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+""t";

   colHeaders +=dt.Columns[i].Caption.ToString() +""n";

   //xiangHTTPshuchuliuzhongxieruqudedeshujuxinxi

   resp.Write(colHeaders);

   //zhuxingchulishuju

   foreach(DataRow row in myRow)

   {

   //zaidangqianxingzhongzhuliehuodeshujushujuzhijianyi"tfengejieshushijiahuichefu"n

   for(i=0;i ls_item +=row[i].ToString() + ""t";

   ls_item += row[i].ToString() +""n";

   //dangqianxingshujuxieruHTTPshuchuliubingqiezhikongls_itemyi便bianxiaxingshuju

   resp.Write(ls_item);

   ls_item="";

   }

   }

   else

   {

   if(typeid=="2")

   {

   //congDataSetzhongzhijiedaochuXMLshujubingqiexiedaoHTTPshuchuliuzhong

   resp.Write(ds.GetXml());

   }

   }

   //xiehuanchongquzhongdeshujudaoHTTPtouwenjianzhong

   resp.End();

   }

  2youdatagridshengcheng

  public void ToExcel(System.Web.UI.Control ctl)

   {

   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");

   HttpContext.Current.Response.Charset ="UTF-8";

   HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;

   HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

   ctl.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.Current.Response.End();

   }

  yongfaToExcel(datagrid1);

  3zhegeyongdataview

  public void OutputExcel(DataView dv,string str)

  {

   //

   // TODO: zaicichutianjiagouzaohanshuluoji

   //

   //dvweiyaoshuchudaoExceldeshujustrweibiaotimingcheng

   GC.Collect();

   Application 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;

   //

   //qudebiaoti

   //

   foreach(DataColumn col in dv.Table.Columns)

   {

   colIndex++;

   excel.Cells[4,colIndex] = col.ColumnName;

   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//shezhibiaotigeshiweijuzhongduiqi

   }

   //

   //qudebiaogezhongdeshuju

   //

   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.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");

   xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//shezhiriqixingdeziduangeshiweijuzhongduiqi

   }

   else

   if(col.DataType == System.Type.GetType("System.String"))

   {

   excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();

   xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//shezhizifuxingdeziduangeshiweijuzhongduiqi

   }

   else

   {

   excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();

   }

   }

   }

   //

   //jiazaiyigehejixing

   //

   int rowSum = rowIndex + 1;

   int colSum = 2;

   excel.Cells[rowSum,2] = "heji";

   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;

   //

   //shezhixuanzhongdebufendeyanse

   //

   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();

   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//shezhiweiqianhuangsegongjiyou56zhong

   //

   //qudezhenggebaobiaodebiaoti

   //

   excel.Cells[2,2] = str;

   //

   //shezhizhenggebaobiaodebiaotigeshi

   //

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;

   //

   //shezhibaobiaobiaogeweizuishiyingkuandu

   //

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();

   //

   //shezhizhenggebaobiaodebiaotiweikualiejuzhong

   //

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;

   //

   //huizhibiankuang

   //

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//shezhizuobian线xianjiacu

   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//shezhishangbian线xianjiacu

   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//shezhiyoubian线xianjiacu

   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//shezhixiabian线xianjiacu

   //

   //xianshixiaoguo

   //

   excel.Visible=true;

   //xSt.Export(Server.MapPath(".")+""""+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);

   xBk.SaveCopyAs(Server.MapPath(".")+""""+this.xlfile.Text+".xls");

   ds = null;

   xBk.Close(false, null,null);

  

   excel.Quit();

   System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);

   System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

   System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);

   xBk = null;

   excel = null;

   xSt = null;

   GC.Collect();

   string path = Server.MapPath(this.xlfile.Text+".xls");

   System.IO.FileInfo file = new System.IO.FileInfo(path);

   Response.Clear();

   Response.Charset="GB2312";

   Response.ContentEncoding=System.Text.Encoding.UTF8;

   // tianjiatouxinxiwei"wenjianxiazai/lingcunwei"duihuakuangzhidingmorenwenjianming

   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));

   // tianjiatouxinxizhidingwenjiandaxiaorangliulanqinenggouxianshixiazaijindu

   Response.AddHeader("Content-Length", file.Length.ToString());

  

   // zhidingfanhuideshiyigebunengbeikehuduanduqudeliubixubeixiazai

   Response.ContentType = "application/ms-excel";

  

   // bawenjianliufasongdaokehuduan

   Response.WriteFile(file.FullName);

   // tingzhiyemiandezhixing

  

   Response.End();

  }原文】【拼音
 
 
 
 
上一篇《asp生成excel报表》
下一篇《ASP编程代码:隐藏图片的真实地址》
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
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-Disposition", "attachment;filename=" + FileName); string colHeaders= "", ls_item=""; int i=0; //定义表对象与行对像,同时用DataSet对其值进行初始化 DataTable dt=ds.Tables[0]; DataRow[] myRow=dt.Select(""); // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件 if(typeid=="1") { //取得数据表各列标题,各标题之间以"t分割,最后一个列标题后加回车符 for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+""t"; colHeaders +=dt.Columns[i].Caption.ToString() +""n"; //向HTTP输出流中写入取得的数据信息 resp.Write(colHeaders); //逐行处理数据 foreach(DataRow row in myRow) { //在当前行中,逐列获得数据,数据之间以"t分割,结束时加回车符"n for(i=0;i ls_item +=row[i].ToString() + ""t"; ls_item += row[i].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(); } 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"; HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default; HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ctl.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.Current.Response.End(); } 用法: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 = excel.Workbooks.Add(true); xSt = (_Worksheet)xBk.ActiveSheet; // //取得标题 // foreach(DataColumn col in dv.Table.Columns) { colIndex++; excel.Cells[4,colIndex] = col.ColumnName; xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,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.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐 } else if(col.DataType == System.Type.GetType("System.String")) { excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString(); xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 } else { excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString(); } } } // //加载一个合计行 // int rowSum = rowIndex + 1; int colSum = 2; excel.Cells[rowSum,2] = "合计"; xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter; // //设置选中的部分的颜色 // xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select(); xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种 // //取得整个报表的标题 // excel.Cells[2,2] = str; // //设置整个报表的标题格式 // xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true; xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22; // //设置报表表格为最适应宽度 // xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select(); xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit(); // //设置整个报表的标题为跨列居中 // xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select(); xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection; // //绘制边框 // xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1; xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗 xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗 xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗 xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗 // //显示效果 // excel.Visible=true; //xSt.Export(Server.MapPath(".")+""""+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML); xBk.SaveCopyAs(Server.MapPath(".")+""""+this.xlfile.Text+".xls"); ds = null; xBk.Close(false, null,null); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null; excel = null; xSt = null; GC.Collect(); string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path); Response.Clear(); Response.Charset="GB2312"; Response.ContentEncoding=System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(file.FullName); // 停止页面的执行 Response.End(); }
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有