VB.Net的Excel插件,如何将数据写入特定的工作表名称?

所以这是我的问题。 我正在将一个旧的Excelmacros转换成一个Excel添加,所以我可以更容易地与我的同事分享。 我是VB.net的新手,但我正在尽我所能,所以请容易对我。

我有一个Windows窗体,允许用户input数据,当他们击中input数据button时,数据应该从表单形成一个特定的工作表。 代码如下:

Imports Excel = Microsoft.Office.Interop.Excel Public Class Form_CutListEntry Dim xApp As New Excel.Application Dim wss As Microsoft.Office.Tools.Excel.Worksheet Private Sub Btn_InsertJobInfo_Click(sender As Object, e As EventArgs) Handles Btn_InsertJobInfo.Click wss = xApp.Worksheets("Job Info") 'Check that all data is entered If Trim(TxtBx_CustomerName.Text) = "" Then TxtBx_CustomerName.Focus() MsgBox("Please enter a Customer Name") Exit Sub End If If Trim(TxtBx_OrderNum.Text) = "" Then TxtBx_OrderNum.Focus() MsgBox("Please enter an Order Number") Exit Sub End If If Trim(TxtBx_CutlistAuthor.Text) = "" Then TxtBx_CutlistAuthor.Focus() MsgBox("Please enter your initials") Exit Sub End If 'Write data to excel worksheet. wss.Cells(3, 1) = "Customer Name: " + TxtBx_CustomerName.Text wss.Cells(4, 1) = "Order Number: " + TxtBx_OrderNum.Text wss.Cells(5, 1) = "Todays Date: " + TxtBx_TodaysDate.Text wss.Cells(6, 1) = "Cutting List Prepared By: " + TxtBx_CutlistAuthor.Text Exit Sub End Sub 

(注意我拿出了意见和一些额外的部分是不相干的,所以下面详细的错误信息有错误的行号)

我可以从Excel中打开窗体,但是当我input一些数据并点击input数据时,会发生这种情况:

 An exception of type 'System.Runtime.InteropServices.COMException' occurred in Toms CutList Maker.dll but was not handled in user code Additional information: Exception from HRESULT: 0x800A03EC 

在这一行上:

 wss = xApp.Worksheets("Job Info") 

任何人都可以用这个指向我的写作方向吗?

如果有人感兴趣,这里是完整的错误细节:

  System.Runtime.InteropServices.COMException was unhandled by user code ErrorCode=-2146827284 HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source=Microsoft.Office.Interop.Excel StackTrace: at Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets() at Toms_CutList_Maker.Form_CutListEntry.Btn_InsertJobInfo_Click(Object sender, EventArgs e) in d:\tom\documents\visual studio 2013\Projects\Toms CutList Maker\Toms CutList Maker\CutList Entry.vb:line 15 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: 

您不能直接从Excel应用程序中引用工作表。 工作表集合是Workbook对象的一部分。 Workbooks集合是应用程序对象的一部分。

试试这个:

 Dim xApp As New Excel.Application Dim wss As Microsoft.Office.Tools.Excel.Worksheet Dim wb As Microsoft.Office.Tools.Excel.Workbook .... wb = xApp.Workbooks("My Workbook.xlsx") 'replace with your workbook name and proper file extension wss = wb.Worksheets("Job Info") 

你直接从Excel中得到一个错误。 我做了同样的事情,这是我得到的:

 Dim xlWorkBook As Excel.Workbook = xlapp.Workbooks.Add(Application.StartupPath + "\My Workbook.xlsx") xlWorkBook.Sheets.Add("Job Info") Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Sheets("JobInfo") 

确保您试图打开的工作簿存在。