2016-07-06 7 views
0

Ich habe eine Seite, die entweder Informationen in einer HTML-Tabelle anzeigt oder dieselben Informationen wie eine Excel-Datei ausgibt.VBScript-Ausgabe als Excel-Arbeitsblatt funktioniert nicht

Die HTML-Ansicht funktioniert gut, aber der Excel-Teil nicht mit: (. 500 - Interner Serverfehler Es gibt ein Problem mit der Ressource ist, die Sie suchen, und es kann nicht angezeigt werden.)

Idealerweise Wenn es möglich ist, den Excel-Teil des Codes zu trennen, würde ich das lieber als Endprodukt haben, aber ich muss es zuerst funktionieren lassen. Ich würde mich über irgendwelche Ideen freuen.

<%@ Language=VBScript %> 
<% Session("CurrentPageTitle") = "Test Transaction Summary" %> 
<% 
Response.Buffer = TRUE 
if Request.Form("present") = "xls" then 
set objXL = CreateObject("Excel.Application") 
Set objWB = objXL.WorkBooks.Open("C:\Inetpub\wwwroot\temp\xl.xlsx") 
Set objWS = objXL.ActiveWorkBook.WorkSheets("Sheet1") 
numRow = 9 

end if 

if Request.Form("present") <> "xls" then 
%> 
<!--#include file="../common/header.inc"--> 
<% 
end if 
varState = Request.QueryString("state") 
varCustomerIdentifier = Request.Form("Customer") 
CustomerArray = Split(varCustomerIdentifier,",") 
varCustomer = CustomerArray(0) 
varSub = CustomerArray(1) 
varCity = CustomerArray(2) 

if Request.Form("Summary") = "user" and varState <> "u" then 
    'Should not be here, form navigation glitch - go to GetUserParam.asp 
    location.href="GetUserParam.asp?f=" & varCustomerIdentifier & "&t=user" 
else 
if varState = "u" then 
    varYesterday = Request.Form("Smonth") + "/" + Request.Form("Sday")+ "/" + Request.Form("Syear") 
    varToday = Request.Form("Emonth") + "/" + Request.Form("Eday")+ "/" + Request.Form("Eyear") 
elseif varState = "m" then 
    varYesterday = month(date()) & "/1/" & year(date()) 
    varToday = date() 
    strYesterday = dateadd("d",-1,date()) 
elseif varState = "y" then 
    varYesterday = "1/1/" & year(date()) 
    varToday = date() 
    strYesterday = dateadd("d",-1,date()) 
elseif varState = "s" then 
    varYesterday = Request.Form("Smonth") + "/" + Request.Form("Sday")+ "/" + Request.Form("Syear") 
    varToday = Request.Form("Emonth") + "/" + Request.Form("Eday")+ "/" + Request.Form("Eyear") 
    varInterval = Request.Form("Interval") 
else 
    varYesterday = cdate(datepart("m",date()-1) & "/" & datepart("d",date()-1) & "/" & datepart("yyyy",date()-1)) 
    varToday = cdate(datepart("m",date()) & "/" & datepart("d",date()) & "/" & datepart("yyyy",date())) 
end if 
if varCustomer = "0" then 
    strSQL = "select CustomerNo, SubNo, City, CustomerName from Customer order by CustomerName" 
else 
    strSQL = "select CustomerNo, SubNo, City, CustomerName from Customer where CustomerNo = " & varCustomer & " and SubNo = " & varSub & " and City = " & varCity & " order by CustomerName" 
end if 

' sjs 
dim DBCONN 
set DBCONN = Server.CreateObject("ADODB.Connection") 
DBCONN.CommandTimeout = 60000 
DBCONN.ConnectionTimeout = 60000 
DBCONN.Open "DSN=***;UID=***;PWD=***;" 
set DBConnection = DBCONN 

