如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装Excel?


当前回答

您只需使用Excel XML格式将其写入XML,并以.XLS扩展名命名,它将以Excel打开。您可以控制XML文件标题中的所有格式(粗体、宽度等)。

维基百科中有一个XML示例。

其他回答

商业解决方案SpreadsheetGearfor.NET将做到这一点。

您可以在此处查看ASP.NET(C#和VB)的实时示例,并在此处下载评估版本。

免责声明:我拥有SpreadsheetGear LLC

这里有一个完全免费的C#库,它允许您使用OpenXML库从DataSet、DataTable或List<>导出到真正的Excel 2007.xlsx文件中:

http://mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

免费提供完整的源代码以及说明和演示应用程序。

将此类添加到应用程序后,只需一行代码即可将DataSet导出到Excel:

CreateExcelFile.CreateExcelDocument(myDataSet, "C:\\Sample.xlsx");

没有比这更简单的了。。。

它甚至不需要Excel出现在您的服务器上。

您可以在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

我使用了几个选项:

如果XLSX是必须的:ExcelPackage是一个很好的开始,但当开发人员停止工作时就夭折了。ExML不是一个坏选项,我仍在一些生产网站上使用它。

不过,对于我的所有新项目,我都使用NPOI,即Apache POI的.NET端口。NPOI 2.0(Alpha)也支持XLSX。

如果您正在创建Excel 2007/2010文件,请尝试此开源项目:https://github.com/closedxml/closedxml

它提供了一种面向对象的方式来操作文件(类似于VBA),而无需处理XML文档的麻烦。可以使用使用任何.NET语言,如C#和Visual Basic(VB)。ClosedXML允许您在不使用Excel的情况下创建Excel 2007/2010文件应用典型的示例是在web上创建Excel报表服务器:var工作簿=新XLWorkbook();var worksheet=workbook.Worksheets.Add(“样本表”);worksheet.Cell(“A1”).Value=“Hello World!”;工作簿.另存为(“HelloWorld.xlsx”);