一说到导出excel可能很多人都会觉得说直接利用npoi 导入导出excel格式的文件,那样非常方便,但是可能有些时候有的浏览器不支持,那么该怎么办呢,现在介绍一种纯C#的导出excel的方法,代码如下:

 public void excel_output1(DataTable dt)
{
Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8 ";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter(); sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine(" <Created>1996-12-17T01:32:42Z</Created>");
sw.WriteLine(" <LastSaved>2011-05-29T05:59:50Z</LastSaved>");
sw.WriteLine(" <Version>11.6360</Version>");
sw.WriteLine(" </DocumentProperties>");
sw.WriteLine(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine(" <RemovePersonalInformation/>");
sw.WriteLine(" </OfficeDocumentSettings>");
sw.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <WindowHeight>4530</WindowHeight>");
sw.WriteLine(" <WindowWidth>8505</WindowWidth>");
sw.WriteLine(" <WindowTopX>480</WindowTopX>");
sw.WriteLine(" <WindowTopY>120</WindowTopY>");
sw.WriteLine(" <AcceptLabelsInFormulas/>");
sw.WriteLine(" <ProtectStructure>False</ProtectStructure>");
sw.WriteLine(" <ProtectWindows>False</ProtectWindows>");
sw.WriteLine(" </ExcelWorkbook>");
sw.WriteLine(" <Styles>");
sw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sw.WriteLine(" <Alignment ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders/>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>");
sw.WriteLine(" <Interior/>");
sw.WriteLine(" <NumberFormat/>");
sw.WriteLine(" <Protection/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s28\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s29\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s31\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"24\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s32\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s33\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" </Styles>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet1\">");
//sw.WriteLine(" <Table ss:ExpandedColumnCount=\"9\" ss:ExpandedRowCount=\"9\" x:FullColumns=\"1\""); string dt_str = "<Table ss:ExpandedColumnCount=\"13\" ss:ExpandedRowCount=\"";
int dt_rCount = dt.Rows.Count + ;
dt_str += dt_rCount.ToString() + "\" x:FullColumns=\"1\"";
sw.WriteLine(dt_str); sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">"); sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"93.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"111.75\" ss:Span=\"1\"/>");
sw.WriteLine(" <Column ss:Index=\"4\" ss:AutoFitWidth=\"0\" ss:Width=\"102\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"135.75\" ss:Span=\"3\"/>");
sw.WriteLine(" <Column ss:Index=\"9\" ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"163.75\"/>");
sw.WriteLine(" <Row ss:Index=\"2\">");
sw.WriteLine(" <Cell ss:MergeAcross=\"12\" ss:MergeDown=\"2\" ss:StyleID=\"s31\"><Data");
sw.WriteLine(" ss:Type=\"String\">回访详单</Data></Cell>");
sw.WriteLine(" </Row>");
sw.WriteLine(" <Row ss:Index=\"5\">");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">营业区</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">任务总数</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">明确表示不使用</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">迁移无线</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">改网通</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">改电信</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">联系不上的1个月以上无上网记录</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">联系不上有上网记录</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对网络不满意</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对服务不满意</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">考虑中</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">已续费</Data></Cell>");
sw.WriteLine(" </Row>"); int cs_i = ;
DataRow[] dt_myRow = dt.Select(""); foreach (DataRow dt_row in dt_myRow)
{
sw.WriteLine(" <Row>"); sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
//sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[12].ToString() + "</Data></Cell>");
sw.WriteLine(" </Row>");
cs_i++;
} //sw.WriteLine(" <Row>");
//sw.WriteLine(" <Cell ss:MergeAcross=\"5\" ss:StyleID=\"s33\"><Data ss:Type=\"String\">合计</Data></Cell>");
//sw.WriteLine(" <Cell ss:StyleID=\"s32\" ss:Formula=\"=SUM(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),6-ROW(),0,ROW()-6,1))\"><Data ss:Type=\"Number\">1</Data></Cell>");
//sw.WriteLine(" <Cell ss:MergeAcross=\"1\" ss:StyleID=\"s32\" ><Data ss:Type=\"Number\"></Data></Cell>");
//sw.WriteLine(" </Row>");
sw.WriteLine(" </Table>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <Print>");
sw.WriteLine(" <ValidPrinterInfo/>");
sw.WriteLine(" <PaperSizeIndex>9</PaperSizeIndex>");
sw.WriteLine(" <HorizontalResolution>600</HorizontalResolution>");
sw.WriteLine(" <VerticalResolution>600</VerticalResolution>");
sw.WriteLine(" </Print>");
sw.WriteLine(" <Selected/>");
sw.WriteLine(" <Panes>");
sw.WriteLine(" <Pane>");
sw.WriteLine(" <Number>3</Number>");
sw.WriteLine(" <ActiveRow>8</ActiveRow>");
sw.WriteLine(" <ActiveCol>12</ActiveCol>");
sw.WriteLine(" </Pane>");
sw.WriteLine(" </Panes>");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet2\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\"");
sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet3\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\"");
sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine("</Workbook>"); Response.Write(sw);
Response.End(); }

