Click here to Skip to main content
16,016,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I am not able to format my excel sheet through my C# code. My code is
C#
try
            {
                object misValue = System.Reflection.Missing.Value;
                xlApp = new Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Add(path);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                xlApp.DisplayAlerts = false;
                //Excel.Range excelCell = xlApp.get_Range("A1", "F10");
                Excel.Range excelCell = (Excel.Range)xlWorkSheet.get_Range("A1", "F10");
                excelCell.NumberFormat = "@";

                //Excel.Ranges excelCell1 = xlApp.get_Range("H1", "O10");
                Excel.Range excelCell1 = (Excel.Range)xlWorkSheet.get_Range("H1", "O10");
                excelCell1.NumberFormat = "@";

                //xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlXMLSpreadsheet, misValue, misValue,
                //false, false, Excel.XlSaveAsAccessMode.xlExclusive,misValue, misValue, misValue, misValue, misValue);

                xlWorkBook.Save();
                xlWorkBook.Close(false, false, misValue);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
            }
            catch (Exception ex)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
                MessageBox.Show(ex.Message);
            }

thanks
Posted
Updated 14-Jul-12 0:24am
v2
Comments
Sandeep Mewara 14-Jul-12 6:24am    
Elaborate: "not able to format my excel sheet ". What were you trying/tried and stuck/failed?
Kenneth Haugland 14-Jul-12 7:27am    
Have you tried to remove the try catch block and see were you get the error?
Whictopis 31-May-17 4:49am    
Here is something that works:

ExcelFile xlWorkBook = ExcelFile.Load(path);
ExcelWorksheet xlWorkSheet = xlWorkBook.Worksheets[0];
xlWorkSheet.Cells.GetSubrange("A1", "F10").Style.NumberFormat = "@";
xlWorkSheet.Cells.GetSubrange("H1", "O10").Style.NumberFormat = "@";

The code demonstrates how to use excel range's in C# with a help of this C# library for excel files.

Please try to export excel as in the below article, you can format easily.

Export DataTable to Excel with Formatting in C#[^]
 
Share this answer
 
Hope You can find your solution by : Summarize C# Control Excel Skills[^]
Best Regards
 
Share this answer
 

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