Click here to Skip to main content
11,805,035 members (60,027 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET SQL-Server ADO.NET , +

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 20:52pm

1 solution

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] = "First Name";
    oSheet.Cells[1, 2] = "Last Name";
    oSheet.Cells[1, 3] = "Full Name";
    oSheet.Cells[1, 4] = "Salary";

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).

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

  Print Answers RSS
0 OriginalGriff 315
1 CPallini 210
2 Richard Deeming 200
3 Sergey Alexandrovich Kryukov 168
4 CHill60 150
0 OriginalGriff 3,255
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 26 May 2015
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