如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装Excel?
当前回答
Syncfusion Essential XlsIO可以做到这一点。它对Microsoft office没有依赖性,并且对不同的平台也有特定的支持。
ASP.NET网站ASP.NET MVC超临界压力Xamarin公司WPF和Windows窗体Windows服务和基于批处理的操作
代码示例:
//Creates a new instance for ExcelEngine.
ExcelEngine excelEngine = new ExcelEngine();
//Loads or open an existing workbook through Open method of IWorkbooks
IWorkbook workbook = excelEngine.Excel.Workbooks.Open(fileName);
//To-Do some manipulation|
//To-Do some manipulation
//Set the version of the workbook.
workbook.Version = ExcelVersion.Excel2013;
//Save the workbook in file system as xlsx format
workbook.SaveAs(outputFileName);
如果您符合资格(收入低于100万美元),可通过社区许可计划免费获得整套控件。注:我为Syncfusion工作。
其他回答
您可以考虑使用XML Spreadsheet 2003格式创建文件。这是一种简单的XML格式,使用了一个文档化的模式。
如果您对xlsx格式感到满意,请尝试我的库EPPlus。它从ExcelPackage的源代码开始,但后来变成了完全重写。
它支持范围、单元格样式、图表、形状、图片、命名范围、自动筛选以及许多其他功能。
您有两个选项:
EPPlus 4,根据LGPL许可(原始分支机构,开发至2020年)EPPlus 5,根据Polyform非商业1.0.0许可(自2020年起)。
从EPPlus 5自述文件:
有了新的许可证,EPPlus在某些情况下仍然可以免费使用,但需要商业许可证才能在商业企业中使用。
EPPlus网站:https://www.epplussoftware.com/
您可以在Visual Studio上安装OpenXml nuget包。以下是将数据表导出到excel文件的代码:
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Public Class ExportExcelClass
Public Sub New()
End Sub
Public Sub ExportDataTable(ByVal table As DataTable, ByVal exportFile As String)
' Create a spreadsheet document by supplying the filepath.
' By default, AutoSave = true, Editable = true, and Type = xlsx.
Dim spreadsheetDocument As SpreadsheetDocument = spreadsheetDocument.Create(exportFile, SpreadsheetDocumentType.Workbook)
' Add a WorkbookPart to the document.
Dim workbook As WorkbookPart = spreadsheetDocument.AddWorkbookPart
workbook.Workbook = New Workbook
' Add a WorksheetPart to the WorkbookPart.
Dim Worksheet As WorksheetPart = workbook.AddNewPart(Of WorksheetPart)()
Worksheet.Worksheet = New Worksheet(New SheetData())
' Add Sheets to the Workbook.
Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())
Dim data As SheetData = Worksheet.Worksheet.GetFirstChild(Of SheetData)()
Dim Header As Row = New Row()
Header.RowIndex = CType(1, UInt32)
For Each column As DataColumn In table.Columns
Dim headerCell As Cell = createTextCell(table.Columns.IndexOf(column) + 1, 1, column.ColumnName)
Header.AppendChild(headerCell)
Next
data.AppendChild(Header)
Dim contentRow As DataRow
For i As Integer = 0 To table.Rows.Count - 1
contentRow = table.Rows(i)
data.AppendChild(createContentRow(contentRow, i + 2))
Next
End Sub
Private Function createTextCell(ByVal columnIndex As Integer, ByVal rowIndex As Integer, ByVal cellValue As Object) As Cell
Dim cell As Cell = New Cell()
cell.DataType = CellValues.InlineString
cell.CellReference = getColumnName(columnIndex) + rowIndex.ToString
Dim inlineString As InlineString = New InlineString()
Dim t As Text = New Text()
t.Text = cellValue.ToString()
inlineString.AppendChild(t)
cell.AppendChild(inlineString)
Return cell
End Function
Private Function createContentRow(ByVal dataRow As DataRow, ByVal rowIndex As Integer) As Row
Dim row As Row = New Row With {
.rowIndex = CType(rowIndex, UInt32)
}
For i As Integer = 0 To dataRow.Table.Columns.Count - 1
Dim dataCell As Cell = createTextCell(i + 1, rowIndex, dataRow(i))
row.AppendChild(dataCell)
Next
Return row
End Function
Private Function getColumnName(ByVal columnIndex As Integer) As String
Dim dividend As Integer = columnIndex
Dim columnName As String = String.Empty
Dim modifier As Integer
While dividend > 0
modifier = (dividend - 1) Mod 26
columnName = Convert.ToChar(65 + modifier).ToString() & columnName
dividend = CInt(((dividend - modifier) / 26))
End While
Return columnName
End Function
End Class
使用Open XML SDK 2.0 for Microsoft Office怎么样?
一些好处:
不需要安装Office由Microsoft制作=像样的MSDN文档只有一个.Net dll可用于项目SDK附带了许多工具,如diff、验证器等
链接:
githubMSDN主登录“How Do I…”起始页blogsMSDN brian_jones发布SDKblogs.MSDN brian_jones描述SDK处理大型文件而不崩溃(与DOM方法不同)
各种Office 2003 XML库适用于较小的excel文件。然而,我发现以XML格式保存的大型工作簿的大小是一个问题。例如,我使用的新XLSX格式的工作簿将是40MB(当然也更紧凑),它将变成360MB的XML文件。
就我的研究而言,有两个商业软件包允许输出到较旧的二进制文件格式。他们是:
宝石盒ComponentOne Excel
两者都不便宜(我认为分别为500美元和800美元)。但两者都独立于Excel本身。
我会好奇的是OpenOffice.org之类的Excel输出模块。我想知道它们是否可以从Java移植到.Net。