我正在使用visual basic(visual studio 2008)构建一个windows窗体应用程序。如何从未安装Office的winform vb.net 2008导出为excel?
这个想法是查询MySQL数据库并将结果导出到Excel文档。
我管理这个使用此代码(我将只显示导出到Excel部分)做:
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Imports System.Data
Imports MySql.Data.MySqlClient
Imports System.Configuration
Imports System.Runtime.InteropServices
Private Sub btn_getReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_getReport.Click
Dim wb As Excel.Workbook
Dim ex As New Excel.Application
wb = ex.Workbooks.Add(System.Reflection.Missing.Value)
Dim sheet As Excel.Worksheet = CType(wb.Worksheets.Add, Excel.Worksheet)
sheet.Name = "algo"
Dim i As Integer = 1
Try
While reader.Read
sheet.Cells(i, 1) = CStr(reader.Item(0))
sheet.Cells(i, 2) = CStr(reader.Item(1))
sheet.Cells(i, 3) = CStr(reader.Item(2))
i += 1
End While
Catch MyEx As MySqlException
RaiseEvent MySqlError(Err, MyEx, "read")
Catch exc As Exception
RaiseEvent MySqlError(Err, exc, "read")
End Try
Dim dialog As New SaveFileDialog
Dim result As DialogResult = dialog.ShowDialog
Try
wb.SaveAs(dialog.FileName)
Catch exerr As Exception
End Try
'Show the spreadsheet.
'ex.Visible = True
'wb.Activate()
End Sub
,它工作正常在我的笔记本电脑(其中有安装Office 2003),但是当我创建安装程序包和我要去的地方使用它(它没有安装Office)在服务器上安装它,我得到这个错误:
"Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154."
对于我已阅读,这是在尝试将问题当它没有出现在计算机上时使用excel,我可以理解,真正让我困惑的是这个我曾经使用应用程序导出信息,即使在计算机上运行也无需安装Office,他们如何才能做到这一点?
而为了记录,我需要excel文件,而不是CSV。
非常感谢。
的一些想法见这个问题: how-can-i-programatically-create-read-write-an-excel-without-having-office-inst – Crispy