如何使用另存为对话框将创建的 Excel 实例保存到客户端磁盘

发布于 2024-10-08 16:28:21 字数 2045 浏览 7 评论 0 原文

在项目中,当用户单击按钮时,互操作库会获取一些数据并将其写入 Excel 实例。

现在,我希望:当Excel实例获取所有数据时,必须打开另存为对话框并将该Excel实例保存到用户指定的路径。

有办法做到吗?

编辑:

我将数据获取到 Excel 的代码在这里:

Public Sub ExportToExcel(ByVal dt As DataTable, ByVal outputPath As String)
    ' Create the Excel Application object
    Dim excelApp As New Microsoft.Office.Interop.Excel.ApplicationClass

    ' Create a new Excel Workbook
    Dim excelWorkbook As Excel.Workbook = excelApp.Workbooks.Add(Type.Missing)

    Dim excelSheet As Excel.Worksheet = excelWorkbook.Worksheets(1)
    excelApp.Visible = True
    ' Copy each DataTable as a new Sheet
    'sheetIndex += 1

    '' Create a new Sheet
    'excelSheet = CType( _
    '    excelWorkbook.Sheets.Add(excelWorkbook.Sheets(sheetIndex), _
    '    Type.Missing, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet), Microsoft.Office.Interop.Excel.Worksheet)

    excelSheet.Name = "Bayi"

    ' Copy the column names (cell-by-cell)
    For col = 0 To dt.Columns.Count - 1
        excelSheet.Cells(1, col + 1) = dt.Columns(col).ColumnName
    Next

    CType(excelSheet.Rows(1, Type.Missing), Microsoft.Office.Interop.Excel.Range).Font.Bold = True

    ' Copy the values (cell-by-cell)
    For col = 0 To dt.Columns.Count - 1
        For row = 0 To dt.Rows.Count - 1
            excelSheet.Cells(row + 2, col + 1) = dt.Rows(row).ItemArray(col)
        Next
    Next

    excelSheet = Nothing

    ' Save and Close the Workbook
    excelWorkbook.SaveAs(outputPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, _
     Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, _
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

    excelWorkbook.Close(True, Type.Missing, Type.Missing)

    excelWorkbook = Nothing

    ' Release the Application object
    excelApp.Quit()
    excelApp = Nothing

    ' Collect the unreferenced objects
    GC.Collect()
    GC.WaitForPendingFinalizers()

End Sub

In a project when user click a button, some data is gotten and written to an excel instance by interop library.

Now, I want that: When excel instance get all data, a save as dialog box muste be open and save this excel instance to user specified path.

Is there a way to do it?

Edit:

My code to get data to excel is here:

Public Sub ExportToExcel(ByVal dt As DataTable, ByVal outputPath As String)
    ' Create the Excel Application object
    Dim excelApp As New Microsoft.Office.Interop.Excel.ApplicationClass

    ' Create a new Excel Workbook
    Dim excelWorkbook As Excel.Workbook = excelApp.Workbooks.Add(Type.Missing)

    Dim excelSheet As Excel.Worksheet = excelWorkbook.Worksheets(1)
    excelApp.Visible = True
    ' Copy each DataTable as a new Sheet
    'sheetIndex += 1

    '' Create a new Sheet
    'excelSheet = CType( _
    '    excelWorkbook.Sheets.Add(excelWorkbook.Sheets(sheetIndex), _
    '    Type.Missing, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet), Microsoft.Office.Interop.Excel.Worksheet)

    excelSheet.Name = "Bayi"

    ' Copy the column names (cell-by-cell)
    For col = 0 To dt.Columns.Count - 1
        excelSheet.Cells(1, col + 1) = dt.Columns(col).ColumnName
    Next

    CType(excelSheet.Rows(1, Type.Missing), Microsoft.Office.Interop.Excel.Range).Font.Bold = True

    ' Copy the values (cell-by-cell)
    For col = 0 To dt.Columns.Count - 1
        For row = 0 To dt.Rows.Count - 1
            excelSheet.Cells(row + 2, col + 1) = dt.Rows(row).ItemArray(col)
        Next
    Next

    excelSheet = Nothing

    ' Save and Close the Workbook
    excelWorkbook.SaveAs(outputPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, _
     Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, _
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

    excelWorkbook.Close(True, Type.Missing, Type.Missing)

    excelWorkbook = Nothing

    ' Release the Application object
    excelApp.Quit()
    excelApp = Nothing

    ' Collect the unreferenced objects
    GC.Collect()
    GC.WaitForPendingFinalizers()

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-10-15 16:28:22

我不确定这是否是您想要的,但是如果您想让用户下载您在服务器端创建的 excel 文件,那么只需将 excel 文件的内容写入响应,设置正确的哑剧类型 - 就这样!
PS 不要忘记清除当前生成的响应。

I am not sure, that this is what you want, but if you want to let a user download an excel file, that you've created on the server side, then just write the content of the excel file to the Response, set the correct mime type - and there you go!
P.S. Don't forget to clear the currently generated response.

染年凉城似染瑾 2024-10-15 16:28:22

如果您尝试从网络服务器将其添加到客户端,则不可能出现这种情况。不让你在客户端安装flash/silverlight或者类似的东西,就超越了浏览器客户端软件的安全性。

更新:
她是“快速浏览 Silverlight 3:保存文件对话框"
以及如何在 JavaScript 中读写文件

If u try to add it to the client from a webserver, there is no way that case. Without getting you to install a flash / silverlight on the client or something similar, to go beyond the security of the browser client software.

Updated:
Her is "A quick look at Silverlight 3: Save File Dialog"
And How to read and write files in JavaScript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文