In my web application, I have Grid view and Chart control i need to export to excel. I have OpenOffice only.I exported in .xlsx format to excel but i need to open in excel 2007 it's not open i tried some code in my system and downloaded document i tried to open in windows 2007 system but it was not open.
This is my code without modified code and i found some where that code is below and how can i set to my code i have two controls to export in i founded code have only one control exported.
Please tell me how to do this.
Thank you
What I have tried:
string tmpChartName = "test2.jpg";
protected void btnExport_Click(object sender, EventArgs e)
{
string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MediCountReport.xlsx"));
Response.ContentType = " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string header = @"<table><tbody><tr><td></td></tr></tbody></table>";
Response.Write(header);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
GridView1.AllowPaging = false;
BindGraph();
GridView1.HeaderRow.Style.Add("background-color", "#F5FFFA");
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#DCDCDC");
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
I found this below code
protected void bttnExportXL_Click(object sender, EventArgs e)
{
gridViewMaster.AllowPaging = false;
gridViewMaster.AllowSorting = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
foreach (GridViewRow r in gridViewMaster.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
{
r.Cells[columnIndex].Attributes.Add("class", "text");
}
}
}
gridViewMaster.RenderControl(htmlWrite);
string style = @"<style> .text { mso-number-format:\@; } ";
Response.Write(style);
Response.Write(stringWrite.ToString());
Response.End();
}
Thank you