繁体   English   中英

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

[英]Export datatable to Excel (VB window application)

所以这就是我尝试过的。 它成功导出,但没有数据。 空 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

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

arrays --> Visual Basic中的 Arrays 示例

因此,这是您尝试使用的代码的链接。 网络信息.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

在这里我只建议您的地址,您可以将范围“A1”更改为其他范围:

    'addrow (you can create function for this, with datatable parameter and range parameter to begin)
    Dim IncludeLabel As Boolean = True
    For xx = 0 To dtJelo.Rows.Count - 1
        For yy = 0 To dtJelo.Columns.Count - 1
            If IncludeLabel = True Then
               If xx = 0 Then 
                  xlWorkSheet.range("a1").Offset(xx,yy).value=dtJelo.Columns(yy).ColumnName 
               End If
               xlWorkSheet.range("a1").Offset(xx+1,yy).value=dtJelo.Rows(xx)(yy) 
            Else
               xlWorkSheet.range("a1").Offset(xx,yy).value=dtJelo.Rows(xx)(yy) 
            End If
        Next
    Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM