調用此方法時,整個html設計頁面隨着數據移植到excel。 我只想導出數據,不應該使用html設計。我無法找到解決方案,請有人幫助解決。將DataTable導出到ASP.NET中的
public void ExportToExcel(DataTable dtExportData)
{
string strReportName = string.Empty;
//StringWriter oStringWriter = null;
//Html32TextWriter oHtmlTextWriter = null;
//TableOneDayPassReport.Visible = true;
ExcelGrid.EnableViewState = false;
ExcelGrid.DataSource = dtExportData;
ExcelGrid.DataBind();
Response.Clear();
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.AddHeader("content-disposition", "attachment;filename=OwnTransportDetails.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//TableOneDayPassReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
//Response.End();
// strReportName = dtExportData.TableName;
// Report_Month.InnerHtml = "<b>" + strReportName + " Report</b><br />";
// ReportHead.Attributes.Add("style", "DISPLAY: block; VISIBILITY: visible");
// TableOneDayPassReport.Visible = true;
// //oStringWriter = new StringWriter();
// //oHtmlTextWriter = new Html32TextWriter(oStringWriter);
// ExcelGrid.EnableViewState = false;
// ExcelGrid.DataSource = dtExportData;
// ExcelGrid.DataBind();
// Response.Clear();
// Response.Buffer = true;
// Response.AppendHeader("Content-Disposition", "attachment; filename=" + strReportName + ".xls");
// Response.ContentType = "application/vnd.ms-excel";
// TableOneDayPassReport.RenderControl(oHtmlTextWriter);
// Response.Write(oStringWriter.ToString());
// HttpContext.Current.ApplicationInstance.CompleteRequest();
//// Response.End();
}
檢查此代碼@Suresh Savage –