vb.net中从SQL数据表生成Excel文档

发布于 2024-10-31 15:26:26 字数 975 浏览 1 评论 0原文

在我的项目中,我有一个汇总数据表,我想将其导出到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-11-07 15:26:27

不要担心尝试使用调用成员。

要将一系列值填充到 Excel 文档中,您可以使用类似的方法

dim row as integer = 1
dim column as integer = 1


Worksheet.Cells(row,column) = "My value here"
'or this
Worksheet.Cells("A1").Value = "my Value Here"
' or this
Worksheet.Range("A1") = "My value here"

尝试从在 Excel 内部执行此操作的角度来查看它,然后遵循相同的模式。

编辑
对于文本对齐选项,例如自动换行等。

dim rng as Range = Worksheet.Range("A1")
With rng
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
end With

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

dim row as integer = 1
dim column as integer = 1


Worksheet.Cells(row,column) = "My value here"
'or this
Worksheet.Cells("A1").Value = "my Value Here"
' or this
Worksheet.Range("A1") = "My value here"

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.

dim rng as Range = Worksheet.Range("A1")
With rng
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
end With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文