set DBQuery = Server.CreateObject("ADODB.Command") 
DBQuery.ActiveConnection = DBConnection 
DBQuery.CommandType = 1 
DBQuery.CommandText = strSQL 
DBQuery.CommandTimeout = 900 
Err.Clear 
set dbRS = DBQuery.Execute 
AllUpdateCount = 0 
AllInquiryCount = 0 
AllMTCount = 0 
AllETCount = 0 
AllTotalCount = 0 
index = 0 
while not dbRS.EOF 
    index = index + 1 

    if Request.Form("Summary") = "prevd" then 
     strType = "Yesterday" 
     strNote = "(from " & varYesterday & ")" 
     strFormatSQL = "select distinct [Format] 'formatid' from TransactionSummary where CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and SubNo = " & dbRS.Fields("SubNo").Value & " and City = " & dbRS.Fields("City").Value & " and TransactionDate = '" & varYesterday & "'" 
    elseif Request.Form("Summary") = "user" then 
     strType = "User Specified" 
     strFormatSQL = "select distinct [Format] 'formatid' from TransactionSummary where CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and SubNo = " & dbRS.Fields("SubNo").Value & " and City = " & dbRS.Fields("City").Value & " and TransactionDate >= '" & varYesterday & "' and TransactionDate <= '" & varToday & "'" 
     strNote = "(from " & varYesterday & " to " & varToday & ")" 
    elseif Request.Form("Summary") = "mtd" then 
     strType = "Month To Date" 
     strFormatSQL = "select distinct [Format] 'formatid' from TransactionSummary where CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and SubNo = " & dbRS.Fields("SubNo").Value & " and City = " & dbRS.Fields("City").Value & " and TransactionDate >= '" & varYesterday & "' and TransactionDate < '" & varToday & "'" 
     strNote = "(from " & varYesterday & " through " & varToday & ")" 
    elseif Request.Form("Summary") = "ytd" then 
     strType = "Year To Date" 
     strFormatSQL = "select distinct [Format] 'formatid' from TransactionSummary where CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and SubNo = " & dbRS.Fields("SubNo").Value & " and City = " & dbRS.Fields("City").Value & " and TransactionDate >= '" & varYesterday & "' and TransactionDate < '" & varToday & "'" 
     strNote = "(from " & varYesterday & " through " & varToday & ")" 
    elseif Request.Form("Summary") = "stats" then 
     strType = "Usage Statistics" 
     strFormatSQL = "select TransactionDate from TransactionDetail where CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and SubNo = " & dbRS.Fields("SubNo").Value & " and City = " & dbRS.Fields("City").Value & " and TransactionDate >= '" & varYesterday & "' and TransactionDate <= '" & varToday & " 11:59:59 PM'" 
     strNote = "(from " & varYesterday & " to " & varToday & ")" 
    else 
     strFormatSQL = "" 
    end if 

    'very top 
if Request.Form("present") = "xls" then 

    if varCustomer = "0" and index = 1 then 
    objXL.Cells(5,2) = strType & " Summary For ALL Customers" 
    objXL.Cells(6,2) = strNote 
    end if 

    if varCustomer ="0" then 
    else 
    objXL.Cells(5,2) = strType & " Summary For Customer: " & Trim(dbRS.Fields("CustomerName").Value) & " - City " & Trim(dbRS.Fields("City").Value) 
    objXL.Cells(6,2) = strNote 
    end if 

elseif Request.Form("present") <> "xls" then 

    if varCustomer = "0" and index = 1 then 
     Response.Write("<h3>" & strType & " Summary for All Customers<br>" & chr(13)) 
     Response.Write("<font size=""-1"">" & strNote & "</font></h3>" & chr(13)) 
     Response.Write("<table border=""1"" id=""exCity"" runat=""server"">" & chr(13)) 
     Response.Write("<tr>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Customer</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>City</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Inquiry</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Update</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Total</strong></td>" & chr(13)) 
     Response.Write("</tr>" & chr(13)) 
    end if 

    ' Customer and City for ALL Customers added above each record top 
    if varCustomer = "0" then 
    ' Response.Write("<h3><i>" & Trim(dbRS.Fields("CustomerName").Value) & " - Po------------rt " & Trim(dbRS.Fields("City").Value) & "</i></h3>" & chr(13)) 

    'Customer and City for a single Customer added to very top  
    else 
     Response.Write("<h3>" & strType & " Summary for Customer: " & Trim(dbRS.Fields("CustomerName").Value) & " - City " & Trim(dbRS.Fields("City").Value) & "<br>" & chr(13)) 
     Response.Write("<font size=""-1"">" & strNote & "</font></h3>" & chr(13)) 

    'start a table with format request name transatctions at the top 
     Response.Write("<table border=""1"" id=""exCity"" runat=""server"">" & chr(13)) 
     Response.Write("<tr>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Customer</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>City</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Inquiry</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Update</strong></td>" & chr(13)) 
     Response.Write("<td align=""center""><strong>Total</strong></td>" & chr(13)) 
     Response.Write("</tr>" & chr(13)) 
    end if 
