Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

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 it....is is possible??

ANY POINTERS WOULD BE HELPFUL...THANKS........
Posted

1 solution

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

This sort of programs:
C#
//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:
http://support.microsoft.com/kb/302084[^]
 
Share this answer
 
v2
Comments
Jashobanta 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900