生成excel报告时Vb.net连接超时

时间:2014-02-03 05:31:39

标签: vb.net excel windows-8

我正在尝试使用MSSQL中的数据生成excel报告。它完全适用于我的电脑(服务器)和所有客户端,除了安装了Windows 8的电脑(安装了MS Office 2010)。我在WinxP / win7客户端PC上测试了该程序,两者都能够生成安装的excel报告(MS Office 2007)。问题可能是MS Office的不同版本吗?这是我的代码示例。再次,它在我的服务器PC和其他客户端PC上工作得很好,除了win8机器。

    Private Sub Report_JSIADGTAMKOR()
    On Error Resume Next
    Dim consigneeName As String
    If cmboAccount.Text = "AMKOR" Then
        consigneeName = "AMKOR"
    ElseIf cmboAccount.Text = "JSI-CIRTEK" Then
        consigneeName = "JSI-CIRTEK"
    ElseIf cmboAccount.Text = "JSI-LATTICE" Then
        consigneeName = "JSI-LATTICE"
    Else
        consigneeName = "ANALOG DEVICES GEN. TRIAS - JSI"
    End If

    Wbook = createExcel.Workbooks.Add
    Wsheet = Wbook.Worksheets(1)

    If Format(dteFrom.Value, "MM-dd-yyyy") = Format(dteTo.Value, "MM-dd-yyyy") Then
        DateRange = Format(dteTo.Value, "MM-dd-yyyy")
    Else
        DateRange = Format(dteFrom.Value, "MM-dd") & " To " & Format(dteTo.Value, "MM-dd-yy")
    End If

    Wbook.Worksheets(1).Name = cmboAccount.Text & "-SR " + DateRange.ToString

    Wsheet.Cells(1, 1).Value = "MAKATI TRANSFORWARDERS CORP."
    Wsheet.Cells(1, 1).Font.Bold = True
    Wsheet.Cells(1, 1).Font.Size = 10

    Wsheet.Cells(3, 1).Value = "Daily Status Report - " & DateRange.ToString
    Wsheet.Cells(3, 1).Font.Bold = True
    Wsheet.Cells(3, 1).Font.Size = 10

    Wsheet.Cells(5, 1).Value = consigneeName
    Wsheet.Cells(5, 1).Font.Bold = True
    Wsheet.Cells(5, 1).Font.Size = 10

    Wsheet.Cells(7, 1).Value = "Prepared by: Jasmin"
    Wsheet.Cells(7, 1).Font.Bold = True
    Wsheet.Cells(7, 1).Font.Size = 10

    Wsheet.Cells(9, 1).Value = "HAWB"
    Wsheet.Cells(9, 2).Value = "INVOICE VALUE"
    Wsheet.Cells(9, 3).Value = "DUTIABLE VALUE"
    Wsheet.Cells(9, 4).Value = "CUSTOMS DUTY"
    Wsheet.Cells(9, 5).Value = "LANDED COST"
    Wsheet.Cells(9, 6).Value = "ENTRY NO"
    Wsheet.Cells(9, 7).Value = "IP NO"
    Wsheet.Cells(9, 8).Value = "DESCRIPTION"
    Wsheet.Cells(9, 9).Value = "REMARKS"
    For Me.itemCounter = 1 To 9
        Wsheet.Cells(9, itemCounter).Font.Bold = True
    Next itemCounter

    rs = New ADODB.Recordset
    sql = "Select * From tblDocEntry Where CONVERT(datetime, DateEntry) Between '" & Format(dteFrom.Value, "MM/dd/yyyy") & "' AND '" & Format(dteTo.Value, "MM/dd/yyyy") & "' and ClientCode='" & Trim(cmboAccount.Text) & "' order by DateEntry"
    With rs
        .Open(sql, cn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
        rowCounter = 10
        itemCounter = 1
        While .EOF = False
            Dim chkBLHAWBno As String
            chkBLHAWBno = .Fields("BLHAWBNo").Value
            Dim subHAWB As String
            subHAWB = chkBLHAWBno.ToString.Substring(0, 3)
            If IsExist("tblOmit", "OmitHAWB", subHAWB) Then
                Dim len As String = chkBLHAWBno.ToString.Trim.Length
                Wsheet.Cells(rowCounter, 3).Value = chkBLHAWBno.ToString.Substring(4, len - 4)
            Else

                Wsheet.Cells(rowCounter, 3).Value = .Fields("BLHAWBNo").Value

            End If
            Wsheet.Cells(rowCounter, 1).NumberFormat = "0000"
            Wsheet.Cells(rowCounter, 2).Value = .Fields("FMVUSD").Value
            Wsheet.Cells(rowCounter, 2).NumberFormat = "0.00"
            Wsheet.Cells(rowCounter, 3).Value = .Fields("PESOVALUE").Value
            Wsheet.Cells(rowCounter, 3).NumberFormat = "0.00"
            Wsheet.Cells(rowCounter, 4).Value = .Fields("DUTYAMOUNT").Value
            Wsheet.Cells(rowCounter, 4).NumberFormat = "0.00"
            Wsheet.Cells(rowCounter, 5).Value = .Fields("LANDEDCOST").Value
            Wsheet.Cells(rowCounter, 5).NumberFormat = "0.00"
            Wsheet.Cells(rowCounter, 6).Value = .Fields("ENTRYNO").Value
            Wsheet.Cells(rowCounter, 7).Value = .Fields("IPNO").Value
            Wsheet.Cells(rowCounter, 8).Value = Trim(.Fields("GoodDesc").Value)
            Wsheet.Cells(rowCounter, 9).Value = .Fields("DOCREMARKS").Value
            rowCounter = rowCounter + 2
            itemCounter = itemCounter + 1
            .MoveNext()
        End While
        .Close()
    End With
    rs = Nothing

    Wsheet.Cells(rowCounter + 1, 1).Value = "Total Number of Entry/ies: " & (itemCounter - 1)
    Wsheet.Cells(rowCounter + 2, 1).Value = "Date Printed: " + Format(Now, "MM/dd/yyyy") + " - " + Format(Now, "hh:mm:ss tt")
    Wsheet.Cells(rowCounter + 1, 1).Font.Bold = True
    Wsheet.Cells(rowCounter + 2, 1).Font.Bold = True
    Wsheet.Columns.AutoFit()
    Wsheet.Rows.AutoFit()
    'Wsheet.Cells.NumberFormat = "0000"
    filesavepath = AppPath & "\Status Reports\" & Trim(cmboAccount.Text) & " - Status Report " + DateRange.ToString + ".xls"
    Wbook.SaveAs(filesavepath)
    Wbook.Close(True)
    System.Diagnostics.Process.Start(filesavepath)
    Wbook = Nothing
    Wsheet = Nothing
    MsgBox("Report Successfully Generated", vbOKOnly + vbInformation, "System Alert")
End Sub

抛出的错误是这样的: “连接超时已到期:在登录后阶段过去了超时时间。连接可能在等待服务器完成登录过程并响应时超时。或者在尝试创建多个活动连接时可能已超时。”< / p>

非常感谢有关我的问题的任何帮助

1 个答案:

答案 0 :(得分:0)

除非以艰难的方式尝试,否则没有人可以给你确切的答案。

一般情况下,我建议您采取更多措施进行调试,以确定:

  1. 这是一个SQL连接问题吗?

    • 可能是因为Win 8框阻止了sql连接通过?防火墙?
    • sql中预期数据的大小是多少?可能是Win 8机器有一些防病毒软件扫描传入的数据会减慢进程吗?
  2. 是否由于excel写入过程花了太长时间?

    • 我可以看到你逐个单元格更新excel文件,实际上有更好的方法可以显着加快它的速度,你可以通过互操作让google快速写入excel
    • 数据大小 - 是否可能会有太多行要写入文件并且需要多长时间?
  3. 您可以添加更多断点或一些日志记录来挖掘实际原因,然后解决这个问题。