码迷,mamicode.com
首页 > 其他好文 > 详细

直接把页面的table导出到excel表中

时间:2015-08-03 18:28:22      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

前台HTML

技术分享
  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReLoadLogin.aspx.cs" Inherits="ReLoadLogin" %>
  2 
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 <html xmlns="http://www.w3.org/1999/xhtml">
  5 <head>
  6     <meta charset="utf-8" />
  7     <title>导出excel</title>
  8     <script type="text/javascript">
  9     function loadShuJu(){
 10         $(".hidValue").val($(".ta").html());//获取ta值并赋值给hidValue
 11     }
 12 </script>
 13 
 14 </head>
 15 <body class="login-layout">
 16     <form id="Form1" runat="server">
 17 <input type="hidden" id="hidValue" class="hidValue" runat="server" />
 18 <asp:Button ID="btnDaoChu" runat="server" cssclass="btn btn-sm btn-info "   Text=" 导出 " onclick="btnDaoChu_Click"  />
 19 <hr />
 20 <table id="ta" class="ta" runat="server">
 21             <tr name="nf">
 22                 <td style=" width:100px">地区</td>
 23                 <td style=" width:200px" colspan="3">项目</td>
 24             </tr>
 25             <tr name="gqmj">
 26                 <td rowspan="51">德阳(旌阳区、广汉市、中江县…)</td>
 27                 <td colspan="3">供区面积</td>
 28                 
 29             </tr>
 30             <tr name="gdp">
 31                 <td colspan="3">GDP(亿元)</td>
 32                 
 33             </tr>
 34             <tr name="rk">
 35                 <td colspan="3">人口(万人)</td>
 36                 
 37             </tr>
 38             <tr name="hs">
 39                 <td colspan="3">户数(万户)</td>
 40                 
 41             </tr>
 42             <tr name="dyzj">
 43                 <td colspan="3">电源装机(万kW)</td>
 44                 
 45             </tr>
 46             <tr name="zhdyhglc">
 47                 <td colspan="3">综合电压合格率(城网)(%)</td>
 48                 
 49             </tr>
 50             <tr name="zhdyhgln">
 51                 <td colspan="3">综合电压合格率(农网)(%)</td>
 52                 
 53             </tr>
 54             <tr name="gdkklc">
 55                 <td colspan="3">供电可靠率(RS-3)(城网)(%)</td>
 56                 
 57             </tr>
 58             <tr name="gdkkln">
 59                 <td colspan="3">供电可靠率(RS-3)(农网)(%)</td>
 60                 
 61             </tr>
 62             <tr name="qshzdydfh">
 63                 <td colspan="3">全社会最大用电负荷(万kWh)</td>
 64                 
 65             </tr>
 66             <tr name="qshydl">
 67                 <td rowspan="5">电量</td>
 68                 <td colspan="2">全社会用电量(亿kWh)</td>
 69                 
 70             </tr>
 71             <tr name="sdl">
 72                 <td colspan="2">售电量(万kWh)</td>
 73                 
 74             </tr>
 75             <tr name="gdl">
 76                 <td colspan="2">购电量(万kWh)</td>
 77                 
 78             </tr>
 79             <tr name="gwdl">
 80                 <td colspan="2">购网电量(万kWh)</td>
 81                 
 82             </tr>
 83             <tr name="gdfdl">
 84                 <td colspan="2">购地方电量(万kWh)</td>
 85                 
 86             </tr>
 87             <tr name="gdzctz">
 88                 <td colspan="3">固定资产投资(万kWh)</td>
 89                 
 90             </tr>
 91             <tr name="dwtz">
 92                 <td>其中</td>
 93                 <td colspan="2">电网投资(亿元)</td>
 94                 
 95             </tr>
 96             <tr name="zhxsl110">
 97                 <td colspan="3">110千伏及以下综合线损率(%)</td>
 98                 
 99             </tr>
