将数据表导出到Excel(VB窗口应用程序)

时间:2019-10-29 20:36:29

标签: vb.net winforms

这就是我尝试过的。它成功导出,但是没有数据。空的Excel。我想我需要添加一些代码来添加行和列,但是我不知道如何。我是新来的。

      Try
        'Exporting to Excel.
        Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim
        Dim filename As String = "JellyDetails" & current_yyyy & current_mon & ".xlsx"

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        'addrow

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        xlWorkSheet.SaveAs("C:\Users\spongebob\Desktop\" & filename & ".xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created")


    Catch ex As Exception

    End Try

2 个答案:

答案 0 :(得分:0)

我不确定您要在这里做什么,但这是创建一个excel文件并用数组中的一些数据填充单元格的示例。

数组示例-> Arrays in Visual Basic

因此,这里是您尝试使用的代码的链接。 Net-informations.com

您缺少将年份和日期作为字符串返回文件名称的函数。另外,您在使用的原始代码中缺少方法releaseObject()

测试代码

Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop

Namespace WinFormTestApp
    Partial Public Class Form1
        Inherits Form

        Public Sub New()

            Call LoadData()

        End Sub

        Private Sub LoadData()
            Try

                'Exporting to Excel.
                'Dont know where this dtJelo comes from, so I just assigned a folder variable below.
                'Dim folderPath As String = dtJelo.Rows(0)("jelly").ToString.Trim
                Dim folderPath As String = "C:\temp\"
                Dim filename As String = "JellyDetails" & current_yyyy() & current_mon() & ".xlsx"

                Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
                Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add
                Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)

                'Create an array to populate Excel with
                Dim myArray(5) As String

                myArray(0) = "This is line number 1"
                myArray(1) = "This is line number 2"
                myArray(2) = "This is line number 3"
                myArray(3) = "This is line number 4"
                myArray(4) = "This is line number 5"
                myArray(5) = "This is line number 6"


                'This is where they are adding the code.
                xlWorkSheet.Cells(1, 1) = "Column Header Text"

                For xx As Integer = 0 To UBound(myArray)
                    'Add 2 to xx so that you do not overwrite the Column Header
                    xlWorkSheet.Cells(xx + 2, 1) = myArray(xx)
                Next

                xlWorkBook.SaveAs($"{folderPath}{filename}", Excel.XlFileFormat.xlWorkbookDefault)

                xlWorkBook.Close(True)
                xlApp.Quit()

                releaseObject(xlWorkSheet)
                releaseObject(xlWorkBook)
                releaseObject(xlApp)

                'MsgBox("Excel file created")
                MessageBox.Show($"Excel file created , you can find the file at {folderPath}{filename}")

            Catch ex As Exception

            End Try
        End Sub

        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub

        Private Function current_mon() As String
            Try
                Return String.Format(Now.Month.ToString, "00")
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        Private Function current_yyyy() As String
            Try
                Return Now.Year.ToString
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
    End Class
End Namespace

他们的代码

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()

        If xlApp Is Nothing Then
            MessageBox.Show("Excel is not properly installed!!")
            Return
        End If


        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "Sheet 1 content"

        xlWorkBook.SaveAs("d:\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
        xlWorkBook.Close(True, misValue, misValue)
        xlApp.Quit()

        releaseObject(xlWorkSheet)
        releaseObject(xlWorkBook)
        releaseObject(xlApp)

        MessageBox.Show("Excel file created, you can find the file d:\csharp-Excel.xls")
    End Sub

    'YOU WERE MISSING THIS FUNCTION IN YOUR EXAMPLE
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

答案 1 :(得分:0)

在这里,我仅建议您进行增加,您可以将范围“ A1”更改为其他范围:

 anychart.onDocumentReady(function() {
          // create data set on our data
          var dataSet = anychart.data.set([
            ['Nail polish', 12814, 3054, 4376, 4229],
            ['Eyebrow pencil', 13012, 5067, 3987, 3932],
            ['Rouge', 11624, 7004, 3574, 5221],
            ['Lipstick', 8814, 9054, 4376, 9256],
            ['Eyeshadows', 12998, 12043, 4572, 3308],
            ['Eyeliner', 12321, 15067, 3417, 5432],
            ['Foundation', 10342, 10119, 5231, 13701],
            ['Lip gloss', 22998, 12043, 4572, 4008],
            ['Mascara', 11261, 10419, 6134, 18712]
          ]);