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

VB.Net Excel Addin,如何按名称将数据写入特定工作表?,第1张

概述所以这就是我遇到的问题.我正在将一个旧的Excel宏转换为excel add in,这样我就可以更轻松地与我的同事分享.我是VB.net的新手,但我正在做我能做的事,所以请放轻松我. 我有一个允许用户输入数据的Windows表单,当他们点击输入数据按钮时,数据应该从表单转到特定的工作表.代码如下: Imports Excel = Microsoft.Office.Interop.ExcelPu 所以这就是我遇到的问题.我正在将一个旧的Excel宏转换为excel add in,这样我就可以更轻松地与我的同事分享.我是VB.net的新手,但我正在做我能做的事,所以请放轻松我.

我有一个允许用户输入数据的Windows表单,当他们点击输入数据按钮时,数据应该从表单转到特定的工作表.代码如下:

imports Excel = Microsoft.Office.Interop.ExcelPublic 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中打开窗体,但是当我输入一些数据并单击输入数据时会发生这种情况:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in Toms CutList Maker.dll but was not handled in user codeAdditional 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 Collection是Application Object的一部分.

试试这个:

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

以上是内存溢出为你收集整理的VB.Net Excel Addin,如何按名称将数据写入特定工作表?全部内容,希望文章能够帮你解决VB.Net Excel Addin,如何按名称将数据写入特定工作表?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1212732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存