100             <tr name="zhxsl10">
101                 <td colspan="3">10千伏及以下综合线损率(%)</td>
102                 
103             </tr>
104             <tr name="zbdz">
105                 <td rowspan="8">变电站</td>
106                 <td rowspan="2">合计</td>
107                 <td>变电站座数</td>
108                 
109             </tr>
110             <tr name="zbdrl">
111                 <td>变电容量</td>
112                 
113             </tr>
114             <tr name="bdz220">
115                 <td rowspan="2">220kV</td>
116                 <td>变电站座数</td>
117                 
118             </tr>
119             <tr name="bdzrl220">
120                 <td>变电容量</td>
121                 
122             </tr>
123             <tr name="bdz110">
124                 <td rowspan="2">110kV</td>
125                 <td>变电站座数</td>
126                 
127             </tr>
128             <tr name="bdzrl110">
129                 <td>变电容量</td>
130                 
131                 
132             </tr>
133             <tr name="bdz35">
134                 <td rowspan="2">35kV</td>
135                 <td>变电站座数</td>
136                 
137             </tr>
138             <tr name="bdzrl35">
139                 <td>变电容量</td>
140                 
141             </tr>
142             <tr name="hwgsl">
143                 <td colspan="3">10kV环网柜数量</td>
144                 
145             </tr>
146             <tr name="kgsl">
147                 <td colspan="3">10kV开关数量</td>
148                 
149             </tr>
150             <tr name="zgxpjcd">
151                 <td colspan="3">10千伏主干线平均长度(km)</td>
152                 
153             </tr>
154             <tr name="xlts10">
155                 <td colspan="3">10kV线路条数</td>
156                 
157             </tr>
158             <tr name="cw">
159                 <td rowspan="6">其中</td>
160                 <td colspan="2">城网</td>
161                 
162             </tr>
163             <tr name="cwgy">
164                 <td rowspan="2">其中</td>
165                 <td>公用</td>
166                 
167             </tr>
168             <tr name="cwzx">
169                 <td>专线</td>
170                 
171             </tr>
172             <tr name="nw">
173                 <td colspan="2">农网</td>
174                 
175             </tr>
176             <tr name="nwgy">
177                 <td rowspan="2">其中</td>
178                 <td>公用</td>
179                 
180             </tr>
181             <tr name="nwzx">
182                 <td>专线</td>
183                 
184             </tr>
185             <tr name="hll">
186                 <td colspan="3">10kV互联率(%)</td>
187                 
188             </tr>
189             <tr name="cwhll">
190                 <td rowspan="2">其中</td>
191                 <td colspan="2">城网</td>
192                 
193             </tr>
194             <tr name="nwhll">
195                 <td colspan="2">农网</td>
196                 
197             </tr>
198             <tr name="n_1tgl">
199                 <td colspan="3">N-1通过率(%)</td>
200                 
201             </tr>
202             <tr name="n_1tglcw">
203                 <td rowspan="2">其中</td>
204                 <td colspan="2">城网</td>
205                 
206             </tr>
207             <tr name="n_1tglnw">
208                 <td colspan="2">农网</td>
209                 
210             </tr>
211             <tr name="jkjyhl">
212                 <td colspan="3">10千伏架空绝缘化率(%)</td>
213                 
214             </tr>
215             <tr name="jkjyhlcw">
216                 <td rowspan="2">其中</td>
217                 <td colspan="2">城网</td>
218                 
219             </tr>
220             <tr name="jkjyhlnw">
221                 <td colspan="2">农网</td>
222                 
223             </tr>
224             <tr name="dlhl">
225                 <td colspan="3">10千伏电缆化率(%)</td>
226                 
227             </tr>
228             <tr name="dlhlcw">
229                 <td rowspan="2">其中</td>
230                 <td colspan="2">城网</td>
231                 
232             </tr>
233             <tr name="dlhlnw">
234                 <td colspan="2">农网</td>
235                 
236             </tr>
237         </table>
238     </form>
239 </body>
240 </html>
241     
View Code

后台代码:

技术分享
//获取table值
private string getExcelContent()
    {
        string v = hidValue.Value;
        StringBuilder sb = new StringBuilder();
        sb.Append("<table borderColor=‘black‘ border=‘1‘ >");
        sb.Append(""+v.ToString());
        sb.Append("</table>");
        return sb.ToString();
    } 
获取table值
技术分享
 1 public class CommonTool
 2     {
 3         /// <summary> 
 4         /// 以流的形式,可以设置很丰富复杂的样式 
 5         /// </summary> 
 6         /// <param name="content">Excel中内容(Table格式)</param> 
 7         /// <param name="filename">文件名</param> 
 8         /// <param name="cssText">样式内容</param> 
 9         public static void ExportToExcel(string filename, string content, string cssText)
10         {
11             var res = HttpContext.Current.Response;
12             content = String.Format("<style type=‘text/css‘>{0}</style>{1}", cssText, content);
13 
14             res.Clear();
15             res.Buffer = true;
16             res.Charset = "UTF-8";
17             res.AddHeader("Content-Disposition", "attachment; filename=" + filename);
18             res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
19             res.ContentType = "application/ms-excel;charset=UTF-8";
20             res.Write(content);
21             res.Flush();
22             res.End();
23         }
24     }
导出操作
技术分享
protected void btnDaoChu_Click(object sender, EventArgs e)
    {
        string content = getExcelContent();//获取html
        string css = ".firstTR td{color:blue;width:100px;}.secondTR td{color:blue;width:100px;}";//样式
        string filename = "Test.xls";//名称

        CommonTool.ExportToExcel(filename, content, css);
}
View Code

 

直接把页面的table导出到excel表中

标签:

原文地址:http://www.cnblogs.com/-040506/p/4699900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!