vb.net中从SQL数据表生成Excel文档
在我的项目中,我有一个汇总数据表,我想将其导出到 Excel 文档中,为此我部署了以下代码:
Public Shared Function Main() As Integer
Dim exc As New Application
exc.Visible = True
Dim workbooks As Excel.Workbooks = exc.Workbooks
Dim workbook As Excel._Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)
Dim sheets As Sheets = workbook.Worksheets
Dim worksheet As _Worksheet = CType(sheets.Item(1), _Worksheet)
exc.DataEntryMode = True
Dim range1 As Range = worksheet.Range("C1", Missing.Value)
Const nCells As Integer = 5
Dim args1(1) As [Object]
args1(0) = nCells
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1)
Return 100
End Function
当我尝试执行该行时:
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1)
我收到以下错误: 调用目标已引发异常
此时我不知道问题出在哪里。有人可以帮助我吗?
In my project I have a summary data table which i want to export in Excel document, for this purpose i have deployed the following code:
Public Shared Function Main() As Integer
Dim exc As New Application
exc.Visible = True
Dim workbooks As Excel.Workbooks = exc.Workbooks
Dim workbook As Excel._Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)
Dim sheets As Sheets = workbook.Worksheets
Dim worksheet As _Worksheet = CType(sheets.Item(1), _Worksheet)
exc.DataEntryMode = True
Dim range1 As Range = worksheet.Range("C1", Missing.Value)
Const nCells As Integer = 5
Dim args1(1) As [Object]
args1(0) = nCells
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1)
Return 100
End Function
When I tried to execute the line:
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, range1, args1)
I get the error of :Exception has been thrown by the target of an invocation
at this point I can't figured out what is the problem. Is there anybody to assist me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要担心尝试使用调用成员。
要将一系列值填充到 Excel 文档中,您可以使用类似的方法
尝试从在 Excel 内部执行此操作的角度来查看它,然后遵循相同的模式。
编辑
对于文本对齐选项,例如自动换行等。
Don't worry about trying to use invoke member.
To populate a range of values into an Excel documant you can use something like this
Try looking at it from the point of view of doing this inside Excel and then following the same pattern.
EDIT
For Text alignement options like word wrap etc.