VB.net循环保存多个Excel文件

我正在使用以下代码将值写入数据表,然后将其导出到Excel。

我现在想要实现的是修改代码,以便为每个唯一客户保存一个单独的Excel文件([Customer Lookup])

这是我的出口代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Initialize the objects before use Dim dataAdapter As New SqlClient.SqlDataAdapter() Dim dataSet As New DataSet Dim command As New SqlClient.SqlCommand Dim datatableMain As New System.Data.DataTable() Dim connection As New SqlClient.SqlConnection 'Assign your connection string to connection object connection.ConnectionString = "server=inlt01\SQLEXPRESS; " + "database=DaisyBilling; " + "integrated security=yes" command.Connection = connection command.CommandType = CommandType.Text 'You can use any command select command.CommandText = "SELECT [CustomerCLI], [Customer Lookup], " + "ROUND (SUM ([SalesPrice]),2) as [Sum of Buy Price], " + "ROUND (SUM ([Sell Price]),2) as [Sum of Sell Price], " + "[Tariff Lookup] FROM [DaisyBilling].[dbo].[" + DaisyBillingForm.TextBox1.Text + "] GROUP BY [CustomerCLI], [Customer Lookup], " + "[Tariff Lookup] Order by [Customer Lookup]" dataAdapter.SelectCommand = command Dim f As FolderBrowserDialog = New FolderBrowserDialog Try If f.ShowDialog() = DialogResult.OK Then 'This section help you if your language is not English. System.Threading.Thread.CurrentThread.CurrentCulture = _ System.Globalization.CultureInfo.CreateSpecificCulture("en-US") Dim oExcel As Excel.Application Dim oBook As Excel.Workbook Dim oSheet As Excel.Worksheet oExcel = CreateObject("Excel.Application") oBook = oExcel.Workbooks.Add(Type.Missing) oSheet = oBook.Worksheets(1) Dim dc As System.Data.DataColumn Dim dr As System.Data.DataRow Dim colIndex As Integer = 0 Dim rowIndex As Integer = 0 'Fill data to datatable connection.Open() dataAdapter.Fill(datatableMain) connection.Close() 'Export the Columns to excel file For Each dc In datatableMain.Columns colIndex = colIndex + 1 oSheet.Cells(1, colIndex) = dc.ColumnName Next 'Export the rows to excel file For Each dr In datatableMain.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each dc In datatableMain.Columns colIndex = colIndex + 1 oSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName).ToString() Next Next 'Set final path Dim fileName As String = "\" + DaisyBillingForm.TextBox1.Text + "_" + DateTime.Now.ToString("ddMMyyyy") & "_" & DateTime.Now.ToString("HHmmss") + ".xls" Dim finalPath = f.SelectedPath + fileName 'TextBox1.Text = finalPath oSheet.Columns.AutoFit() 'Save file in final path oBook.SaveAs(finalPath, XlFileFormat.xlWorkbookNormal, Type.Missing, _ Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _ Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) 'Release the objects ReleaseObject(oSheet) oBook.Close(False, Type.Missing, Type.Missing) ReleaseObject(oBook) oExcel.Quit() ReleaseObject(oExcel) 'Some time Office application does not quit after automation: 'so i am calling GC.Collect method. GC.Collect() MessageBox.Show("Export done successfully!") End If Catch ex As Exception MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK) End Try End Sub 

我如何创build一个循环,并运行这个代码每个独特的客户,并保存每个单独的文件?

我比较新的VB,任何帮助极大的赞赏。

谢谢,