2012-01-27 166 views
0

是否有人使用多个电子表格成功地将数据传输到Excel中? 我一直坚持下去。我正在使用Visual Basic 2010.使用多个电子表格将数据导出到Excel中

+2

是,使用[EPPlus(HTTP:// epplus .codeplex.com /释放/视图/ 42439)。它就像'Dim ws = package.Workbook.Worksheets.Add(“Name”)一样简单' – 2012-01-27 19:16:10

+0

如果您需要帮助,您应该提供更多详细信息。我在您的“代码”中看不到任何问题;-) – 2012-01-27 19:21:25

+0

谢谢蒂姆!现在我准备好了。 :)那么问题是,我没有任何代码。下载了一段代码,然后生成excel xml,但是这样做太单调乏味了。 EPPlus看起来更有希望。感谢您的帮助 - 将使用这个作为我的最终工具。 – 2012-01-28 04:25:43

回答

0

是的,使用EPPlus。它是那样简单:

Dim ws = package.Workbook.Worksheets.Add("Name") 

感谢蒂姆:)

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 
相关问题