這幾天要從數據庫導出EXCEL表格,就找了N鐘方法,經測試,下面的方法比較的好用一點。都是經過輸入DataTable而導出的。不過導出的EXCEL都不是正規的EXCEL格式,只能說是HTML格式的,導出的再導入進數據庫就會出現問題了。想導入的話用EXCEL打開另存為EXCEL格式就好了
1.利用DataRow直接輸出,經測試,沒有亂碼。
Code
public bool LendOutExcel(string strFileName, DataTable DT)
{
try
{
//清除Response緩存內容
HttpContext.Current.Response.Clear();
//緩存輸出,並在完成整個響應之後將其發送
HttpContext.Current.Response.Buffer = true;
//strFileName指定輸出文件的名稱,注意其擴展名和指定文件類型相符,可以為:.doc .xls .txt .htm
strFileName = strFileName + ".xls";
//設置輸出流的HTPP字符集,確定字符的編碼格式
//HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//下面這行很重要, attachment 參數表示作為附件下載,您可以改成 online在線打開
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName));
//Response.ContentType指定文件類型.可以為application/ms-excel,application/ms-word,application/ms-txt,application/ms-html或其他浏覽器可直接支持文檔
HttpContext.Current.Response.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";
int i = 0;
//定義表對象與行對像,同時用DataSet對其值進行初始化
DataRow[] myRow = DT.Select("");
//取得數據表各列標題,各標題之間以\t分割,最後一個列標題後加回車符
for (i = 0; i < DT.Columns.Count - 1; i++)
{
colHeaders += DT.Columns[i].Caption.ToString() + "\t";
}
colHeaders += DT.Columns[i].Caption.ToString() + "\n";
//向HTTP輸出流中寫入取得的數據信息
HttpContext.Current.Response.Write(colHeaders);
//逐行處理數據
foreach (DataRow row in myRow)
{
//在當前行中,逐列獲得數據,數據之間以\t分割,結束時加回車符\n
for (i = 0; i < DT.Columns.Count - 1; i++)
ls_item += row[i].ToString() + "\t";
ls_item += row[i].ToString() + "\n";
//當前行數據寫入HTTP輸出流,並且置空ls_item以便下行數據
HttpContext.Current.Response.Write(ls_item);
ls_item = "";
}
//寫緩沖區中的數據到HTTP頭文件中
HttpContext.Current.Response.End();
return true;
}
catch
{
return false;
}
}
輸出為:學院活動表.xls
活動名稱 活動時間 活動地點 發起人 承擔人 批准人 活動概況 參與人 備注
第5次9 2008-7-27 0:00:00 HBU_ 1 shenxian_ 1 jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次8 2008-7-27 0:00:00 HBU_ 2 shenxian_ 2 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次7 2008-7-27 0:00:00 HBU_ 3 shenxian_ 3 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次6 2008-7-27 0:00:00 HBU_ 4 shenxian_ 4 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次5 2008-7-27 0:00:00 HBU_ 5 shenxian_ 5 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次9 2008-7-27 0:00:00 HBU_ 1 shenxian_ 1 jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次8 2008-7-27 0:00:00 HBU_ 2 shenxian_ 2 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次7 2008-7-27 0:00:00 HBU_ 3 shenxian_ 3 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次6 2008-7-27 0:00:00 HBU_ 4 shenxian_ 4 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次5 2008-7-27 0:00:00 HBU_ 5 shenxian_ 5 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi