//excel输出
protected void excel_ServerClick3(object sender, EventArgs e)
{
bindgrid(); //先执行一次
dg.PageSize = count + 1;
bindgrid(); //再执行一次
dg.Columns[0].Visible = false;
string t2 = "";
t2 = "<table><tr><td colspan='2' align='center'>成品库存月报</table></tr></td>";
XLFZ.ExcelPrt ExPrt = new XLFZ.ExcelPrt();
//调用转换方法
ExPrt.ToExcelTitle(dg, "出口大类省市统计", t2, MyConnection);
dg.PageSize = 1000;
return;
}
把数据转成excel格式的方法
string strConn = ConfigurationManager.AppSettings["XLFZConnection"];
MyConnection = new SqlConnection(strConn);
string prtrq = "";FileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileName));
//设置网络输出流的HTTP字符集为UTF-8,Current为当前 HTTP 请求获取 HttpContext 对象
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
//设置输出流HTTPMIME类型为excel
HttpContext.Current.Response.ContentType = "application/ms-excel";
//将HTTP头添加到输出流
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
//不保存该控件的视图状态
ctl.Page.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
//将文本写入到输出流
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
//将服务器控件的内容输出到HtmlTextWriter对象中
//ctl.RenderControl(hw);
//StringWriter.ToString返回包含迄今为止写入到当前 StringWriter 中的字符的字符串
HttpContext.Current.Response.Write("<meta http-equiv='Content-Type' charset='gb2312'>");
HttpContext.Current.Response.Write(TitleName.ToString());
//HttpContext.Current.Response.Write(sw.ToString());
//获取dataset数据
DataView dt = (DataView)Session["ypxsd"];