11

This is a code which could create only create xls file. But I want to create xlsx (Excel) file; how can I do that from this code or else can I have another code which I could use to create xlsx files.

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;


Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                MessageBox.Show("Excel is not properly installed!!");
                return;
            }


            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Cells[1, 1] = "ID";
            xlWorkSheet.Cells[1, 2] = "Name";
            xlWorkSheet.Cells[2, 1] = "1";
            xlWorkSheet.Cells[2, 2] = "One";
            xlWorkSheet.Cells[3, 1] = "2";
            xlWorkSheet.Cells[3, 2] = "Two";



            xlWorkBook.SaveAs("d:\\vdfgdfg.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
        }
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Malinda Peiris
  • 492
  • 1
  • 6
  • 17
  • Possible duplicate of [Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error](http://stackoverflow.com/questions/9769703/exporting-to-xlsx-using-microsoft-office-interop-excel-saveas-error) – Circle Hsiao Jan 12 '17 at 05:24
  • Have you taken a look at https://www.nuget.org/packages/GemBox.Spreadsheet ? It is a component that doesnt require excel and there is a free licence for small sheets. – Esben Skov Pedersen Jan 12 '17 at 05:55
  • It might be helpful for you to know that the only thing you need to change in your code is `xlWorkBookNormal` to `xlOpenXMLWorkbook` and of course change your file name from `xls` to `xlsx` :D – mw509 Jun 10 '20 at 11:27

5 Answers5

19

Please try below updated code.

    public void CreateExcel()
    {
      Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        if (xlApp == null)
        {
            MessageBox.Show("Excel is not properly installed!!");
            return;
        }


        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlWorkSheet.Cells[1, 1] = "ID";
        xlWorkSheet.Cells[1, 2] = "Name";
        xlWorkSheet.Cells[2, 1] = "1";
        xlWorkSheet.Cells[2, 2] = "One";
        xlWorkSheet.Cells[3, 1] = "2";
        xlWorkSheet.Cells[3, 2] = "Two";

                  //Here saving the file in xlsx
                xlWorkBook.SaveAs("d:\\vdfgdfg.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, misValue,
                misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);


        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);

        MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xlsx");
    }
0

Take a look on EasyXLS. It is a library that creates xlsx files.

    ExcelDocument workbook = new ExcelDocument(1);

    // Set the sheet name
    workbook.easy_getSheetAt(0).setSheetName("Sheet1");

   // Add data
   ExcelTable xlsTable = ((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
   xlsTable.easy_getCell(0, 0).setValue("ID");
   xlsTable.easy_getCell(0, 1).setValue("Name");
   xlsTable.easy_getCell(1, 0).setValue("1");
   xlsTable.easy_getCell(1, 1).setValue("One");
   xlsTable.easy_getCell(2, 0).setValue("2");
   xlsTable.easy_getCell(2, 1).setValue("Two");

    // Create Excel file
    workbook.easy_WriteXLSXFile("d:\\vdfgdfg.xlsx");

See more at:
http://www.easyxls.com/manual/basics/create-excel-file.html

alex.pulver
  • 2,031
  • 2
  • 28
  • 28
0

Try OpenXML library, should do the trick, find more at

Export DataTable to Excel with Open Xml SDK in c#

P.s. Interop won't work unless excel is installed on the machine.

Blue
  • 56
  • 1
  • 8
0

Take a look at my SwiftExcel library. It was design for quick and easy excel output and what is more important - performance.

using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
{
    ew.Write("ID", 1, 1);
    ew.Write("Name", 2, 1);

    ew.Write("1", 1, 2);
    ew.Write("One", 2, 2);

    ew.Write("2", 1, 3);
    ew.Write("Two", 2, 3);
}
Roman.Pavelko
  • 1,307
  • 1
  • 10
  • 15
0

Replace the following line:

 xlWorkBook.SaveAs("d:\\vdfgdfg.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

with this line: xlWorkBook.SaveAs("d:\\vdfgdfg.xlsx");