나는 눈금보기가있다. 어떤 컬럼을 포함합니다. 그것이 단지 10 개의 열을 가지고 있다고 가정하면, 2 개의 열만이 있습니다. 반면에 2 개의 열 머리글은 비어 있습니다. 이제 이름없이 2 개의 열이있는 10 개의 열이 모두 포함 된 Excel에이 gridview를 내보내고 있습니다. GridView를 엑셀으로 내보내는 동안 빈 헤더 열 두 개를 제외 할 수 있습니까? 당신이 그들을 필요하지 않은 경우엑셀에서 그리드보기를 내보내는 동안 일부 열을 건너 뛰는 방법은 무엇입니까?
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Export("Customers.xls", this.gdvMessages);
}
private void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
// add the header row to the table
if (gv.HeaderRow != null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
"HTML 테이블에 두 개의 열을 포함하지 말아야합니다."예제를 제공 할 수 있습니까? – Sandy