VB.net在这些文件夹中创建文件夹,子文件夹和Excel文件的更快方法

时间:2016-07-15 18:23:46

标签: sql sql-server vb.net excel

我正在使用VB 2010,SQL Server 2012 Excel 2010。

我公司的用户需要为医院和医生使用特定的Excel报告。

我在SQL Server中有一个表,其中包含某些数据,以及分配到该医院/医生办公室的人员的姓名,医院名称以及医院名称/医生办公室的地址。

文件夹可以具有以下结构:

C:\Test\assignee name\hospital name\hospital or doctor address\hospital or doctor address.xlsx

或者,医院名称可以为空白,结尾为以下结构:

C:\Test\assignee name\hospital or doctor address\hospital or doctor address.xlsx

以下是我到目前为止的代码,但只需4个小时即可获得这些受理人文件夹和报告中的4个。

我需要有26个受理人文件夹,3831个子文件夹,7281个文件。

我正在寻找更快的方法。

这是我的代码:

Private Sub Create_CQM_Folders_VB()

Private Sub Create_CQM_Folders_VB()
    Dim t1 As DateTime = DateTime.Now
    Dim t2 As DateTime
    Dim duration As TimeSpan        
    Dim connectionString As String = "server=SERVER1;Initial Catalog=DATABASE1;Integrated Security=SSPI"

    Using conn As New SqlConnection(connectionString)
        Dim command As New SqlCommand("SELECT distinct [Chase Assignment],[folder grouping],standardprovideraddress2 from SITES_Assigned", conn)
        Dim count As Integer = 1
        conn.Open()

        Dim myReader As SqlDataReader = command.ExecuteReader()
        //myReader = command.ExecuteReader
        While (myReader.Read())
            stringvar = Trim(myReader(0).ToString)
            stringvar2 = Trim(myReader(1).ToString)
            stringvar3 = Trim(myReader(2).ToString)

            If String.IsNullOrEmpty(stringvar2) Then
                MkDir("C:\test\" & stringvar & "\" & stringvar3)
                Call Create_Main_CQM_Report()

                If Val(xlAppSumm.Version) < 12 Then
                    //You use Excel 2000-2003
                    FileExtStr = ".xls" : FileFormatNum = -4143
                Else
                    //You use Excel 2007
                    FileExtStr = ".xlsx" : FileFormatNum = 51
                End If

                //save each of the reports to their corresponding paths

                Dim xlsxfilename As String
                xlsxfilename = stringvar3
                xlWorkBookSumm.SaveAs("C:\test\" & stringvar & "\" & stringvar3 _
                         & "\" & xlsxfilename & FileExtStr, FileFormat:=FileFormatNum)

                xlWorkBookSumm.Close()
                xlAppSumm.Quit()
                // End If
            Else
                MkDir("C:\test\" & stringvar & "\" & stringvar2 & "\" & stringvar3)
                Call Create_Main_CQM_Report()

                If Val(xlAppSumm.Version) < 12 Then
                    //You use Excel 2000-2003
                    FileExtStr = ".xls" : FileFormatNum = -4143
                Else
                    //You use Excel 2007
                    FileExtStr = ".xlsx" : FileFormatNum = 51
                End If

                //save each of the reports to their corresponding paths
                Dim xlsxfilename As String
                xlsxfilename = stringvar3

                xlWorkBookSumm.SaveAs("C:\test\" & stringvar & "\" & stringvar2 & "\" & stringvar3 _
                         & "\" & xlsxfilename & FileExtStr, FileFormat:=FileFormatNum)

                xlWorkBookSumm.Close()
                xlAppSumm.Quit()
                //End If
                count = count + 1
            End If
        End While
        conn.Close()
    End Using

    t2 = DateTime.Now
    duration = t2.Subtract(t1)

    //Get the difference and display it   
    MessageBox.Show(String.Format("Elapsed time:  {0} Hours, {1} Minutes, {2} Seconds", duration.Hours, duration.Minutes, duration.Seconds))
    MessageBox.Show("All reports have been created")
End Sub

Private Sub Create_Main_CQM_Report()

Private Sub Create_Main_CQM_Report()
    //show CQM report in datagridview
    Try
        Dim connString As String = "server=SERVER1;Initial Catalog=DATABASE1;Integrated Security=SSPI"
        Dim conn As New SqlConnection(connString)
        Dim dbComd As New SqlClient.SqlCommand("select * from RPT_ASSIGNED_REPORTS order by [chase assignment], [folder grouping], standardprovideraddress2", conn)

        conn.Open()
        dbComd.CommandType = CommandType.Text
        // dbComd.CommandTimeout = 900

        Dim adapters As New SqlDataAdapter(dbComd)
        Dim dset As New DataSet()
        adapters.Fill(dset)
        DataGridView1.DataSource = dset.Tables(0)
        DataGridView1.AllowUserToAddRows = False

        Dim dv As New DataView()
        dv = dset.Tables(0).DefaultView
        // get the DataView from a DataTable
        If String.IsNullOrEmpty(stringvar2) Then
            dv.RowFilter = "[chase assignment] = '" & stringvar & "' and standardprovideraddress2 = '" & stringvar3 & "'"
        Else
            dv.RowFilter = "[chase assignment] = '" & stringvar & "' and [folder grouping] = '" & stringvar2 & "' and standardprovideraddress2 = '" & stringvar3 & "'"
        End If

        conn.Close()

    Catch ex As Exception
        MessageBox.Show("Error while retrieving records from table..." & ex.Message, "Retrieve Records")
    Finally
        //System.Windows.MessageBox.Show("Records retrieved")
    End Try

    Dim misValue As Object = System.Reflection.Missing.Value
    Dim lastrow As Long
    Dim curr_date As String = DateTime.Now.ToString("MM.dd.yyyy HH.mm.ss")

    copyAlltoClipboard()
    xlAppSumm = New Excel.Application()
    xlWorkBookSumm = xlAppSumm.Workbooks.Add(misValue)
    xlWorkSheetSumm = xlWorkBookSumm.Sheets("Sheet1")
    xlWorkSheetSumm.Range("A1").PasteSpecial(Excel.XlPasteType.xlPasteAll)
    //xlWorkSheetSumm.Columns("A:A").Delete()
    xlAppSumm.Visible = False

    With xlWorkSheetSumm
        //save each of the reports to their corresponding paths
        .Columns("A:AN").EntireColumn.AutoFit()
    End With

    My.Computer.Clipboard.Clear()

    //Clear datagridview for next sheet
    Call Clear_DGV1()
End Sub

0 个答案:

没有答案