I want to create xlsx (Excel) file from c#
Asked Answered
R

6

14

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");
        }
Ruffianism answered 12/1, 2017 at 5:21 Comment(3)
Possible duplicate of Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs ErrorTransport
Have you taken a look at nuget.org/packages/GemBox.Spreadsheet ? It is a component that doesnt require excel and there is a free licence for small sheets.Edith
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 :DHippodrome
V
23

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");
    }
Varicotomy answered 12/1, 2017 at 5:25 Comment(2)
i tried it once it dosent work it only save a xlsx file but cannot open itRuffianism
Please try my updated code.It is working properly.It will save excel in .XLSX and will open it too.Varicotomy
E
3

You may use the NPOI library to create an Excel document. It doesn't require Office installed and doesn't use Interop / COM+. It also supports .NET Core.

using var workbook = new XSSFWorkbook();

var sheet = workbook.CreateSheet("Sheet1");

int rowIndex = 0;

var row = sheet.CreateRow(rowIndex++);
int cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("ID");
row.CreateCell(cellIndex++).SetCellValue("Name");

var row = sheet.CreateRow(rowIndex++);
cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("1");
row.CreateCell(cellIndex++).SetCellValue("One");

var row = sheet.CreateRow(rowIndex++);
cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("2");
row.CreateCell(cellIndex++).SetCellValue("Two");


string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
string xlsxPath = Path.Combine(path, $"vdfgdfg.xlsx");

using var fileStream = new FileStream(xlsxPath, FileMode.Create, FileAccess.Write);
workbook.Write(fileStream);

Links:

Energetics answered 26/10, 2023 at 10:11 Comment(0)
P
2

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);
}
Phallic answered 5/1, 2020 at 4:40 Comment(1)
Also it's under MIT license.Deicer
R
1

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

Relapse answered 12/1, 2017 at 7:22 Comment(0)
S
1

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.

Shammy answered 1/4, 2019 at 12:3 Comment(0)
A
1

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");

Appear answered 5/10, 2020 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.