Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 358.4k

Datatable to Excel (Excel formatting)

Mar 28 2017 5:44 AM
I have a data table .I want to Export it to a Excel file.I did it using the following code.
  1. StreamWriter wr = new StreamWriter(@"E:\\Book1.xls");  
  2. // Write Columns to excel file  
  3. for (int i = 0; i < dt.Columns.Count; i++)  
  4. {  
  5. wr.Write(dt.Columns[i].ToString().ToUpper() + "\t");  
  6. }  
  7. wr.WriteLine();  
  8. //write rows to excel file  
  9. for (int i = 0; i < (dt.Rows.Count); i++)  
  10. {  
  11. for (int j = 0; j < dt.Columns.Count; j++)  
  12. {  
  13. if (dt.Rows[i][j] != null)  
  14. {  
  15. wr.Write(Convert.ToString(dt.Rows[i][j]) + "\t");  
  16. }  
  17. else  
  18. {  
  19. wr.Write("\t");  
  20. }  
  21. }  
  22. wr.WriteLine();  
  23. }  
  24. wr.Close();  
  25. label1.Text = "Data Exported Successfully";  
  26. }  
  27. catch (Exception ex)  
  28. {  
  29. throw ex;  
  30. }  
 But I want Some Cell Formatting in Xls .Want to change the color of the entire cell like that something.Please help me to solve my problem.

Answers (3)