我正在尝试使用EPPLUS将电子表格中的一些信息放入数据库中,然后将其导出到sql server。
我遇到的问题是,当我创建数据表列,然后开始插入行时,最终结果显示行信息和列标题之间的不对齐。
这是代码:
Sub ConvertPackage2DT()
Dim FILENAME As String = "C:\Data.xlsx"
Dim flInfo As New FileInfo(FILENAME)
_package = New ExcelPackage(flInfo) 'load the excel file
Dim strSheetName As String = "Radiology"
Dim Conn As SqlConnection = New SqlConnection("Data Source=dwsqlserver1\instance3;Initial Catalog=LocalDatasets_Dev;Integrated Security=True") 'connection to server end
Dim DTExport As New DataTable
If Not IsNothing(_package) Then
Dim _workbook As ExcelWorkbook = _package.Workbook
Dim _sheets As ExcelWorksheets = _workbook.Worksheets
Dim sh As ExcelWorksheet = _sheets.Item(strSheetName)
If _package.Workbook.Worksheets.Count <> 0 Then
For Each _sheet In _sheets
Dim SHEET_NAME As String = _sheet.Name
Dim tbl As New DataTable
Dim hasHeader = True ' adjust accordingly '
For ColNum = 1 To sh.Dimension.End.Column
tbl.Columns.Add(sh.Cells(1, ColNum).Value)
Next
System.Diagnostics.Debug.Print(tbl.Columns.Count)
For rowNum = 2 To sh.Dimension.End.Row
Dim v(sh.Dimension.End.Column - 1) As Object
For X = 0 To sh.Dimension.End.Column - 2
v(X) = sh.Cells(rowNum, X + 1).Value
Next
tbl.Rows.Add(v)
'tbl.Rows.Add(wsRow.Select(Function(cell) cell.Text).ToArray)
Next
Conn.Open()
Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(Conn)
bulkcopy.DestinationTableName = "[LocalDatasets_Dev].[dbo].[" & GetNewTableName(6) & "]"
bulkcopy.WriteToServer(tbl)
End Using
Conn.Close()
MsgBox("Complete")
Exit Sub
Next _sheet
End If
End If
End Sub
End Module
有人可以帮忙吗?谢谢你