[英]Data Table to Excel spreadsheet
I'm trying to pass a data table into my sub procedure. 我正在尝试将数据表传递到子过程中。 I am getting an error and am unsure why I am getting the error. 我收到错误,不确定为什么收到错误。 Is it something with my column/row indexes being set to 0? 我的列/行索引设置为0是否有问题?
Line 67 in my code is.... 我的代码中的第67行是...
wBook.SaveAs(strFName) 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
Ok, shortly after I posted this I realized what I did. 好的,发布此消息后不久,我意识到自己做了什么。 tdpInvoiceDate.Text contained characters that weren't correct for a filename. tdpInvoiceDate.Text包含与文件名不正确的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.