| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> asp >> asp.net li dao chu excel biao fang fa hui zong

asp.net li dao chu excel biao fang fa hui zong

2008-10-25 09:18:29 编辑來源:互联网 评论
 
 
 
本文为【asp.net里导出excel表方法汇总】的拼音翻译版
  1、 you dataset sheng cheng
  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;
   // ding yi biao dui xiang yu xing dui xiang , tong shi yong DataSet dui qi zhi jin xing chu shi hua
   DataTable dt=ds.Tables[0];
   DataRow[] myRow=dt.Select("");
   // typeid=="1" shi dao chu wei EXCEL ge shi wen jian ;typeid=="2" shi dao chu wei XML ge shi wen jian
   if(typeid=="1")
   {
   // qu de shu ju biao ge lie biao ti , ge biao ti zhi jian yi "t fen ge , zui hou yi ge lie biao ti hou jia hui che fu
   for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+""t";
   colHeaders +=dt.Columns[i].Caption.ToString() +""n";
   // xiang HTTP shu chu liu zhong xie ru qu de de shu ju xin xi
   resp.Write(colHeaders);
   // zhu xing chu li shu ju
   foreach(DataRow row in myRow)
   {
   // zai dang qian xing zhong , zhu lie huo de shu ju , shu ju zhi jian yi "t fen ge , jie shu shi jia hui che fu "n
   for(i=0;i ls_item +=row[i].ToString() + ""t";
   ls_item += row[i].ToString() +""n";
   // dang qian xing shu ju xie ru HTTP shu chu liu , bing qie zhi kong ls_item yi bian xia xing shu ju
   resp.Write(ls_item);
   ls_item="";
   }
   }
   else
   {
   if(typeid=="2")
   {
   // cong DataSet zhong zhi jie dao chu XML shu ju bing qie xie dao HTTP shu chu liu zhong
   resp.Write(ds.GetXml());
   }
   }
   // xie huan chong qu zhong de shu ju dao HTTP tou wen jian zhong
   resp.End();
  }
  2、 you datagrid sheng cheng
  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();
  }
  yong fa :ToExcel(datagrid1);
  3、 zhe ge yong dataview
  public void OutputExcel(DataView dv,string str)
  {
   //
   // TODO: zai ci chu tian jia gou zao han shu luo ji
   //
   //dv wei yao shu chu dao Excel de shu ju ,str wei biao ti ming cheng
   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;
   //
   // qu de biao ti
   //
   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;// she zhi biao ti ge shi wei ju zhong dui qi
   }
   //
   // qu de biao ge zhong de shu ju
   //
   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;// she zhi ri qi xing de zi duan ge shi wei ju zhong dui qi
   }
   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;// she zhi zi fu xing de zi duan ge shi wei ju zhong dui qi
   }
   else
   {
   excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
   }
   }
   }
   //
   // jia zai yi ge he ji xing
   //
   int rowSum = rowIndex + 1;
   int colSum = 2;
   excel.Cells[rowSum,2] = " he ji ";
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
   //
   // she zhi xuan zhong de bu fen de yan se
   //
   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;// she zhi wei qian huang se , gong ji you 56 zhong
   //
   // qu de zheng ge bao biao de biao ti
   //
   excel.Cells[2,2] = str;
   //
   // she zhi zheng ge bao biao de biao ti ge shi
   //
   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;
   //
   // she zhi bao biao biao ge wei zui shi ying kuan du
   //
   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();
   //
   // she zhi zheng ge bao biao de biao ti wei kua lie ju zhong
   //
   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;
   //
   // hui zhi bian kuang
   //
   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;// she zhi zuo bian xian jia cu
   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;// she zhi shang bian xian jia cu
   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;// she zhi you bian xian jia cu
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;// she zhi xia bian xian jia cu
   //
   // xian shi xiao guo
   //
   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;
   // tian jia tou xin xi , wei " wen jian xia zai / ling cun wei " dui hua kuang zhi ding mo ren wen jian ming
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
   // tian jia tou xin xi , zhi ding wen jian da xiao , rang liu lan qi neng gou xian shi xia zai jin du
   Response.AddHeader("Content-Length", file.Length.ToString());
  
   // zhi ding fan hui de shi yi ge bu neng bei ke hu duan du qu de liu , bi xu bei xia zai
   Response.ContentType = "application/ms-excel";
  
   // ba wen jian liu fa song dao ke hu duan
   Response.WriteFile(file.FullName);
   // ting zhi ye mian de zhi xing
   Response.End();
  }【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

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

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

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

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

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

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

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

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

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

 
 
