Wednesday 12 September 2012

C# - Excel - How to create simple Excel file programatically



Watch this example on YouTube:

1. First add Excel reference  (Microsoft Excel 14.0 Object Library) from COM tab to your project




Add the following
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

Add button to the form and the following on click
        private void button1_Click(object sender, EventArgs e)
        {
            string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
            if (File.Exists(fileTest))
            {
                File.Delete(fileTest);
            }

            Excel.Application oApp;
            Excel.Worksheet oSheet;
            Excel.Workbook oBook;

            oApp = new Excel.Application();
            oBook = oApp.Workbooks.Add();
            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            oSheet.Cells[1, 1] = "some value";

            oBook.SaveAs(fileTest);
            oBook.Close();
            oApp.Quit();
        }

That's it, file has been created. Watch this example on YouTube:

11 comments:

  1. wow THANK you alot...

    Just for others who had the same error with me and could not see the video clearly--

    I changed the
    FROM: string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";

    TO:string fileTest = "C:\\more.xlsx";
    Then the whole code worked

    ReplyDelete
  2. Schreibt stat
    string fileTest = "C:\\Temp\\ExcelTest\\test.xlsx";
    das
    string fileTest = @"C:\Temp\ExcelTest\test.xlsx";

    ReplyDelete
  3. I ran into an error on

    "oBook = oApp.Workbooks.Add();"

    "Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

    Any idea how to solve it? Cheers,

    ReplyDelete
    Replies
    1. Have you sorted the error? I get the same one

      Delete
    2. you must have excel installed on your computer. if this is not an option than use OpenXML - example here https://www.youtube.com/watch?v=YAXzQ3sXxxE

      Delete
  4. I have used Aspose.Cells for .NET Library to create an excel file and it has generated very good results and they offer many more code sample to be used in application. Try it:

    http://www.aspose.com/.net/excel-component.aspx

    ReplyDelete
  5. Finally, I got the code tht interact with an excel

    ReplyDelete
  6. This Error is shown

    Microsoft Excel cannot access the file 'C:\Temp\ExcelTest\3C6CEB00'. There are several possible reasons:

    • The file name or path does not exist.
    • The file is being used by another program.
    • The workbook you are trying to save has the same name as a currently open workbook.

    ReplyDelete
  7. hello
    can you tell me how to create .aspx file
    please make a video on it

    ReplyDelete
  8. I am using Visual Studio 2017 want to create a new excel file and wish to add spreadsheets to it.
    Getting an error at
    oSheet = (Excel.Worksheet)oBook.Worksheets.get_item(1);

    ReplyDelete
  9. I have been looking at C# to Excel code for weeks. Its all has so many extra steps which are unnecessary. This code works amazingly. Great work and thanks for sharing!

    ReplyDelete