62,046
社区成员
发帖
与我相关
我的任务
分享
public void WriteDSToExcel(DataSet ds)
{
string strFile="";
string path="";
DataTable dt=ds.Tables[0];
//文件信息设置
strFile=strFile+"LogBackUp";
strFile=strFile+DateTime.Now.ToString("yyyyMMddhhmmss");
strFile=strFile+".xls";
path=Server.MapPath(strFile);
System.IO.FileStream fs=new FileStream(path,System.IO.FileMode.Create,System.IO.FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,new System.Text.UnicodeEncoding());
//画表头
for(int i=0;i<dt.Columns.Count;i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("\t");
}
sw.WriteLine("");
//画表体
for(int i=0;i<dt.Rows.Count;i++)
{
sw.Write(DelQuota(dt.Rows[i]["UserID"].ToString()));
sw.Write("\t");
sw.Write(DelQuota(dt.Rows[i]["UserName"].ToString()));
sw.Write("\t");
sw.Write(DelQuota(dt.Rows[i]["provinceID"].ToString()));
sw.Write("\t");
sw.Write(DelQuota(dt.Rows[i]["cityID"].ToString()));
sw.Write("\t");
sw.Write(DelQuota(dt.Rows[i]["areaID"].ToString()));
sw.Write("\t");
sw.Write(DelQuota(dt.Rows[i]["Enabled"].ToString()));
sw.WriteLine("");
}
sw.Flush();
sw.Close();
}
前台
EnableEventValidation="false"
#region 导出
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void BtnPrint_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.AfficheGV.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
#endregion