6
  void excelsave()
  {
      try
      {
          ApplicationClass app = new ApplicationClass(); // the Excel application.

          Workbook book = null;
          Worksheet sheet = null;
          Range range = null;
          // the range object is used to hold the data
          app.Visible = false;
          app.ScreenUpdating = false;
          app.DisplayAlerts = false;

          string execPath =
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

          book = app.Workbooks.Open(@"E:\SSIS\ABC\Book1.xls",
               Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value);
          sheet = (Worksheet)book.Worksheets[1];

          range = sheet.get_Range("A1", Missing.Value);
          range.Columns.ColumnWidth = 22.34;
          range = sheet.get_Range("B1", Missing.Value);
          range.Columns.ColumnWidth = 22.34;
          book.SaveAs(@"E:\SSIS\ABC\Book1.xls", Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
       }
       catch (Exception ex)
       {

       }
 }

Here I am opening an Excel sheet trying to increase the column width and need to make the column headers as bold and save the document. Right now, the document is not getting saved. I am using VS 2008 and C# 3.5.

Is there anything that I am doing wrong here?

4
  • 8
    you could start by logging any exceptions rather than ignoring them all in that empty catch block! Commented Jan 16, 2012 at 2:14
  • Show us your ex.Message and ex.StackTrace, assuming it's raising an exception. Commented Jan 16, 2012 at 2:21
  • You better stop using Interop and start using excel-automation. Is your application a web-application? Commented Jan 16, 2012 at 2:30
  • possible duplicate of how to access an excel file in c# Commented Jan 16, 2012 at 3:52

1 Answer 1

10

I ran the following using VS 2010 and .NET 4, but this code should still work in your environment. Also, I simplified your code a bit. Hopefully this will get you going in the right direction.

    static void excelsave()
    {
        try
        {
            Application app = new Application();
            string execPath =
              Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

            Workbook book = app.Workbooks.Open(@"c:\test.xls");
            Worksheet sheet = (Worksheet)book.Worksheets[1];

            Range range = sheet.get_Range("A1");
            range.Columns.ColumnWidth = 22.34;
            range = sheet.get_Range("B1");
            range.Columns.ColumnWidth = 22.34;

            sheet.get_Range("A1", "B1").Font.Bold = true;

            book.SaveAs(@"c:\test2.xls");  // or book.Save();
            book.Close();
        }
        catch (Exception ex)
        {
        }
    } 

UPDATE

You can find a similar explanation/example of what you are doing at: http://www.dotnetperls.com/excel

Marshal.ReleaseComObject(book);  // do this after the close

Also, there is a good discussion on cleaning up Excel/COM ... How To Properly Clean Up Excel Interop Objects In c#

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.