如何在导出到excel代码时添加保存对话框? [英] How do I add in a save dialogue in my export to excel codes?

查看:115
本文介绍了如何在导出到excel代码时添加保存对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家,



我将在导出时使用2种方法保存我的excel文件,第1次直接保存在服务器中,第2次以保存对话框让用户选择自己的位置,因此在这种情况下,每个导出中将保存2个文件,我可以直接保存到服务器中,但如何在导出到excel代码时添加保存对话框? />


Hi Members,

I will be saving my excel file upon export using 2 methods, 1st to save as directly in server, 2nd to come up with a save dialogue to let user choose their own location thus in this case 2 files will be saved in total in each export which I am able to save directly into the server but How do I add in a save dialogue in my export to excel codes?

protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
 
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook  =  app.Workbooks.Add(Type.Missing);
 
String DT1 = "Data table 1";
String DT2 = "Data table 2";
 
ExportToExcel(app, workbook, Gridview1, DT1, 1);
 
ExportToExcel(app, workbook, Gridview2, DT2, 2);   

workbook.SaveAs(@"C:\Users\testacc\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 
}







public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName)
        {
            // see the excel sheet behind the program
            app.Visible = false;

            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Add();
           
            // changing the name of active sheet
            worksheet.Name = SheetName;

            // storing header part in Excel
            for (int i = 1; i < gridview.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText;
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < gridview.Rows.Count - 1; i++)
            {
                for (int j = 0; j < gridview.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString();
                }
            }

           
        }

推荐答案

你应该阅读以下Microsoft知识库文章:

You should read the following Microsoft knowledgebase article:



Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务),因为Office在此环境中运行时可能会出现不稳定的行为和/或死锁。


Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.





有多种方法可以在服务器上创建Excel电子表格而无需使用Office互操作。例如:

  • EPPlus [ ^ ];
  • ClosedXML [ ^ ];
  • ExcelDataReader [ ^ ] ;
  • OpenXML SDK [ ^ ];


  • There are various ways to create Excel spreadsheets on the server without using Office interop. For example:

    • EPPlus[^];
    • ClosedXML[^];
    • ExcelDataReader[^];
    • The OpenXML SDK[^];
    • Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      Response.AppendHeader("Content-Disposition", "attachment; filename=sheet.xlsx");
      excelFile.SaveAs(Response.OutputStream); // EPPlus example





      如果您希望用户将文件保存到服务器上的指定位置,那么您将不得不编写自定义文件系统浏览器,让用户从服务器的文件系统中选择一个位置。没有内置方法可以做到这一点。



      如果您尝试从服务器端代码显示另存为对话框,它将显示在服务器上,没有人会看到它。在Visual Studio中运行代码时,它可能出现,但这只是因为服务器和客户端在该特定情况下是同一台计算机。



      If you want the user to save the file to a specified location on the server, then you're going to have to write a custom file-system browser to let the user select a location from your server's file-system. There's no built-in way to do that.

      If you attempt to display a "Save As" dialog from server-side code, it will display on the server, where nobody will ever see it. It might appear to work when you run your code in Visual Studio, but that's only because the server and client are the same computer in that specific case.


      这篇关于如何在导出到excel代码时添加保存对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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