如何从vb.net将函数中的数组添加到Excel? [英] How do i add an array from a function into excel from vb.net?

查看:115
本文介绍了如何从vb.net将函数中的数组添加到Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

‘my function to make an array
Public Function TheArray() As Array
        Dim count As Integer = ListBox1.Items.Count
        Dim MyArray(count, 5) As Integer
        For i As Integer = 0 To count - 1
           
            MyArray(i, 0) = ListBox1.Items(i).ToString
            MyArray(i, 1) = ListBox2.Items(i).ToString
            MyArray(i, 2) = ListBox3.Items(i).ToString
            MyArray(i, 3) = ListBox4.Items(i).ToString
            MyArray(i, 4) = ListBox5.Items(i).ToString

        Next
Return MyArray

    End Function


‘my code I have used so far to create export array from function to excel
Public Class Form5
    Dim oExcel As Object
        Dim oBook As Object
    '
    Dim oSheet As Object
    Dim filename As String

    Public Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        

        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)


        oSheet.Range("A2").Value = ("Generation number")
        oSheet.Range("B2").Value = ("Juvenile Population")
        oSheet.Range("C2").Value = ("Adult Population")
        oSheet.Range("D2").Value = ("Senile Population")
        oSheet.Range("E2").Value = ("Total")
        


        Dim count As Integer = Form4.ListBox1.Items.Count
         Dim array1(count, 5) As Integer

        array1 = Form4.TheArray()

        For row = 0 To count - 1

            For column = 0 To 4
                
                oSheet.Range("A3:D3").value = array1(row, column)

            Next
        Next
‘I hope to display values that my array contains which is according to the values entered by the user so I don’t know the number of rows-yet there are 5 columns


推荐答案

尝试一下:

Dim

    Dim array1 = Form4.TheArray()

Dim

    Dim c1 = oSheet.Cells(1, 3)

Dim

    Dim c2 = oSheet.Cells(5, 3 + count)

Dim

    Dim r = oSheet.Range(c1, c2)

r.Value2 = array1

    r.Value2 = array1


这篇关于如何从vb.net将函数中的数组添加到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