0
在將GridView
數據寫入Excel之前,我試圖將文本「這是我的客戶信息」打印到Excel文件中。該文本應該在最上面,剩餘的數據應該顯示在本文下面。如何在將網格數據寫入Excel之前打印文本
任何人都可以幫助我嗎?我該怎麼做?請檢查我目前的代碼。該代碼只是將我的GridView
數據寫入Excel文件。
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
Search(null, null);
// GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
// row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
//table.Rows.Add(title);
}
GridView1.Columns[7].Visible = false;
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
文本添加到GridView然後把在Excel中的網格?你有一個標題行,你想在哪裏顯示文字? –
這段文字應該在excel的第一行。我不想在gridview中顯示這個文本, – aniltc
我給你的建議是使用Microsoft Access Redistributables(https://stackoverflow.com/questions/23041021/how-to-write-some-data-to-excel-file -xlsx)或DocumentFormat.OpenXML(https://www.nuget.org/packages/DocumentFormat.OpenXml/)或某種類型的OpenXML包裝器來編寫文檔。通過自己直接寫入文件來創建它們是在尋求麻煩。 –