1、 you dataset sheng cheng 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; // ding yi biao dui xiang yu xing dui xiang , tong shi yong DataSet dui qi zhi jin xing chu shi hua DataTable dt=ds.Tables[0]; DataRow[] myRow=dt.Select(""); // typeid=="1" shi dao chu wei EXCEL ge shi wen jian ;typeid=="2" shi dao chu wei XML ge shi wen jian if(typeid=="1") { // qu de shu ju biao ge lie biao ti , ge biao ti zhi jian yi "t fen ge , zui hou yi ge lie biao ti hou jia hui che fu for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+""t"; colHeaders +=dt.Columns[i].Caption.ToString() +""n"; // xiang HTTP shu chu liu zhong xie ru qu de de shu ju xin xi resp.Write(colHeaders); // zhu xing chu li shu ju foreach(DataRow row in myRow) { // zai dang qian xing zhong , zhu lie huo de shu ju , shu ju zhi jian yi "t fen ge , jie shu shi jia hui che fu "n for(i=0;i ls_item +=row[i].ToString() + ""t"; ls_item += row[i].ToString() +""n"; // dang qian xing shu ju xie ru HTTP shu chu liu , bing qie zhi kong ls_item yi bian xia xing shu ju resp.Write(ls_item); ls_item=""; } } else { if(typeid=="2") { // cong DataSet zhong zhi jie dao chu XML shu ju bing qie xie dao HTTP shu chu liu zhong resp.Write(ds.GetXml()); } } // xie huan chong qu zhong de shu ju dao HTTP tou wen jian zhong resp.End(); } 2、 you datagrid sheng cheng 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(); } yong fa :ToExcel(datagrid1); 3、 zhe ge yong dataview public void OutputExcel(DataView dv,string str) { // // TODO: zai ci chu tian jia gou zao han shu luo ji // //dv wei yao shu chu dao Excel de shu ju ,str wei biao ti ming cheng 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; // // qu de biao ti // 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;// she zhi biao ti ge shi wei ju zhong dui qi } // // qu de biao ge zhong de shu ju // 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;// she zhi ri qi xing de zi duan ge shi wei ju zhong dui qi } 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;// she zhi zi fu xing de zi duan ge shi wei ju zhong dui qi } else { excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString(); } } } // // jia zai yi ge he ji xing // int rowSum = rowIndex + 1; int colSum = 2; excel.Cells[rowSum,2] = " he ji "; xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter; // // she zhi xuan zhong de bu fen de yan se // 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;// she zhi wei qian huang se , gong ji you 56 zhong // // qu de zheng ge bao biao de biao ti // excel.Cells[2,2] = str; // // she zhi zheng ge bao biao de biao ti ge shi // 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; // // she zhi bao biao biao ge wei zui shi ying kuan du // 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(); // // she zhi zheng ge bao biao de biao ti wei kua lie ju zhong // 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; // // hui zhi bian kuang // 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;// she zhi zuo bian xian jia cu xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;// she zhi shang bian xian jia cu xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;// she zhi you bian xian jia cu xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;// she zhi xia bian xian jia cu // // xian shi xiao guo // 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; // tian jia tou xin xi , wei " wen jian xia zai / ling cun wei " dui hua kuang zhi ding mo ren wen jian ming Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); // tian jia tou xin xi , zhi ding wen jian da xiao , rang liu lan qi neng gou xian shi xia zai jin du Response.AddHeader("Content-Length", file.Length.ToString()); // zhi ding fan hui de shi yi ge bu neng bei ke hu duan du qu de liu , bi xu bei xia zai Response.ContentType = "application/ms-excel"; // ba wen jian liu fa song dao ke hu duan Response.WriteFile(file.FullName); // ting zhi ye mian de zhi xing Response.End(); }
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
情人节的清纯女生(9)
情人节的清纯女生(8)
情人节的清纯女生(7)
情人节的清纯女生(6)
山东蓬莱海边组照
一探哲蚌 II
一探哲蚌 III
古长城的一角
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有