在Excel中添加标题将数据导入到Excel时 [英] Add header in Excel While import datatable to excel
问题描述
你好,
如何在将任何数据表导入excel时在excel中添加标题。
我使用此函数导入excel
public void ExportToExcel(DataTable dt)
{
if(dt.Rows.Count> 0)
{
string filename =PatientList.xls;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//获取控件的HTML。
dgGrid.RenderControl(hw);
//将HTML写回浏览器。
//Response.ContentType = application / vnd.ms-excel;
Response.ContentType =application / vnd.ms-excel ;
Response.AppendHeader(Content-Disposition,附件; filename =+ filename +);
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
Hello,
How to add header in excel while importing any datatable into excel.
I am using this function to Import to excel
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "PatientList.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
推荐答案
添加以下代码,你会得到标题。
Response.AppendHeader(Content-Disposition,sheetname);
Add the below code, you will get header.
Response.AppendHeader("Content-Disposition", sheetname);
从DataTable向Excel导出数据 [ ^ ]
点击此链接。
Export Data to Excel from DataTable[^]
Go through this link.
// column headings
for (int i = 0; i < Tbl.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
}
这篇关于在Excel中添加标题将数据导入到Excel时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!