在没有安装office的情况下在.net中创建excel文件

发布于 2024-10-10 00:52:44 字数 167 浏览 0 评论 0原文

我需要在未安装 Microsoft Office 的生产服务器 (Windows Server 2008) 上使用 .net 创建 Excel 文件(包含多个工作表)。 借助VS2010和Windows Server 2008可以实现这一点吗? 或者有什么方法可以通过使用 Office Web 应用程序来实现这一目标。

I require to create excel files (with multiple sheets) using .net on a production server (Windows Server 2008) where Microsoft Office is not installed.
Can this be fulfilled with the help of VS2010 and Windows Server 2008.
or is there any way to achieve this by using office webapps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半葬歌 2024-10-17 00:52:44

我使用 OpenXML SDK 就是为了这个目的。它不允许您在服务器环境中实际计算和运行函数,但它非常适合创建和操作工作簿。

下载:

http:// /www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

文档:

http://msdn.microsoft.com/en-us/library/bb448854.aspx

I use the OpenXML SDK which is intended for that purpose. It doesn't allow you to actually calculate and run functions in a server environment but its very good for creating and manipulating workbooks.

Download:

http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

Documentation:

http://msdn.microsoft.com/en-us/library/bb448854.aspx

遗失的美好 2024-10-17 00:52:44

您可以使用此库:http://epplus.codeplex.com/releases/view/42439

它跳过 Excel 二进制文件的使用,转而使用 Excel XML 格式,因此编写 Excel 可以正确理解和呈现的纯文本、简单文本。

You can use this library: http://epplus.codeplex.com/releases/view/42439

It skips use of Excel binaries, in favor of the Excel XML format, therefore writing plain, simple text which Excel can understand and render properly.

情徒 2024-10-17 00:52:44

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

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

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

If the target is office 2007 or 2010, you can try with this free library http://epplus.codeplex.com/

If the target is <= to 2003 you can try with GemBox.Spreadsheet

The free version is limited to :
Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

高跟鞋的旋律 2024-10-17 00:52:44

使用官方的 Microsoft OpenXML SDK。

Use the official Microsoft OpenXML SDK.

累赘 2024-10-17 00:52:44

试试这个:

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

Try this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文