我想从c#中创buildxlsx(Excel)文件

这是一个只能创buildxls文件的代码。 但是我想创buildxlsx(Excel)文件; 我怎么能从这个代码做到这一点,否则我可以有另一个我可以用来创buildxlsx文件的代码。

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

请尝试下面更新的代码。

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

看看EasyXLS 。 这是一个创buildxlsx文件的库。

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

更多信息请访问:
http://www.easyxls.com/manual/basics/create-excel-file.html