Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, Im stuck on a really small problem. Ihave an application that once completed writes the results out to an excel file. I want to format the excle file. i.e colours and fonts but im having no luck. code below,
C#
public void WriteOutToExcel(System.Data.DataTable objDataTable, string csFileName)
       {

           Microsoft.Office.Interop.Excel.Application xlApp;
           Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
           Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
           object misValue = System.Reflection.Missing.Value;
           int iRowCount = 0;
           int iColumnCount = 0;

           string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now, csFileName);
           string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), filename);

           xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
           xlWorkBook = xlApp.Workbooks.Add(misValue);

           xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

           //Created the Header columns
           iRowCount += 1;
           for (int i = 0; i < objDataTable.Columns.Count; i++)
           {
               iColumnCount += 1;
               xlApp.Cells[iRowCount, iColumnCount] = objDataTable.Columns[i].ColumnName;

           }

           // loop through the datatable and insert all the results
           foreach (DataRow row in objDataTable.Rows)
           {
               iRowCount += 1;
               iColumnCount = 0;
               object[] array = row.ItemArray;
               for (int i = 0; i < array.Length ; i++)
               {
                   iColumnCount += 1;
                   xlApp.Cells[iRowCount, iColumnCount] = array[i].ToString();
               }

           }

           //Save the file.
           xlWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
           xlWorkBook.Close(true, misValue, misValue);
           xlApp.Quit();

           //Finally destroy all the objects.
           if (xlApp != null)
               Marshal.ReleaseComObject(xlApp);
           if (xlWorkBook != null)
               Marshal.ReleaseComObject(xlWorkBook);
           if (xlWorkSheet != null)
               Marshal.ReleaseComObject(xlWorkSheet);

       }

Any help is greatly apreciated thanks.
Posted
Updated 20-Apr-12 4:44am
v2
Comments
Sandeep Mewara 20-Apr-12 10:45am    
Always wrap your code in PRE tag for better readability.

I don't know where is the problem...

You need to know Microsoft Excel Objects[^], especially: borders, font, interior.

In VB:
VB
xlRange = xlWorkSheet.Range("A1:A5")
'borders
xlRange.Borders.LineStyle = xlDouble
xlRange.Borders(xlEdgeBottom).Color = vbRed
'font
xlRange.Font.Bold = True
'interior
xlRange.Interior.ColorIndex = vbYellow


And a little tip:
Copying data from recordset, try to use MS Excel method for Worksheet:
VB
xlWorksheet.Range("A1").CopyFromRecordset (objRecordset)

It's much, much quickest then copying it in the for ... to... next loop.

That's all!
 
Share this answer
 
v3
Comments
frostcox 20-Apr-12 12:12pm    
Thanks very much I will try that.
Maciej Los 20-Apr-12 12:34pm    
If my answer was helpful, please mark the solution as "solved" and rate it.
wizardzz 20-Apr-12 12:21pm    
Fixed spelling.
Maciej Los 20-Apr-12 12:34pm    
Thank you ;)
To add style, select the range to which to apply a style, and set the appropriate properties. I don't have a dev environment in from of me, so this'll be a bit off, but for example, something like this would set your first three column headers to bold
C#
xlWorkSheet.Range("A0:C0").Font.Bold = True;


For other things, you should explore the Excel enums in your interop and use those in assigning values (most start with xl, so you can find them in intellisense).
 
Share this answer
 
Comments
frostcox 20-Apr-12 12:13pm    
Nice one thanks for your help.

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