【发布时间】:2011-07-12 14:57:16
【问题描述】:
在一个创建 excel 文件的程序中,我想知道我是否可以让没有 MS Office 的用户有机会创建一个仅安装了 LibreOffice 的 .xls 文件。我应该使用什么而不是“使用 Excel = Microsoft.office.interlope.excel;”和其余的命令?天呐!
【问题讨论】:
标签: c# .net excel libreoffice libreoffice-calc
在一个创建 excel 文件的程序中,我想知道我是否可以让没有 MS Office 的用户有机会创建一个仅安装了 LibreOffice 的 .xls 文件。我应该使用什么而不是“使用 Excel = Microsoft.office.interlope.excel;”和其余的命令?天呐!
【问题讨论】:
标签: c# .net excel libreoffice libreoffice-calc
LibreOffice 使用 ODF(开放文档格式)。 ODF 不是一种难以掌握的格式,因为它基本上是一组 XML 文件,这些文件被压缩到一个文件中,称为 ODF 文件。您可以read here 了解如何读取和保存 ODF 文件。也可以check here for a real example in C#
【讨论】:
如果您安装了 LibreOffice,请查找 cli_basetypes.dll、cli_cppuhelper.dll、cli_oootypes.dll、cli_uno.dll、cli_ure.dll、cli_uretypes.dll,然后添加对您项目的引用,它必须对您有用,我也安装了“适用于 Word、Excel 和 PowerPoint 文件格式的 Microsoft Office 兼容包”和“Microsoft Access Database Engine 2010 Redistributable”(无需完成 Office 安装即可获得 ACE.OLEDB.12.O 连接)。这是 VB Sample 的一部分,我在其中连接到 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
【讨论】: