【问题标题】:Vb.net connection timeout while generating excel report生成excel报告时vb.net连接超时
【发布时间】:2014-02-03 05:31:39
【问题描述】:

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

    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

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

任何关于我的问题的帮助将不胜感激

【问题讨论】:

标签: vb.net excel windows-8


【解决方案1】:

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

一般我建议你采取更多的行动来调试它,以识别:

  1. 是不是sql连接问题?

    • 会不会是Win 8的盒子阻止了sql连接打通?防火墙?
    • 来自 sql 的预期数据的大小是多少?会不会是 Win 8 机器有一些防病毒软件扫描传入的数据而减慢了进程?
  2. 是不是excel写的时间太长?

    • 我可以看到你逐个单元格地更新 excel 文件,实际上有更好的方法可以显着加快它,你可以要求谷歌通过互操作快速写入到 excel
    • 数据大小 - 会不会有太多行要写入文件并且需要很长时间?

你可以设置更多的断点或一些日志语句来挖掘真正的原因,然后解决这个问题。

【讨论】:

  • 我昨天解决了我的问题。我只记得我使用的测试服务器是 MSSQL 2005,它与 win8 存在兼容性问题。我忘记更改连接字符串并让它连接到具有 MSSQL 2010 的服务器 PC。所以是的,尝试连接到 MSSQL 2005 的 win8 计算机导致了问题。谢谢你的建议
猜你喜欢
  • 2013-10-05
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 2020-05-01
  • 2017-07-31
相关资源
最近更新 更多