cnblogs/dcrenl/.net DataSet 导出到Excel.html
2024-09-24 12:43:01 +08:00

67 lines
5.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="sina_keyword_ad_area2" class="articalContent ">
<div>public void CreateExcel(DataSet ds, string typeid, stringFileName)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;HttpResponse resp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; resp =Page.Response;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;resp.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;resp.AppendHeader("Content-Disposition", "attachment;filename=" +FileName);</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; stringcolHeaders = "", ls_item = "";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; int i =0;</div>
<div><br />
</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;//定义表对象和行对像同时用DataSet对其值进行初始化&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; DataTabledt = ds.Tables[0];</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; DataRow[]myRow = dt.Select("");</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //typeid=="1"时导出为EXCEL格式文档typeid=="2"时导出为XML格式文档&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; if (typeid== "1")</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;//取得数据表各列标题,各标题之间以\t分割最后一个列标题后加回车符&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; for (i = 0; i&lt; dt.Columns.Count; i++)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if (i == dt.Columns.Count - 1)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; colHeaders+= dt.Columns[i].Caption.ToString() + "\n";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; colHeaders+= dt.Columns[i].Caption.ToString() + "\t";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;//向HTTP输出流中写入取得的数据信息&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; resp.Write(colHeaders);</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;//逐行处理数据&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; foreach (DataRow row inmyRow)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;//在当前行中,逐列获得数据,数据之间以\t分割结束时加回车符\n&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; for (i = 0; i &lt; dt.Columns.Count;i++)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; {</div>
<div><br />
</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; if (i ==dt.Columns.Count - 1)</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; ls_item += row[i].ToString()+ "\n";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; else</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; ls_item += row[i].ToString()+ "\t";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;//当前行数据写入HTTP输出流并且置空ls_item以便下行数据&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; resp.Write(ls_item);</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; ls_item = "";</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; else</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; if (typeid == "2")</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;//从DataSet中直接导出XML数据并且写到HTTP输出流中&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; resp.Write(ds.GetXml());</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;//写缓冲区中的数据到HTTP头文档中&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;resp.End();</div>
<div>&nbsp; &nbsp; &nbsp;&nbsp; }&nbsp;</div>
</div>
<p>&nbsp;</p>