有没有办法像使用 C# 的 MS Office Excel 一样创建/读取 LibreOffice 电子表格?

发布于 2024-11-19 13:26:24 字数 151 浏览 1 评论 0原文

在创建 Excel 文件的程序中,我想知道是否可以为没有 MS Office 的用户提供仅安装 LibreOffice 来创建 .xls 文件的机会。我应该使用什么来代替“using Excel = Microsoft.office.interlope.excel;”和其余的命令?天啊!

In a program that creates an excel file, i wonder if i could give the chance to the users that don't have MS Office to create an .xls file with only LibreOffice installed. What should i use instead of "using Excel = Microsoft.office.interlope.excel;" and the rest of the commands ? TnX!

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

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

发布评论

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

评论(2

怀里藏娇 2024-11-26 13:26:24

LibreOffice 使用 ODF(开放文档格式)。 ODF 并不是一种难以掌握的格式,因为它基本上只是压缩成一个文件(称为 ODF 文件)的 XML 文件的集合。您可以阅读此处了解如何读取和保存 ODF 文件。另外,您可以在此处查看 C# 的真实示例

LibreOffice uses the ODF (Open Document Format). ODF is not a hard format to grasp because all it basically is is a collection of XML files that are zipped into one file, called an ODF file. You can read here on how to read and save ODF files. Also, you can check here for a real example in C#

妖妓 2024-11-26 13:26:24

如果您安装了 LibreOffice,请查找 cli_basetypes.dll、cli_cppuhelper.dll、cli_oootypes.dll、cli_uno.dll、cli_ure.dll、cli_uretypes.dll,然后添加对您的项目的引用,它一定适合您,我还安装了“Microsoft Office Compatibility” Pack for Word、Excel 和 PowerPoint 文件格式”和“Microsoft Access Database Engine 2010 Redistributable”(获取ACE.OLEDB.12.O 连接,无需完成 Office 安装)。这是 VB 示例的一部分,其中我连接到 oledb 以创建一些查询。

    OpenFileDialog.Filter = "Spreadsheets (*.xls*)|*.xls*"
    OpenFileDialog.Multiselect = False
    Try
        If (OpenFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            objOffice = CreateObject("com.sun.star.ServiceManager") 'preparar instancia libreOffice (prepare libreOffice instance)
            instOffice = objOffice.createInstance("com.sun.star.frame.Desktop")
            Dim obj(-1) As Object
            Dim myDoc = instOffice.loadComponentFromURL("file:///" & OpenFileDialog.FileName.Replace("\", "/"), "_default", 0, obj)
            Dim hojas = myDoc.getSheets().getElementNames() 'Obtener nombres de las hojas de calculo (get Spreadsheet names)
            System.Threading.Thread.Sleep(1000) 'Esperar a que termine la instancia Office (await libreOffice thread)
            myDoc.Close(True)

            Dim MyConnection As System.Data.OleDb.OleDbConnection 'Preparar conexión para realizar consulta tipo sql (preparing connection)
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            If OpenFileDialog.FileName.ToUpper.Contains(".XLSX") Then
                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;'")
            Else
                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'")
            End If

If you had installed LibreOffice look for cli_basetypes.dll, cli_cppuhelper.dll, cli_oootypes.dll, cli_uno.dll, cli_ure.dll, cli_uretypes.dll then add references to your project, it must work for you, i also installed "Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint File Formats" and "Microsoft Access Database Engine 2010 Redistributable" (to get ACE.OLEDB.12.O connection without complete Office installation). This is a part of VB Sample in which i got connection to oledb to create some queries.

    OpenFileDialog.Filter = "Spreadsheets (*.xls*)|*.xls*"
    OpenFileDialog.Multiselect = False
    Try
        If (OpenFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            objOffice = CreateObject("com.sun.star.ServiceManager") 'preparar instancia libreOffice (prepare libreOffice instance)
            instOffice = objOffice.createInstance("com.sun.star.frame.Desktop")
            Dim obj(-1) As Object
            Dim myDoc = instOffice.loadComponentFromURL("file:///" & OpenFileDialog.FileName.Replace("\", "/"), "_default", 0, obj)
            Dim hojas = myDoc.getSheets().getElementNames() 'Obtener nombres de las hojas de calculo (get Spreadsheet names)
            System.Threading.Thread.Sleep(1000) 'Esperar a que termine la instancia Office (await libreOffice thread)
            myDoc.Close(True)

            Dim MyConnection As System.Data.OleDb.OleDbConnection 'Preparar conexión para realizar consulta tipo sql (preparing connection)
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            If OpenFileDialog.FileName.ToUpper.Contains(".XLSX") Then
                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;'")
            Else
                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & OpenFileDialog.FileName & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'")
            End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文