简体   繁体   English

我想从 c# 创建 xlsx (Excel) 文件

[英]I want to create xlsx (Excel) file from c#

This is a code which could create only create xls file.这是一个只能创建创建 xls 文件的代码。 But I want to create xlsx (Excel) file;但我想创建 xlsx (Excel) 文件; how can I do that from this code or else can I have another code which I could use to create xlsx files.我怎么能从这段代码中做到这一点,否则我可以有另一个代码来创建 xlsx 文件。

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

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

Take a look on EasyXLS .看看EasyXLS It is a library that creates xlsx files.它是一个创建 xlsx 文件的库。

    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 http://www.easyxls.com/manual/basics/create-excel-file.html

Try OpenXML library, should do the trick, find more at尝试OpenXML库,应该可以解决问题,在

Export DataTable to Excel with Open Xml SDK in c# 使用 C# 中的 Open Xml SDK 将数据表导出到 Excel

Ps Interop won't work unless excel is installed on the machine.除非机器上安装了 excel,否则 Ps Interop将无法工作。

Take a look at my SwiftExcel library.看看我的SwiftExcel库。 It was design for quick and easy excel output and what is more important - performance.它是为快速简便的 excel 输出而设计的,更重要的是 - 性能。

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

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");用这一行: xlWorkBook.SaveAs("d:\\\\vdfgdfg.xlsx");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM