如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装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

其他回答

下面是使用LINQ to XML完成此操作的方法,并提供示例代码:

用LINQ to XML快速导入和导出Excel数据

它有点复杂,因为您必须导入名称空间等等,但它确实可以避免任何外部依赖。

(当然,它是VB.NET,而不是C#,但您可以始终在自己的项目中隔离VB.NET内容,以使用XML文字,并使用C#执行其他所有操作。)

http://www.codeproject.com/KB/cs/Excel_and_C_.aspx<=为什么不使用内置的windows功能,只需在服务器上安装office,您安装的任何应用程序都可以自动化。

使用本机方法就容易多了。

如果安装了它,你就可以使用它,这是windows中最棒、使用率最低的功能,在过去的好日子里,它被称为COM,它为你节省了大量时间和痛苦。

或者更简单地使用ref lib MS供应品-http://csharp.net-informations.com/excel/csharp-create-excel.htm

你可能想看看GemBox.Spresheet。

他们有一个免费版本,具有所有功能,但如果符合您的需要,则限制为每张150行,每张工作簿5页。

我还不需要自己使用它,但看起来很有趣。

你试过sylk吗?

我们曾经在经典的asp中生成excelsheets作为sylk,现在我们也在寻找excelsheeter。

sylk的优点是,可以格式化单元格。

OpenXML也是一个很好的选择,它有助于避免在服务器上安装MS Excel。Microsoft提供的Open XML SDK 2.0简化了操作Open XML包和包中的底层Open XML架构元素的任务。开放式XML应用程序编程接口(API)封装了开发人员在开放式XML包上执行的许多常见任务。

看看OpenXML:有助于避免在服务器上安装MS Excel的替代方案