C#关于导出excel的方法的更多相关文章

  1. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  2. ASP.NET导出excel表方法汇总

    asp.net里导出excel表方法汇总  1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...

  3. 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法

    在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...

  4. 懒人小工具:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法

    之前写了篇文章,懒人小工具:[自动生成Model,Insert,Select,Delete以及导出Excel的方法](http://www.jianshu.com/p/d5b11589174a),但是 ...

  5. 懒人小工具:T4生成实体类Model,Insert,Select,Delete以及导出Excel的方法

    由于最近公司在用webform开发ERP,用到大量重复机械的代码,之前写了篇文章,懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法,但是有人觉得这种方法 ...

  6. 懒人小工具1:winform自动生成Model,Insert,Select,Delete以及导出Excel的方法

       懒人小工具2:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法    github地址:https://github.com/Jimmey-Jiang/J ...

  7. java根据xml配置文件导出excel通用方法

    java web项目中时常会用到导出功能,而导出excel几乎是每个项目必备的功能之一.针对形形色色的导出方法及个人平时的工作经验,特将导出excel方法整理成通用的方法,根据xml配置来实现特定的导 ...

  8. 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)

    假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的 ...

  9. 导出excel 的方法及示例

    一.基本知识 1.Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 2. HSSF 是Horribl ...

随机推荐

  1. c# signalr聊天室开源资料

    SignalR+LayIM源码: http://www.cnblogs.com/panzi/p/5742089.html 钉钉客户端源码: http://www.cnblogs.com/loveson ...

  2. MySql 首记

    1.连接数据: 格式是:  -P 端口号  -h  mysql主机名\ip -u root (用户)  -p 2.显示数据库: show databases; 3.选择数据库: use databas ...

  3. background-clip,background-origin

    开门见山,简单来说 background-origin定义了background的绘制区域(就是从什么地方开始绘制),三个属性 padding-box 背景图像相对于内边距框来绘制border-box ...

  4. windows磁盘分区

    windows 下对磁盘进行分区吗,如何调整大小. N的输入单位为GB,输出单位为MB; (N-1)4+1024N;

  5. ubuntu 15.04 联网提示:wifi已通过硬件开关禁用

    执行以下命令: echo "options asus_nb_wmi wapf=4"| sudo tee /etc/modprobe.d/asus_nb_wmi.conf 重启

  6. 分享大家一个背景为下雪的JQuery

    <html><head> <meta charset="utf-8"> <meta content="IE=edge,chrom ...

  7. WIN7远程桌面连接方法!

    WIN7远程桌面连接方法!

  8. 谷歌大神Jeff Dean:大规模深度学习最新进展 zz

    http://www.tuicool.com/articles/MBBbeeQ 在AlphaGo与李世石比赛期间,谷歌天才工程师Jeff Dean在Google Campus汉城校区做了一次关于智能计 ...

  9. [已解决] windows 80端口被占用

    看下是不是IIS开着,如果是关掉就OK拉 原文地址:http://www.cnblogs.com/gifisan/p/5822156.html

  10. android之电话拨号器

    在android入门的案例中,除了HelloWorld这个经典案例,还有一个电话拨号器需要掌握,现在我就来个电话拨号器的示范,毕竟大牛也是从菜鸟进化而来的. 首先你应该知道自己要设置怎样的UI,然后创 ...