在没有安装Office的情况下,在.NET中创建Excel文件

4

我需要在一个没有安装Microsoft Office的生产服务器(Windows Server 2008)上使用.NET创建多个工作表的Excel文件。是否可以通过VS2010和Windows Server 2008来实现这一目标,或者是否可以通过使用Office Web Apps来实现。


2
可能是与https://dev59.com/rXVC5IYBdhLWcg3w51hv重复的问题。 - Brian Clapper
为什么要点踩?新用户... - jgauffin
https://dev59.com/rXVC5IYBdhLWcg3w51hv 的答案提到了许多可以实现你所需功能的库。你的问题实际上是一个选择问题。 - Codo
5个回答

4

请在引用产品、库或任何内容时,附上链接。 - Samuel Neff
你不觉得OpenXML非常难用吗?仅仅像添加超链接这样简单的操作就需要大量的代码。 - David

0

0

如果目标是Office 2007或2010,您可以尝试使用这个免费库http://epplus.codeplex.com/

如果目标是<=2003,您可以尝试使用GemBox.Spreadsheet

免费版本的限制为: 每个工作表的最大行数为150。 每个工作簿的最大工作表数为5。


我尝试了EPPlus。但当它尝试读取我正在处理的Excel文件时,在库中出现了NullReferenceException。 - Samuel Neff

0

使用官方的Microsoft OpenXML SDK。


0

试试这个:

Sub exportExcel(ByVal grdView As DataGridView, ByVal fileName As String, _
                    ByVal fileExtension As String, ByVal filePath As String)

        ' Choose the path, name, and extension for the Excel file
        Dim myFile As String = filePath & "\" & fileName & fileExtension
        ' Open the file and write the headers
        Dim fs As New IO.StreamWriter(myFile, False)

        Try

            fs.WriteLine("<?xml version=""1.0""?>")
            fs.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
            fs.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")

            ' Create the styles for the worksheet
            fs.WriteLine("  <ss:Styles>")
            ' Style for the column headers
            fs.WriteLine("    <ss:Style ss:ID=""1"">")
            fs.WriteLine("      <ss:Font ss:Bold=""1""/>")
            fs.WriteLine("      <ss:Alignment ss:Horizontal=""Center"" ss:Vertical=""Center"" " & _
                "ss:WrapText=""1""/>")
            fs.WriteLine("      <ss:Interior ss:Color=""#C0C0C0"" ss:Pattern=""Solid""/>")
            fs.WriteLine("    </ss:Style>")
            ' Styles for the column information
            fs.WriteLine("    <ss:Style ss:ID=""2"">")
            fs.WriteLine("      <ss:Alignment ss:Vertical=""Center"" ss:WrapText=""1""/>")
            fs.WriteLine("    </ss:Style>")
            fs.WriteLine("  </ss:Styles>")

            ' Write the worksheet contents

            fs.WriteLine("<ss:Worksheet ss:Name=""EasyWorks"">")
            fs.WriteLine("  <ss:Table>")

            For i As Integer = 0 To grdView.Columns.Count - 1
                fs.WriteLine(String.Format("    <ss:Column ss:Width=""{0}""/>", _
                grdView.Columns.Item(i).Width))
            Next

            fs.WriteLine("    <ss:Row>")
            For i As Integer = 0 To grdView.Columns.Count - 1
                If grdView.Columns(i).Visible Then
                    fs.WriteLine(String.Format("      <ss:Cell ss:StyleID=""1"">" & _
                        "<ss:Data ss:Type=""String"">{0}</ss:Data></ss:Cell>", _
                        grdView.Columns.Item(i).HeaderText))
                End If
            Next
            fs.WriteLine("    </ss:Row>")

            ' Check for an empty row at the end due to Adding allowed on the DataGridView
            Dim subtractBy As Integer, cellText As String
            If grdView.AllowUserToAddRows = True Then subtractBy = 2 Else subtractBy = 1
            ' Write contents for each cell
            For i As Integer = 0 To grdView.RowCount - subtractBy
                If grdView.Rows(i).Visible Then
                    fs.WriteLine(String.Format("    <ss:Row ss:Height=""{0}"">", _
                        grdView.Rows(i).Height))
                    For intCol As Integer = 0 To grdView.Columns.Count - 1
                        If grdView.Columns(intCol).Visible Then
                            cellText = grdView.Item(intCol, i).Value.ToString
                            ' Check for null cell and change it to empty to avoid error
                            If cellText = vbNullString Then cellText = ""
                            fs.WriteLine(String.Format("      <ss:Cell ss:StyleID=""2"">" & _
                                "<ss:Data ss:Type=""String"">{0}</ss:Data></ss:Cell>", _
                                cellText.ToString))
                        End If
                    Next
                    fs.WriteLine("    </ss:Row>")
                End If
            Next

            ' Close up the document
            fs.WriteLine("  </ss:Table>")
            fs.WriteLine("</ss:Worksheet>")
            fs.WriteLine("</ss:Workbook>")
            fs.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "ERROR: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
        Finally
            myFile = Nothing
            fs = Nothing
        End Try

    End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接