Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My code as follows;

xlsheet.Cells[1, 1] = " Printed Date " + string.Format("{0:dd-MMM-yyyy}", DateTime.Now);

Output as follows;

Printed Date 05-Mar-2013.

In excel, i get the about in the particular in the Left Position in excel.

But i want to merge the required cells fit into centre position.

how can i merge the cells using coding.


how can i do using coding.

Rgds,
Narasiman P.
Posted

1 solution

Use a Style object, e.g.
C#
Style style = _WorkBook.Styles.Add("MyStyle");
style.HorizontalAlignment = XlHAlign.xlHAlignCenter;
style.VerticalAlignment = XlVAlign.xlVAlignCenter;
xlsheet.Cells[1, 1].Style = style;

See also http://msdn.microsoft.com/en-us/library/office/ff194252.aspx[^]
 
Share this answer
 
Comments
[no name] 5-Mar-13 8:21am    
Based on your above style object,

i use based on my requirement, my code as follows;

Excel.Style style;
Excel.Workbook workbook;
style sty = workbook.Styles.Add("mystyle");
style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
style.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
xlsheet.cells[1,1].Style = style;

xlsheet.cells[1,1].style =style.
in the above line after . style is not comming defaulty.

what is the wrong in my code please help me.

kindly please help me.

how can i do?

what is the problem in my code.

Please help me.

Rgds,
Narasiman P.
Bernhard Hiller 5-Mar-13 9:10am    
You are right, a cast is still required:
(xlsheet.Cells[1,1] as Excel.Range).Style = style;
Maciej Los 5-Mar-13 9:05am    
Good job!
+5!

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