Click here to Skip to main content
15,885,936 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void ExporttoExcel(DataTable table)
       {
           filename = "All_OS" + "_" + datepicker.Value.Replace("/", "-").ToString() +".xls";
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.ClearContent();
           HttpContext.Current.Response.ClearHeaders();
           HttpContext.Current.Response.Buffer = true;
           HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
           HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
           HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
           HttpContext.Current.Response.Charset = "utf-8";
           HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
           HttpContext.Current.Response.Write("<style> .amt{mso-number-format:\"\\#\\#0\\.00\";} </style>");            //sets font
           HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
           HttpContext.Current.Response.Write("<BR><BR><BR>");
           //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
           HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' "
                                              + "borderColor='#000000' cellSpacing='0' cellPadding='0' "
                                              + "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
           //am getting my grid's column headers
           int columnscount = table.Columns.Count;
           HttpContext.Current.Response.Write("<TR>");
           HttpContext.Current.Response.Write("<Td colspan=23>");
           HttpContext.Current.Response.Write("<font style='font-size:12.0pt; font-family:Calibri;'><CENTER> ");
           HttpContext.Current.Response.Write("All Outstanding " + datepicker.Value.ToString());
           HttpContext.Current.Response.Write(" </CENTER></Font> ");
           HttpContext.Current.Response.Write("</Td>");
           HttpContext.Current.Response.Write("<TR>");
           for (int j = 0; j < columnscount; j++)
           {
                 //write in new column
                   HttpContext.Current.Response.Write("<Td>");
                   //Get column headers  and make it as bold in excel columns
                   HttpContext.Current.Response.Write("");
                   HttpContext.Current.Response.Write(table.Columns[j].ColumnName.ToString());
                   HttpContext.Current.Response.Write("");
                   HttpContext.Current.Response.Write("</Td>");
           }
           HttpContext.Current.Response.Write("</TR>");

           foreach (DataRow row in table.Rows)
           {//write in new row
               HttpContext.Current.Response.Write("<TR>");
               for (int i = 0; i < table.Columns.Count; i++)
               {
                   HttpContext.Current.Response.Write("<Td>");
                   HttpContext.Current.Response.Write(row[i].ToString());
                   HttpContext.Current.Response.Write("</Td>");
               }
               HttpContext.Current.Response.Write("</TR>");
           }

           HttpContext.Current.Response.Write("</Table>");
           HttpContext.Current.Response.Write("</font>");
           HttpContext.Current.Response.Flush();
           HttpContext.Current.Response.End();
       }








Output
Actual Result
50
100
2,100.00
3,450.00

Expected Result
50.00
100.00
2,100.00
3,450.00
Posted
Updated 27-May-14 2:10am
v5

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