Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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
C#
protected void bttnExportXL_Click(object sender, EventArgs e)
    {
       gridViewMaster.AllowPaging = false;
       gridViewMaster.AllowSorting = false;
        Response.Clear();

        //Response.AddHeader("content-disposition", "attachment;filename=Report_"+ DateTime.Now.ToShortDateString() +".xlsx");
        Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");
        Response.Charset = "";

        // If you want the option to open the Excel file without saving than
        // comment out the line below
        // Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        //Response.ContentType = "application/vnd.xls";
        
        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
Posted
Comments
Sinisa Hajnal 4-Nov-16 3:41am    
While newer version commonly can open old version files, the reverse in not true. You should try and open the file in Excel 2007 it will tell you if the new file format is supported. If not, you need to save the file in older format.
Richard Deeming 4-Nov-16 9:42am    
You are NOT exporting an Excel file.

You are sending back some HTML, along with a header that "lies" to the browser, and tells it the response is an Excel document.

Excel then does its best to interpret that HTML as an Excel sheet, but the results may vary. Other spreadsheet applications may or may not be able to import the HTML you're returning.

You'll probably have better luck if you use a library which generates a real Excel file. For example:
* EPPlus[^];
* ClosedXML[^];
* The OpenXML SDK[^];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900