我们如何在下面的函数中为asp.net中的excel表提供标题和标题
本文关键字:标题 excel 中的 net asp 在下面 函数 我们 | 更新日期: 2023-09-27 18:29:49
我想要格式化的函数如下
private void GenerateXLSXFile(DataTable tbl)
{
ExcelPackage excelPackage = new ExcelPackage();
ExcelWorkSheet excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage");
excelWorksheet.Cells("A1").LoadFromDataTable(tbl, true);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
MemoryStream stream = new MemoryStream(excelPackage.GetAsByteArray());
Response.OutputStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
Response.Flush();
Response.Close();
}
取一个临时网格,然后如果您使用数据表,请使用该网格或按如下方式使用数据。。。
DataSet ds = ViewState["Data"] as DataSet;
DataView sortedView = (ds.Tables[0]).DefaultView;
//DataTable dt = ViewState["Data"].ToString();
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = sortedView;
GridView1.DataBind();
GridView1.Caption = "ABCD"; //// allows you to give caption to excel sheet
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=RM.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.Caption = "RM Master";
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:'@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();