1

我正在尝试将数据表传递到我的子过程中。我收到错误消息,不确定为什么会收到错误消息。是不是我的列/行索引设置为 0?

我的代码中的第 67 行是....

wBook.SaveAs(strFName)

在此处输入图像描述

Private Sub DataTableToExcel(ByVal dtTemp As DataTable)
        Try
            Dim _excel As New Microsoft.Office.Interop.Excel.Application
            Dim wBook As Microsoft.Office.Interop.Excel.Workbook
            Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

            wBook = _excel.Workbooks.Add()
            wSheet = wBook.ActiveSheet()

            Dim dt As System.Data.DataTable = dtTemp
            Dim dc As System.Data.DataColumn
            Dim dr As System.Data.DataRow
            Dim cIndex As Integer = 0
            Dim rIndex As Integer = 0

            For Each dc In dt.Columns
                cIndex = cIndex + 1
                _excel.Cells(1, cIndex) = dc.ColumnName
            Next

            For Each dr In dt.Rows
                rIndex = rIndex + 1
                cIndex = 0
                For Each dc In dt.Columns
                    cIndex = cIndex + 1
                    _excel.Cells(rIndex + 1, cIndex) = dr(dc.ColumnName)
                Next
            Next

            wSheet.Columns.AutoFit()
            Dim strFName As String = "H:\RawDataReport\" & txtGPSystemNum.Text & dtpInvoiceDate.Text & _
                                        ".xlsx"
            If System.IO.File.Exists(strFName) Then
                System.IO.File.Delete(strFName)
            End If

            wBook.SaveAs(strFName)
            wBook.Close()
            _excel.Quit()

        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
4

1 回答 1

0

好的,在我发布此消息后不久,我意识到我做了什么。tdpInvoiceDate.Text 包含不适合文件名的字符。

于 2013-09-05T17:59:47.440 回答