我有一个使用工作表VBA编码从ASP.net写入EXCEL的函数,我在其中导出数据表并按照我想要的方式修改每个Excel单元格。
但我有一个问题:在导出时,我想询问用户是否要打开或保存文件
我写了这段代码,打开/另存为弹出窗口不起作用:
(我找到了将数据表导出为excel而没有设计的代码,但我希望我的报告更有条理)
Sub WriteToExcel(ByVal DATE1 As String, ByVal DATE2 As String)
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim DT As New DataTable
Dim filename As String = "Report-" & Date.Now.ToShortDateString.Replace("/", "-") & " " & Date.Now.ToShortTimeString.Replace(":", "-") & ".xls"
Try
DT = Session("datatable")
If DT.Rows.Count > 0 Then
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
oSheet.Range("D1").Value = "Service Based Daily/Monthly Revenu"
oSheet.Range("D1").Font.Bold = True
oSheet.Range("D1").font.size = 13
oSheet.Range("D2").Value = "From " & DATE1 & " to " & DATE2
oSheet.Range("D2").Font.Bold = True
oSheet.Range("D2").font.size = 13
oSheet.Range("D1:E1:F1").MergeCells = True
oSheet.Range("D2:E2:F2").MergeCells = True
oSheet.Range("B4").Value = "Report Build Time: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString
oSheet.Range("B4").Font.Bold = False
oSheet.Range("B4").font.size = 11
oSheet.Range("B4:C4:D4:E4").Interior.Color = RGB(200, 200, 172)
oSheet.Range("B4:C4:D4:E4").MergeCells = True
oSheet.Range("B6").Value = "Service Name"
oSheet.Range("C6").Value = "Hits"
oSheet.Range("D6").Value = "Revenue"
oSheet.Range("E6").Value = "Service Cost"
With oSheet.Range("B6:C6:D6:E6")
.Font.Bold = True
.font.size = 13
.font.Color = RGB(0, 0, 0)
.Interior.Color = RGB(70, 134, 196)
.HorizontalAlignment = -4108
.VerticalAlignment = -4108
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.EntireColumn.ColumnWidth = 20
.MergeCells = False
End With
Dim i As Integer = 7
For Each dRow As DataRow In DT.Rows
oSheet.Range("B" & i).Value = dRow("Service_Name").ToString()
oSheet.Range("C" & i).Value = dRow("Hits").ToString()
oSheet.Range("D" & i).Value = dRow("Revenue").ToString()
oSheet.Range("E" & i).Value = dRow("Service_Cost").ToString()
oSheet.Range("B" & i & ":C" & i & ":D" & i & ":E" & i).HorizontalAlignment = -4108
oSheet.Range("B" & i & ":C" & i & ":D" & i & ":E" & i).VerticalAlignment = -4108
i += 1
Next
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "inline; filename=" & filename)
Response.BinaryWrite(oExcel)
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End If
End Sub
我想改变这一部分:
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "inline; filename=" & filename)
Response.BinaryWrite(oExcel)
我希望用户选择要保存文件的位置。有任何建议吗?
答案 0 :(得分:0)
您应该可以使用附件而不是内联强制保存为对话。
Response.Clear
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)