导出到excel(单个网格视图转换为单个excel文件,分为多个excel表) [英] export to excel ( single grid view into a single excel file divided into multiple excel sheet)
问题描述
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BAL objBAL = new BAL();
GridView1.DataSource = objBAL.associateDetails();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in GridView1.Rows)
{
gvrow.BackColor = Color.White;
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
************************ ************************************************** ******************
i使用上面的代码将网格视图数据导出到excel表格。
as我有大量的记录所有记录都进入单个excel表。
i希望将记录分成不同的部分并导出到单个excel文件中,但有多个表保存记录。
请告诉我我可以在代码中做的最大修改,以达到我的要求。
********************************************************************************************
i am using the above code to export the grid view data into excel sheet.
as i have large number of records all the records come into single excel sheet.
i want the records to be divided into different parts and export into single excel file but having multiple sheet holding the records.
please tell me the most possible modification i can do in my code to achieve my requirements.
推荐答案
有一个与您的帖子有关的文章。请通过这个我希望它有所帮助。
将数据集导出到多个Excel表格 [ ^ ]
There is an article relating to your post. Please go through this i hope it helps.
Export DataSet to Multiple Excel Sheets[^]
这篇关于导出到excel(单个网格视图转换为单个excel文件,分为多个excel表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!