end if 

    'query db for one Customer 
    DBQuery.CommandText = strFormatSQL 
    Err.Clear 
    set dbRS3 = DBQuery.Execute 
    UpdateTotalCount = 0 
    InquiryTotalCount = 0 
    MTTotalCount = 0 
    ETTotalCount = 0 
    CustomerTotalCount = 0 
    IntervalCounter = 0 
    IntervalStart = varYesterday & " 12:00:00 AM" 
    while not dbRS3.EOF 
     if varState = "s" then 
      Difference = datediff("n", IntervalStart, dbRS3.Fields("TransactionDate").Value) 
      if (CInt(Difference) < CInt(varInterval)) then 
       IntervalCounter = IntervalCounter + 1 
      else 
       IntervalNext = dateadd("n", varInterval, IntervalStart) 
       Response.Write("<tr><td align=""left"">" & IntervalStart & " to&nbsp;&nbsp;<br>" & IntervalNext & "</td><td align=""center"" valign=""middle"">" & IntervalCounter & "</td></tr>" & chr(13)) 
       IntervalCounter = 0 
       IntervalStart = IntervalNext 
      end if 
     else 
      if Request.Form("Summary") = "prevd" then 
       strSQL = "select Sum(s.TransactionCount) 'hits', r.RequestName from TransactionSummary as s LEFT JOIN RepositoryDB.dbo.BLS_REQ as r ON s.[Format] = r.RequestID where s.CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and s.SubNo = " & dbRS.Fields("SubNo").Value & " and s.City = " & dbRS.Fields("City").Value & " and s.TransactionDate = '" & varYesterday & "' and s.[Format] = " & dbRS3.Fields("formatid").Value & " GROUP BY r.RequestName" 
      else 
       strSQL = "select sum(s.TransactionCount) 'hits', r.RequestName from TransactionSummary as s LEFT JOIN RepositoryDB.dbo.BLS_REQ as r ON s.[Format] = r.RequestID where s.CustomerNo = " & dbRS.Fields("CustomerNo").Value & " and s.SubNo = " & dbRS.Fields("SubNo").Value & " and s.City = " & dbRS.Fields("City").Value & " and s.TransactionDate >= '" & varYesterday & "' and s.TransactionDate <= '" & varToday & "' and s.[Format] = " & dbRS3.Fields("formatid").Value & " GROUP BY r.RequestName" 
      end if 
      DBQuery.CommandText = strSQL 

      Err.Clear 
      set dbRS2 = DBQuery.Execute 
      If Err.Number <> 0 Then 
       Response.Write("Could not get Transaction Summary - database error. SQL Script = " & strSQL) 
      End If 

      ' Count the hits into their respective total 
      If (Trim(dbRS3.Fields("formatid").Value) = 25) or (Trim(dbRS3.Fields("formatid").Value) = 425)Then 
       MTTotalCount = MTTotalCount + dbRS2.Fields("hits").value 
      ElseIf (Trim(dbRS3.Fields("formatid").Value = 23)) Then 
       ETTotalCount = ETTotalCount + dbRS2.Fields("hits").value 
      ElseIf (Trim(dbRS3.Fields("formatid").Value) = 38) or _ 
       ((Trim(dbRS3.Fields("formatid").Value) >= 400) and _ 
       (Trim(dbRS3.Fields("formatid").Value) <= 499) or _ 
       ((Trim(dbRS3.Fields("formatid").Value) >= 800) and _ 
       (Trim(dbRS3.Fields("formatid").Value) <= 899))) Then 
        UpdateTotalCount = UpdateTotalCount + dbRS2.Fields("hits").value 
      Else 
       InquiryTotalCount = InquiryTotalCount + dbRS2.Fields("hits").value 
      End If 
      set dbRS2 = nothing 
     end if 
     dbRS3.MoveNext 
    wend 
    set dbRS3 = nothing 
    UpdateTotalCount = UpdateTotalCount + MTTotalCount + ETTotalCount 
    CustomerTotalCount = UpdateTotalCount + InquiryTotalCount 

    ' show totals 
if Request.Form("present") = "xls" then 
    objXL.Cells(numRow,2) = Trim(dbRS.Fields("CustomerName").Value) 
    objXL.Cells(numRow,3) = Trim(dbRS.Fields("City").Value) 
    objXL.Cells(numRow,4) = UpdateTotalCount 
    objXL.Cells(numRow,5) = InquiryTotalCount          
    objXL.Cells(numRow,6) = CustomerTotalCount 
    objXL.Range("B" & numRow & ":F" & numRow).BORDERS.Weight = 2 
    numRow = numRow + 1    
