ho provato a trasferire i dati della vista griglia per eccellere .... ma l'output è un foglio excel vuoto. Come risolvere questo problema? Esiste un codice per trasferire il valore della vista griglia a excel foglio con base di dati?gridview data export to excel in asp.net
protected void btnexcel_Click1(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";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
gvdetails.HeaderRow.Cells[0].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[1].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[2].Style.Add("background-color", "green");
for (int i = 0; i < gvdetails.Rows.Count;i++)
{
GridViewRow row = gvdetails.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
}
}
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
}
Qual è l'errore che state ottenendo qui? – Ratna
@Ratna: il foglio Excel che sta generando è vuoto. Si prega di leggere il requisito correttamente. – pordi
spero che tu sappia che questa non è un'esportazione excel, stai servendo una tabella html e un browser ingannevole con un tipo di contenuto sbagliato nella risposta. Ci sono numerosi problemi che puoi ottenere con questo approccio. http://stackoverflow.com/a/10245406/351383 –