如何使用VB.Net中创建的Windows服务创建和保存Excel文件 [英] How to create and save Excel file using windows service created in VB.Net
问题描述
VB.Net新手。我创建了一个Windows服务。我的简单目标是创建一个Excel文件并将其保存到我的本地驱动器。我使用了以下代码。
Hi,
Am new to VB.Net. I have created a windows service. Where my simple goal is to create an Excel file and save it to my local drive. I have used the following code.
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
'Start a new workbook in Excel.
'Add data to cells of the first worksheet in the new workbook.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Doe"
oSheet.Range("B2").Value = "John"
oBook.SaveAs(strFileName)
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
现在这个 CreateObject 抛出异常无法创建ActiveX组件。我添加了Microsoft Excel& amp;办公室在我的项目中。
知道我做错了什么或应该做什么?可以从Windows服务创建和保存excel文件吗?
PS - 我在Windows 7 64位机器上使用vs2010,office 2010.
Now this CreateObject throwing exception "Cannot create ActiveX component." I have added reference of Microsoft Excel & Office in my project.
Any idea what am doing wrong or what should be done? It is possible to create and save excel file from windows service right?
P.S - Am on windows 7 64 bit machine and using vs2010, office 2010.
推荐答案
只要Excel在运行该服务的盒子上,就有可能,虽然我在StackExchange上听说互操作不能作为服务器应用程序
看看这篇文章 [ ^ ]来自Microsoft
或非常好替代方法 [ ^ ]来自我们自己的CodeProject,可能是b这是服务所需的解决方案
It may be possible as long as Excel is on the box that the service is running on, although I''ve heard on StackExchange that interop doesn''t work as a server application
Have a look at this article[^] from Microsoft
or a very good alternative approach [^] from our very own CodeProject which may be the very solution you need for a service
这篇关于如何使用VB.Net中创建的Windows服务创建和保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!