加快数据提取到Excel

我目前使用VBA来运行一个存储过程和存储的数据已提取到表。 然后,VBA将相应地查询数据中的所有数据,并将其放入excel。

在这个问题上,VBA需要很长时间才能将所有数据(大约10万行数据)提取到excel中。 还有其他的方法来加快这个过程吗? 以下是我的代码的一部分。 粗体字是插入到Excel代码。

'Row number where data inserting starts Do current_sheet = owb.ActiveSheet With current_sheet 'Insert header to worksheet in first row, ie. A1, B1, C1 For i = 0 To data_cols.GetLength(0) - 1 cell = data_cols(i, 0) & header_row_num 'Change to header_row_num .Range(cell).Value = data_cols(i, 1) Next i End With row_count = header_row_num + 1 'Change the first row count to a row after header_row_num 'Insert data to worksheet While rs.EOF = False With current_sheet 'Set format of specic columns before inserting data .Columns("A").NumberFormat = "@" .Columns("B").NumberFormat = "@" .Columns("C").NumberFormat = "@" .Columns("D").NumberFormat = "@" .Columns("E").NumberFormat = "@" .Columns("F").NumberFormat = "@" .Columns("G").NumberFormat = "@" .Columns("H").NumberFormat = "@" .Columns("I").NumberFormat = "@" .Columns("J").NumberFormat = "@" .Columns("K").NumberFormat = "@" .Columns("L").NumberFormat = "@" .Columns("M").NumberFormat = "@" .Columns("N").NumberFormat = "@" .Columns("O").NumberFormat = "@" .Columns("P").NumberFormat = "@" .Columns("Q").NumberFormat = "@" .Columns("R").NumberFormat = "@" .Columns("S").NumberFormat = "@" **'Start inserting data For i = 0 To data_cols.GetLength(0) - 1 'Get the cell name cell = data_cols(i, 0) & row_count 'Populate data to the cell If IsDBNull(rs.Fields(data_cols(i, 2)).Value()) Then .Range(cell).Value = " " Else .Range(cell).Value = rs.Fields(data_cols(i, 2)).Value() End If Next i End With rs.MoveNext() 'Indicates next row row_count += 1** If row_count > 60000 Then owb.Worksheets.Add(, current_sheet) need_new_sheet = True Console.WriteLine("Added new sheet to workbook...") Exit While Else need_new_sheet = False End If End While Loop While (need_new_sheet And rs.EOF = False) 

如果你需要知道某个variables。

 row_count = header_row_num + 1 'Change the first row count to a row after header_row_num oxl = CreateObject("Excel.Application") oxl.Visible = False owb = oxl.Workbooks.Add Dim data_cols(,) As String = {{"A", "Name", "NAME"}, _ {"B", "Age", "AGE"}} (Not real columns, example) 

任何意见或想法将不胜感激。 提前致谢 :)

在Excel中填写10万行肯定需要时间。

这是你可以做的,以尽量减less时间

  1. 在macros的开始处使用oxl.ScreenUpdating = False ,最后将其设置为True
  2. 您可能希望将数据存储在数组中,然后一次性将数组写入Excel。 这肯定会减less执行时间
  3. Excel 2007以后,Excel有1048576行的行限制,所以如果你超过这个限制,你可能会考虑到这一点。
  4. Console.WriteLine("Added new sheet to workbook...")是VB.net。 如果您使用的是VBA,请使用Debug.print
  5. 顺便说一句,这将不会对速度有明显的影响,但你可以写下面的代码

哪个是

 .Columns("A").NumberFormat = "@" .Columns("B").NumberFormat = "@" ' ' ' .Columns("R").NumberFormat = "@" .Columns("S").NumberFormat = "@" 

 .Columns("A:S").NumberFormat = "@" 

最快的方法是CopyFromRecordset

来自MSDN:


 expression.CopyFromRecordset(Data, MaxRows, MaxColumns) 

expression :必需。 一个返回一个Range对象的expression式。

Data :必需的变体。 将Recordset对象的名称复制到范围中。

MaxRows :可选变体。 要复制到工作表上的最大logging数。 如果省略此参数,则复制Recordset对象中的所有logging。

MaxColumns :可选变体。 要复制到工作表上的最大字段数。 如果省略此参数,则复制Recordset对象中的所有字段。


例如,在你的情况下只需input:

 Range("A2").CopyFromRecordset rs