是否有人使用多个电子表格成功将数据传输到Excel? 我坚持了下来。我正在使用Visual Basic 2010。
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用此方法将数据导出到Excel文件:
Public Shared Function ExportDataTableToDataFile(ByVal srcDataTable As DataTable, ByVal dbTbName As String, _
ByVal dbFilePath As String, ByVal dbFileType As String, _
Optional ByVal schemaTable As DataTable = Nothing) As Boolean
Dim expConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFilePath
Select Case dbFileType
Case ".dbf"
If dbTbName.Length > 8 Then
dbTbName = dbTbName.Remove(8)
End If
expConnStr &= ";Extended Properties=dBase IV"
Case ".xls"
expConnStr &= dbTbName & ".xls;Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"""
Case ".mdb"
expConnStr &= dbTbName & ".mdb;User Id=admin;Password="
Case ".csv"
expConnStr &= ";Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"""
dbTbName &= ".csv"
Case ".mpp"
expConnStr = "Provider=Microsoft.Project.OLEDB.10.0;Project Name=" & dbFilePath & dbTbName & ".mpp"
Case Else
Return False
End Select
Dim res As Boolean = True
Dim createCmdStr As String = "CREATE TABLE [" & dbTbName & "] ("
Dim insertCmdStr As String = "INSERT INTO [" & dbTbName & "] ("
Dim insertParams As String = " VALUES ("
Dim paramPrefix As String = "@p_"
Dim oleDbCon As New OleDbConnection(expConnStr)
Dim oleDbDa As New OleDbDataAdapter
Dim createCmd As New OleDbCommand
Dim insertCmd As New OleDbCommand
Dim param As OleDbParameter
Dim oleDbColTp As OleDbType
Try
If String.IsNullOrEmpty(dbTbName) Or String.IsNullOrEmpty(dbFilePath) Or srcDataTable Is Nothing Then
res = False
Return res
End If
If Not System.IO.Directory.Exists(dbFilePath) Then
System.IO.Directory.CreateDirectory(dbFilePath)
End If
If System.IO.File.Exists(dbFilePath & dbTbName & dbFileType) Then
System.IO.File.Delete(dbFilePath & dbTbName & dbFileType)
End If
If dbFileType = ".mdb" Then
Dim ADOXCatalog As New ADOX.Catalog
Try
ADOXCatalog.Create(expConnStr)
Catch ex As System.Runtime.InteropServices.COMException
Catch ex As Exception
MessageBox.Show(ex.Source & ": " & ex.Message.ToString, "Chyba!")
Return False
Finally
ADOXCatalog = Nothing
End Try
End If
For Each col As DataColumn In srcDataTable.Columns
If col.DataType Is GetType(DateTime) Then
oleDbColTp = OleDbType.VarWChar
Else
If schemaTable IsNot Nothing Then
oleDbColTp = schemaTable.Rows(srcDataTable.Columns.IndexOf(col))(11)
Else
oleDbColTp = GetOleDbType(col.DataType)
End If
End If
createCmdStr &= "[" & col.ColumnName & "] " & GetSqlDbType(col.DataType).ToString
If col.DataType Is GetType(String) Then 'OleDb dovoluje max dlzku 255 pre VarChar
If col.MaxLength > 255 Then
col.MaxLength = 255
End If
createCmdStr &= " (" & col.MaxLength & ")"
End If
'createCmdStr &= IIf(col.AllowDBNull, "", " NOT NULL")
insertCmdStr &= "[" & col.ColumnName & "]"
insertParams &= "?"
param = insertCmd.Parameters.Add(paramPrefix & col.ColumnName, oleDbColTp, col.MaxLength, col.ColumnName)
param.SourceVersion = DataRowVersion.Current
If Array.IndexOf(srcDataTable.PrimaryKey, col) >= 0 Then
createCmdStr &= " PRIMARY KEY"
ElseIf col.Unique Then
createCmdStr &= " UNIQUE"
End If
If srcDataTable.Columns.IndexOf(col) < srcDataTable.Columns.Count - 1 Then
createCmdStr &= ", "
insertCmdStr &= ", "
insertParams &= ", "
Else
createCmdStr &= ")"
insertCmdStr &= ")"
insertParams &= ")"
End If
Next
insertCmdStr &= insertParams
createCmd.Connection = oleDbCon
createCmd.CommandText = createCmdStr
insertCmd.Connection = oleDbCon
insertCmd.CommandText = insertCmdStr
oleDbDa.InsertCommand = insertCmd
For Each drow As DataRow In srcDataTable.Rows
drow.AcceptChanges()
drow.SetAdded()
Next
If oleDbCon.State <> ConnectionState.Closed Then
oleDbCon.Close()
End If
oleDbCon.Open()
'createCmd.ExecuteNonQuery()
oleDbDa.Update(srcDataTable)
Catch ex As Exception
res = False
MessageBox.Show(ex.Message.ToString, "Chyba!")
Finally
oleDbCon.Close()
End Try
Return res
End Function