最近在做統計功能,要求統計結果(表格)既能查看(BS系統,在瀏覽器查看),又能輸出為excel文件。對於輸出excel文件,在網上找到n種方案,因為還需查看,最終選擇了統計結果輸出為table,查看時直接顯示table,輸出excel時把table寫進輸出流,ContentType設置為application/vnd.ms-excel再輸出,具體方法如下:
軟件環境:VS2008,C#,IE,office2010
1.輸出流內容的格式
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table>
......
</table>
</body>
</html>
2.Response的屬性設置
Response關鍵屬性有幾個
//設定編碼方式,若輸出的excel有亂碼,可優先從編碼方面解決
Response.ContentEncoding = System.Text.Encoding.UTF8;
//關閉ViewState,此屬性在Page中
EnableViewState = false;
//filenames是自定義的文件名
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filenames);
//content是步驟1的html,注意是string類型
Response.Write(content);
這樣已經能發布最基本的excel,下面說些細節
1.樣式最好用css,即設style屬性或class屬性,有些樣式屬性excel不認的。th、td的寬度最好在colgroup中設
2.在excel的格子中換行,可以用這個<br style='mso-data-placement:same-cell;'/>
3.關於邊框寬度的問題,如果你的table是全邊框,可以設置table的border屬性,其中0=不顯示邊框。
如果表的格式比較復雜,特別是表頭,有些邊要隱藏的,這個要先把table的border=0,之后對每個th td用css的邊框樣式進行設置,但這里有個地方要注意,就是寬度,設為0.5pt(例:border-left: 0.5pt solid #000;),如果設為1px輸出的邊框會很粗。
4.excel空余部分邊框的問題。用此方法輸出的excel,空余部分邊框都不顯示,如果要做成想普通excel的樣子,需在輸出流的head部分加上excel的設置,例子如下:
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name></x:Name>
<x:WorksheetOptions>
<x:Selected/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]-->
5. 應用中經常會遇到要從系統或數據庫中導出數據平面文件,一般是導出到txt,csv或excel。txt和csv一般用在系統間的數據交換,而 excel一般有較好的顯示效果,可以按照一定的模板導出,導出就不用再排版了,使用簡單,如果是使用做報表一般都導出excel文件。
但是使用com組件導出到Excel數據很慢,有另一種生成excel文件的方式就是通過html和css快速導出數據同時並能設置樣式,使用這種方式有兩個優點:1是速度快,2是不需安裝excel支持。
當使用這種方法導出文件時,有時會遇到一個問題,就是導出的Excel經常會把我們的數據自動識別為其他格式,例如只有純數字的字段在導出到 Excel后會被自動識別為數字格式,而一旦數字超過11位,Excel便會將其以科學計數法的形式來顯示,比如身份證號碼,帶區號的電話號碼等。
解決方法有多種,這里只介紹一種本人認為最好的一種,即使用CSS給出現問題的表格字段(如<TD>)應用mso-number- format屬性,用這個屬性指定某單元格的數據格式,避免Excel自動轉換格式。mso-number-format是Office提供的格式, 如果您將office文檔轉成HTML就會有這樣的標志。MSO表示Microsoft Office。
示例:
<style type="text/css">
.format{
mso-number-format:'\@';
}
</style>
<td class="format">123456789012345</td>
在css中加入:mso-number-format定義數據格式,格式可以在excel中查看自定義格式,具體可以參考一下:
mso-number-format:"0" NO Decimals
mso-number-format:"0\.000" 3 Decimals
mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec
mso-number-format:"mm\/dd\/yy" Date7
mso-number-format:"mmmm\ d\,\ yyyy" Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM
mso-number-format:"Short Date" 01/03/1998
mso-number-format:"Medium Date" 01-mar-98
mso-number-format:"d\-mmm\-yyyy" 01-mar-1998
mso-number-format:"Short Time" 5:16
mso-number-format:"Medium Time" 5:16 am
mso-number-format:"Long Time" 5:16:21:00
mso-number-format:"Percent" Percent - two decimals
mso-number-format:"0%" Percent - no decimals
mso-number-format:"0\.E+00" Scientific Notation
mso-number-format:"\@" Text
mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943)
最后附上一個成果作為例子,擴展名為xls可用excel打開, 在文末
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
<style type="text/css">
.td
{
width: 84px;
}
.gdtjContainer .tb tr
{
text-align: center;
vertical-align: middle;
}
.gdtjContainer .tb th
{
border-left: 0.5pt solid #000;
border-bottom: 0.5pt solid #000;
text-align: center;
font-weight: normal;
font-size: 10pt;
middle: ;;height:30px;}
.gdtjContainer .header th
{
font-size: 12pt;
}
.gdtjContainer .tb tr th.noleftborder
{
border-left: none;
}
.gdtjContainer .tb tr th.rightborder
{
border-right: 0.5pt solid #000;
}
</style>
</head>
<body>
<div class="gdtjContainer">
<table class="tb" cellspacing="0" cellpadding="0" border="0" width="2184px">
<colgroup>
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
<col class="td" />
</colgroup>
<tr style="height: 40px">
<th style="font-size: 20pt; font-family: 宋體; border: none;" colspan="26">
2011年增城市單位土地使用權出讓情況登記表(統計時間從2011-06-29至2011-06-30)
</th>
</tr>
<tr>
<th colspan="23" style="border-left: none;">
</th>
<th style="text-align: left; font-size: 12pt; border-left: none;" colspan="3">
單位:萬元、平方米
</th>
</tr>
<tr class="header">
<th rowspan="2">
合同編號
</th>
<th colspan="2" rowspan="2">
用地單位
</th>
<th colspan="2" rowspan="2">
土地座落
</th>
<th rowspan="2">
供地面積
</th>
<th style="border-left: none">
</th>
<th>
</th>
<th rowspan="2">
用途
</th>
<th colspan="3" rowspan="1">
出讓金
</th>
<th rowspan="2">
容積率
</th>
<th rowspan="2">
建築密度
</th>
<th rowspan="2">
綠地率
</th>
<th rowspan="2">
規划建築面積
</th>
<th rowspan="2">
出讓方式
</th>
<th rowspan="2">
審批日期
</th>
<th rowspan="2">
合同簽訂日期
</th>
<th rowspan="2">
動工期限
</th>
<th rowspan="2">
竣工日期
</th>
<th rowspan="2">
批次情況
</th>
<th rowspan="2">
合同約定繳費期限
</th>
<th rowspan="2">
繳費情況
</th>
<th rowspan="2">
滯納金
</th>
<th rowspan="2" class="rightborder">
備注
</th>
</tr>
<tr style="height: 40px" class="header">
<th>
新增面積
</th>
<th style="font-size: 10pt;">
保障性住房用地占用面積
</th>
<th>
應繳
</th>
<th>
已繳
</th>
<th>
未繳
</th>
</tr>
<tr>
<th>
440183-2011-
</th>
<th colspan="2">
45654
</th>
<th colspan="2">
</th>
<th>
1110000
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
111
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
拍賣出讓
</th>
<th>
</th>
<th>
2011-06-29
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th class="rightborder">
</th>
</tr>
<tr>
<th>
</th>
<th colspan="2">
合計
</th>
<th colspan="2">
</th>
<th>
1110000
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
111
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th class="rightborder">
</th>
</tr>
</table>
</div>
</body>
</html>