end if 

if Request.Form("present") <> "xls" then 

    if varState <> "s" then 
     Response.Write("<tr>" & chr(13)) 
     Response.Write("<td align=""left"">" & Trim(dbRS.Fields("CustomerName").Value) & "</td>" & chr(13)) 
     Response.Write("<td align=""center"">" & Trim(dbRS.Fields("City").Value) & "</td>" & chr(13)) 
     Response.Write("<td align=""center"">" & UpdateTotalCount & "</td>" & chr(13)) 
     Response.Write("<td align=""center"">" & InquiryTotalCount & "</td>" & chr(13)) 
     Response.Write("<td align=""center"">" & CustomerTotalCount & "</td>" & chr(13)) 
     Response.Write("</tr>" & chr(13)) 
    else 
     IntervalNext = dateadd("n", varInterval, IntervalStart) 
     Response.Write("<tr><td align=""left"">" & IntervalStart & " to&nbsp;&nbsp;<br>" & IntervalNext & "</td><td align=""center"" valign=""middle"">" & IntervalCounter & "</td></tr>" & chr(13)) 
    end if 
    ' Response.Write("</table>" & chr(13)) 
end if 

    'set dbRS2 = nothing 
    dbRS.MoveNext 
    AllUpdateCount = AllUpdateCount + UpdateTotalCount 
    AllInquiryCount = AllInquiryCount + InquiryTotalCount 
    AllMTCount = AllMTCount + MTTotalCount 
    AllETCount = AllETCount + ETTotalCount 
    AllTotalCount = AllTotalCount + CustomerTotalCount 

'do grand totals  
wend 
set dbRS = nothing 

if Request.Form("present") <> "xls" then 
Response.Write("</table>" & chr(13)) 
if varCustomer = "0" and varState <> "s" then 
    Response.Write("<h3>Grand Total Update = " & AllUpdateCount & "</h3>" & chr(13)) 
    Response.Write("<h3>Grand Total Inquiry = " & AllInquiryCount & "</h3>" & chr(13)) 
    Response.Write("<h3>Grand Total for All Customers = " & AllTotalCount & "</h3>" & chr(13)) 
    Response.Write("<h3>Grand Total Manual Transactions = " & AllMTCount & "</h3>" & chr(13)) 
    Response.Write("<h3>Grand Total Early Transactions = " & AllETCount & "</h3>" & chr(13)) 
end if 

end if 
end if 
if Request.Form("present") <> "xls" then 
%><!--#include file="../Common/footer.inc"--><% 
end if 

if Request.Form("present") = "xls" then 
objXL.Cells(numRow+1,4) = "Copyright " & Year(Date) 
objXL.Cells(numRow+2,4) = "All Rights Reserved" 

Set Fso = CreateObject("Scripting.FileSystemObject") 
If (Fso.FileExists("C:\Inetpub\temp\xl2.xlsx")) Then 
Set MyFile = Fso.GetFile("C:\Inetpub\temp\xl2.xlsx") 
MyFile.Delete 
Set MyFile=nothing  
End If 
Set Fso=nothing 

objWB.SaveAs "C:\Inetpub\temp\xl2.xlsx" 
objWB.Close 
objXL.Quit 
Response.Redirect "/temp/xl2.asp" 
end if 
%> 

Antwort

0

Dieser Abschnitt des Codes:

if Request.Form("present") = "xls" then 
    objXL.Cells(numRow,2) = Trim(dbRS.Fields("CustomerName").Value) 
    objXL.Cells(numRow,3) = Trim(dbRS.Fields("City").Value) 
    objXL.Cells(numRow,4) = UpdateTotalCount 
    objXL.Cells(numRow,5) = InquiryTotalCount          
    objXL.Cells(numRow,6) = CustomerTotalCount 
    objXL.Range("B" & numRow & ":F" & numRow).BORDERS.Weight = 2 
    numRow = numRow + 1    
end if 

mit dem falschen Eltern-Objekt zu den Cells und Range Objekte bezieht. Das übergeordnete Element ist das Arbeitsblatt, das Sie zuvor als objWS festgelegt haben. Beachten Sie auch, dass Sie wirklich objWS wie folgt gesetzt werden sollte:

Set objWS = objWB.Worksheets("Sheet1") 

weil die ActiveWorkbook ändern kann, wenn man es am wenigsten erwarten ... :)