Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET SQL-Server ADO.NET Excel , +

I am actually creating a C# windows app that will be generating an excel from a data-table which is the output of a query. The excel needs to be formatted with some specific fonts for each column and the headers.. I know how to create excel but how to format it is not known to me. Can anyone help.

after the excel is created, i need to fetch the file and select some particular columns and then paste them in an outlook mail and send is possible??

Posted 22-Jun-12 21:52pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The way I have done this sort of things in the past was by using Automation.

This sort of programs:
//Start Excel and get Application object.
    oXL = new Excel.Application();
    oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = &quot;First Name&quot;;
oSheet.Cells[1, 2] = &quot;Last Name&quot;;
oSheet.Cells[1, 3] = &quot;Full Name&quot;;
oSheet.Cells[1, 4] = &quot;Salary&quot;;</pre>

This is quite easy to generate this sort of code with Word.
Generally I use record a macro and then I look at the code it generated code.
I assume you can perhaps do the same with EXcel.

more details:[^]
Jashobanta at 26-Jun-12 14:58pm
I actually want to use Interop.Excel as I am using .net and the company where I work will not permit me to use any 3rd party application when there is already a way to do that.
@Pascal: so, if I need more than one one sheet in a single excel file, is it possible to work in the same way? Can it be formatted?? I did not get time to try this on,but your suggestions seem to be really helpful.
Thanks in Advance......
Pascal Ganaye at 26-Jun-12 16:03pm
Yes you can create more than one sheet.
From memory:
sheet1 = oWB.Sheets.Add("Sheet1");
sheet2 = oWB.Sheets.Add("Sheet2");
Then something like
sheet1.Cells[1,2].Font.Bold = True;
I am sure it works but not of the syntax (and I don't have Excel here).
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Pascal is suggesting to use Excel.Interop. I direct you to see a 3d party [^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200

Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100