【转】excel表格导出集锦repeater实用,和普通用法
【http://www.cnblogs.com/s-y-l/archive/2011/12/23/2299008.html】
下面導(dǎo)出excel主要解決repeater產(chǎn)生亂碼的問題
publicvoid?CreateExcel(DataSet ds,string?typeid,string?FileName)?
??????? {?
??????????? HttpResponse resp;?
??????????? resp?=?Page.Response;?
??????????? resp.ContentEncoding?=?System.Text.Encoding.GetEncoding("GB2312");?
??????????? resp.AppendHeader("Content-Disposition",?"attachment;filename="+?FileName);?
????????????string?colHeaders="", ls_item="";?
????????????int?i=0;?
????????????//定義表對(duì)象和行對(duì)像,同時(shí)用DataSet對(duì)其值進(jìn)行初始化?
??????????? DataTable dt=ds.Tables[0];?
??????????? DataRow[] myRow=dt.Select("");?
????????????//?typeid=="1"時(shí)導(dǎo)出為EXCEL格式文檔;typeid=="2"時(shí)導(dǎo)出為XML格式文檔?
????????????if(typeid=="1")?
??????????? {?
????????????????//取得數(shù)據(jù)表各列標(biāo)題,各標(biāo)題之間以\t分割,最后一個(gè)列標(biāo)題后加回車符?
????????????????for(i=0;i<dt.Columns.Count;i++)
??????????????? {????????????????????
????????????????????if(i==dt.Columns.Count-1)
??????????????????? {
??????????????????????? colHeaders?+=dt.Columns[i].Caption.ToString()?+"\n";????
??????????????????? }
????????????????????else
??????????????????? {
??????????????????????? colHeaders+=dt.Columns[i].Caption.ToString()+"\t";?
??????????????????? }
??????????????? }
????????????????//向HTTP輸出流中寫入取得的數(shù)據(jù)信息?
??????????????? resp.Write(colHeaders);?
????????????????//逐行處理數(shù)據(jù)?
????????????????foreach(DataRow row?in?myRow)?
??????????????? {?
????????????????????//在當(dāng)前行中,逐列獲得數(shù)據(jù),數(shù)據(jù)之間以\t分割,結(jié)束時(shí)加回車符\n?
????????????????????for(i=0;i<dt.Columns.Count;i++) {?if(i==dt.Columns.Count-1)
??????????????????????? {
??????????????????????????? ls_item?+=?row[i].ToString()?+"\n";?
??????????????????????? }
????????????????????????else
??????????????????????? {
??????????????????????????? ls_item?+=row[i].ToString()?+"\t";??
??????????????????????? }
??????????????????? }
????????????????????//當(dāng)前行數(shù)據(jù)寫入HTTP輸出流,并且置空ls_item以便下行數(shù)據(jù)?
??????????????????? resp.Write(ls_item);?
??????????????????? ls_item="";?
??????????????? }?
??????????? }?
????????????else?
??????????? {?
????????????????if(typeid=="2")?
??????????????? {?
????????????????????//從DataSet中直接導(dǎo)出XML數(shù)據(jù)并且寫到HTTP輸出流中?
??????????????????? resp.Write(ds.GetXml());?
??????????????? }?
??????????? }?
????????????//寫緩沖區(qū)中的數(shù)據(jù)到HTTP頭文檔中?
??????????? resp.End();?
??????? }
下面是普通用法
?Response.Clear();
??????????? Response.Buffer = true;
??????????? Response.Charset = "utf-8";
??????????? Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("名稱", System.Text.Encoding.UTF8) + ".xls");
??????????? Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //設(shè)置輸出流為簡(jiǎn)體中文
??????????? Response.ContentType = "application/ms-excel"; //設(shè)置輸出文件類型為excel文件。?
??????????? this.EnableViewState = false;
??????????? System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
??????????? System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
??????????? System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
??????????? // repeater控件的ID
??????????? this.Repeater1.RenderControl(oHtmlTextWriter);
??????????? Response.Write(oStringWriter.ToString());
??????????? Response.End();
導(dǎo)出另類樣式的excel表
?
? //ExcelExporter.CreateExcel((DataSet)Session["dataset"], DateTime.Now.ToString() + ".xls", "application/ms-excel", this.Page);
??????????? // ExcelExporter.CreateExcelO((DataSet)Session["dataset"], DateTime.Now.ToString() + ".xls", "application/ms-excel", this.Page);
??????????? Response.Clear();
??????????? Response.Buffer = true;
??????????? //Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString()+".xls");
??????????? // Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString() + ".xls");
?
??????????? Response.Charset = "utf-8";
??????????? Response.ContentEncoding = System.Text.Encoding.UTF8;
??????????? string names = DateTime.Now.ToString() + ".xls";
??????????? //Response.Write(names);
??????????? Response.AddHeader("content-disposition", "attachment;filename=" + names);
?
??????????? Response.ContentType = "application/vnd.ms-excel";
?
??????????? //System.IO.StreamWriter write = new System.IO.StreamWriter();
??????????? System.IO.StringWriter write = new System.IO.StringWriter();
??????????? System.Web.UI.HtmlTextWriter html = new HtmlTextWriter(write);
??????????? Repeater1.RenderControl(html);
??????????? Response.Write(write.ToString());
??????????? Response.Flush();
? ? ? ? ? ? Response.End();?
轉(zhuǎn)載于:https://www.cnblogs.com/swjm119/archive/2011/12/23/2299433.html
總結(jié)
以上是生活随笔為你收集整理的【转】excel表格导出集锦repeater实用,和普通用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。