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
4

2 回答 2

1

如果您尝试将其从网络服务器添加到客户端,那么这种情况是不可能的。无需让您在客户端上安装 flash / silverlight 或类似的东西,就可以超越浏览器客户端软件的安全性。

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

于 2010-12-22T13:53:24.137 回答
1

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.

于 2010-12-22T14:34:27.503 回答