Click here to Skip to main content
15,887,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have a aspx page with two radio button and two div box with runat server and one export button.

1. Radio button Name: DSR Report
2. Radio Button Name: Daily Sales

On radio button selection i have got data from database and creating html table in c# with stringBuilder and bind to div box innerhtml property.

My html table is display properly but problem when I export my "Daily Sales" html table into excel when i open this excel it display excel stop working but no problem with exported excel of "DSR Report".

I am using same Exported Code for Excel But i donot understand what is the problem with exporting second html table in excel.

Export Code is below on export button click

C#
string fileName = string.Empty;
       if (rdDailySales.Checked)
           fileName = rdDailySales.Text + "_" + DateTime.Now.Date.Day.ToString() + "_" + DateTime.Now.Date.Month.ToString() + "_" + DateTime.Now.Date.Year.ToString();
       else
           fileName = rdDsr.Text + "_" + DateTime.Now.Date.Day.ToString() + "_" + DateTime.Now.Date.Month.ToString() + "_" + DateTime.Now.Date.Year.ToString();
       Response.Clear();
       Response.Charset = "";
       Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       Response.ContentType = "application/ms-excel.xls";
       Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       firstRow.RenderControl(hw);
       //sencondRow.RenderControl(hw);
       if (rdDsr.Checked)
           dbReport.RenderControl(hw);
       //divbreak.RenderControl(hw);
       //thirdRow.RenderControl(hw);
       if (rdDailySales.Checked)
           dbReport1.RenderControl(hw);
       FileInfo fi = new FileInfo(Server.MapPath("../Styles/jquery.dataTablesExcel.css"));
       StringBuilder sb1 = new StringBuilder();
       StreamReader sr = fi.OpenText();
       while (sr.Peek() >= 0)
       {
           sb1.Append(sr.ReadLine());
       }
       sr.Close();
       Response.Write("<html><head><style type='text/css'>" + sb1.ToString() + "</style><head>" + sw.ToString() + "</html>");
       sw = null;
       hw = null;
       Response.Flush();
       Response.End();


Please guide me what is missing in exporting second html table.
Posted
Comments
Sinisa Hajnal 22-Jul-15 11:23am    
Try filling the data from the first table into the second and check that it exports properly. That way you'll know if there is something in the data that is a problem.

Other then that, why don't you export to excel from the database, save the file on the server and simply provide the link for download? Wouldn't that be easier?
Ram Kumar(Webunitech) 23-Jul-15 1:54am    
thanks for your suggestion i found the solutions i have made a little mistake

Hi,

I have found the solutions is. I am using float:left in DailySales report so when i am exported into excel and try to open its give error. Now I am remove float:left from dailysales table it's working.

Thanks
 
Share this answer
 
Advice: When you ask a question, you don't have to use also a solution.
- You may Improve question or post comments to discuss with helpers.
- When the solution is found, you can say which solution was helpful.
- When the solution is found, it is also a good practice to close the question as a closed question says every helper that no more help is needed.
 
Share this answer
 

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