asp vb在特定路径中保存excel文件

时间:2014-07-03 09:02:40

标签: asp.net vb.net excel vbscript

参考此链接Creating an Excel Application from VB.Net

我想将excel文件保存到此路径:

C:\Users\asdfme\Documents

使用文件名格式[PROJ_DATE].xls示例:PROJ_20140703.xls 这是我的代码:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call temporaryValue()
        Dim appXL As Excel.Application
        Dim wbXl As Excel.Workbook
        Dim shXL As Excel.Worksheet
        Dim raXL As Excel.Range
        Dim strPathExcel As String

        ' Start Excel and get Application object.
        appXL = CreateObject("Excel.Application")
        appXL.Visible = True
        ' Add a new workbook.
        wbXl = appXL.Workbooks.Add
        shXL = wbXl.ActiveSheet
        ' Add table headers going cell by cell.
        Dim heads() As String = {"", "MONo", "PostingDate", "DocDate", "SBU", "Operation", "Resource", "TimeType", "StartDate", "StartTime", "EndDate", "EndTime", "NoofResources", "Remarks", "Quantity", "Rejects"}
        For head1 As Integer = 1 To 15
            shXL.Cells(1, head1).Value = heads(head1)
        Next
        Dim body() As String = {"", txtmo.Text, PostingDate, DocDate, SBU, Operation, Session("user"), (ddltype.SelectedValue).ToUpper, lblsDate.Text, lblsTime.Text, lbleDate.Text, lbleTime.Text, no_ofres, lblrem.Text, txtgoods.Text, txtrejects.Text}
        For body1 As Integer = 1 To 15
            shXL.Cells(2, body1).Value = body(body1)
        Next
        appXL.Visible = True
        appXL.UserControl = True
        ' Release object references.
        wbXl.SaveAs(Filename:="C:\Users\asdfme\Documents\PROJ_20140703.xls")
        raXL = Nothing
        shXL = Nothing
        wbXl = Nothing
        appXL.Quit()
        appXL = Nothing
        Exit Sub
Err_Handler:
        MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
    End Sub

当我运行此代码时,

  • 它只创建一个excel文件并询问我是否要保存该文件。
  • 我想要的是每当我运行它时,文件将以我声明的路径中的文件名格式(如上所述)保存。

请帮忙。谢谢

1 个答案:

答案 0 :(得分:1)

Dim myXlsFileName As String = "PROJ_" & " " & Format(Now.Date, "yyyyddMM") & ""
wbXl.SaveAs(Filename:="C:\Documents and Settings\All Users\Desktop\" & myXlsFileName & ".xls")