打开或保存文件下载提示 [英] Open or save promp on file download

查看:119
本文介绍了打开或保存文件下载提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据表中提取数据。

我试过的是工作但问题是如果用户想要直接打开文件或保存在文件夹中则没有提示。

我应该在代码中添加或更改什么?



我尝试了什么:



I am working on extracting data from the datatable.
What I have tried is working but the problem is there is no prompt to user if they want to directly open the file or save in folder.
What should I add or change in the code?

What I have tried:

private void ExporttoExcel()
        {

            string fileName = "Subcon_Profile_List_Import_Error_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";

            Excel.Application oXL;
            Excel.Workbook oWB;
            Excel.Worksheet oWS;
            Excel.Range oRange;

            oXL = new Excel.Application();

            oXL.Visible = false;
            oXL.DisplayAlerts = false;

            oWB = oXL.Workbooks.Add(Missing.Value);

            oWS = (Excel.Worksheet)oWB.ActiveSheet;
            oWS.Name = "Error_Log";

            int rowCount = 1;

            foreach (DataRow dr in DTSubconProfileListImport.Rows)
            {
                rowCount += 1;

                for (int i = 1; i < DTSubconProfileListImport.Columns.Count + 1; i++)
                {
                    if (rowCount == 2)
                    {
                        oWS.Cells[1, i] = DTSubconProfileListImport.Columns[i - 1].ColumnName;
                    }
                    oWS.Cells[rowCount, i] = dr[i - 1].ToString();
                }
            }

            Excel.Range r1 = oWS.Cells[1, 1];
            Excel.Range r2 = oWS.Cells[rowCount, DTSubconProfileListImport.Columns.Count];
            oRange = (Excel.Range)oWS.get_Range(r1, r2);
            oRange.EntireColumn.AutoFit();

            oWS = null;
            oRange = null;
            oWB.SaveAs(fileName, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            oWB.Close(Missing.Value, Missing.Value, Missing.Value);
            oWB = null;
            oXL.Quit();
           
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

        }

推荐答案

保存文件后,您需要将其推送到客户端



在ASP.NET中使用另存为对话框下载文件 - Rick Strahl的Web日志 [ ^ ]



顺便说一句,我假设您正在本地计算机上完成所有这些工作,并且尚未将其部署到远程计算机上进行测试。当您实际部署此代码时,它不会起作用,因为asp.net站点不支持自动化Excel。使用EPPlus,Excel ODBC驱动程序或XML SDK通过asp.net创建Excel文件。
After you save the file you'll need to push it to the client

Downloading a File with a Save As Dialog in ASP.NET - Rick Strahl's Web Log[^]

By the way, I'm going to assume you are doing all of this work on your local machine and have not deployed it to a remote machine for testing yet. When you actually deploy this code it isn't going to work as automating Excel is not supported on an asp.net site. Use EPPlus, Excel ODBC driver, or XML SDK to create Excel files via asp.net.


这篇关于打开或保存文件下载提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