If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



Did you know.. How to generate Excel 2007 file in C#?

by Farooq Kaiser 24. February 2010 02:19

You can do it using ExcelPackagethat provides server-side generation of Excel 2007 spreadsheets. You can download it from codeplex.  Here is sample code to generate XLSX file using ExcelPackage.

   1: FileInfo XLSXFile = new FileInfo(XLSXFileName);
   2: using (ExcelPackage xlPackage = new ExcelPackage(XLSXFile))
   3: {
   4:     ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Sheet1");
   5:  
   6:     int row = 1;
   7:     foreach (DataRow dr in dataTable.Rows)
   8:     {
   9:         int col = 1;
  10:  
  11:         foreach (object o in dr.ItemArray)
  12:         {
  13:             worksheet.Cell(row, col++).Value = o.ToString().Trim();
  14:         }
  15:  
  16:         row++;
  17:     }
  18:  
  19:     xlPackage.Save();
  20: }

Please feel free to leave any comments. Thanks.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | C# | Did you know


comments powered by Disqus

Comments

Jobs Autos Real estate Videos Power by Google