简体   繁体   English

Excel文件“打开”“另存为”对话框未在asp.net中显示

[英]Excel file “Open” “Save As” dialog box not showing in asp.net

I have searched S/O, but unable to find a solution that helps my problem. 我搜索了S / O,但无法找到可以解决问题的解决方案。 I am exporting data to an xls file from my asp.net application. 我从我的asp.net应用程序导出数据到xls文件。 I need to display the Open / Save As / Cancel dialog box when the user clicks "Export Data" button. 当用户单击“导出数据”按钮时,我需要显示“打开/另存为/取消”对话框。

How do I accomplish that? 我该如何做到这一点?

        oSheet = Nothing
        oRange = Nothing
        oWB.SaveAs(sFileName.ToString())
        oWB.Close()
        oWB = Nothing
        oXL.Quit()

Try this code block. 试试这个代码块。 You may have to fine tune it. 你可能需要微调它。 The Response block of the code has to do with the dialog box: 代码的Response块与对话框有关:

Private Sub GenerateXLSXFile(tbl As DataTable) 

    Dim excelPackage = New ExcelPackage 

    Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage") 

    excelWorksheet.Cells("A1").LoadFromDataTable(tbl, True) 

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
    Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx") 

    Dim stream As MemoryStream = New MemoryStream(excelPackage.GetAsByteArray()) 

    Response.OutputStream.Write(stream.ToArray(), 0, stream.ToArray().Length) 

    Response.Flush() 

    Response.Close() 

End Sub 

You can do it several ways. 你可以用几种方法做到这一点。 Your code sample you gave has nothing to do with that, but apparently your creating the file and saving it to disk. 您提供的代码示例与此无关,但显然您创建了文件并将其保存到磁盘。 Seeing as you have the file path, you could use Response.WriteFile . 看到你有文件路径,你可以使用Response.WriteFile You need to add the Content-Disposition to bring up the Save As dialog. 您需要添加Content-Disposition以显示“另存为”对话框。

Response.AddHeader("Content-Disposition", "attachment; filename=EXCELFILE.xs")
Response.ContentType = "application/ms-excel" // not sure you need the contenttype, and it may be different
Response.WriteFile(FULLPATHTOFILE)
public void export()
{
try
{
    if (Session["ExcelGrid"] != null)
    {
        DataSet DS = null;
        if (Session["ExcelGrid"] != null)
        {
            DS = (DataSet)Session["ExcelGrid"];
            if (DS.Tables[0].Rows.Count > 0)
            {
                int j = 0;
                int k = 0;
                string FileName = string.Concat(DateTime.Now.ToString(), "Order");
                DataTable dt = DS.Tables[0];

                dt.Columns.Remove("UserRole");
                dt.Columns.Remove("iControlDeviationID");
                dt.Columns.Remove("sErrorCode");
                dt.Columns.Remove("sItemCategory");
                dt.Columns.Remove("sItemSubCategory");
                dt.Columns.Remove("iDeviationID");
                dt.Columns.Remove("iControlPointID");
                dt.Columns.Remove("sFacility");
                dt.Columns.Remove("sItemCategory1");
                dt.Columns.Remove("sItemSubCategory1");
                dt.Columns.Remove("sRegion");
                dt.Columns.Remove("sCountry");
                dt.Columns.Remove("sItem");
                dt.Columns["dRegisterDate"].SetOrdinal(5);


                Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
                Context.Response.ContentType = "application/vnd.ms-excel";
                Context.Response.Charset = "";
                Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
                Context.Response.Write("<table border=0 width='100%'><tr align = 'center'>");
                for (j = 0; j < 30; j++)
                {
                    if (j < 1)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            if (dt.Columns[i].Caption.ToString() == "sDeviation")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.DeviationTypes.ToString().ToUpper() + "</b></td>");
                            if (dt.Columns[i].Caption.ToString() == "sDeviationCriticalLevel")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.DeviationCriticalLevel.ToString().ToUpper() + "</b></td>");
                            if (dt.Columns[i].Caption.ToString() == "sObject")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.Object.ToString().ToUpper() + "</b></td>");
                            if (dt.Columns[i].Caption.ToString() == "sControlArea")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.ControlArea.ToString().ToUpper() + "</b></td>");
                            if (dt.Columns[i].Caption.ToString() == "sControlPoint")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.ControlPoint.ToString().ToUpper() + "</b></td>");
                            if (dt.Columns[i].Caption.ToString() == "dRegisterDate")
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.RegisteredDate.ToString().ToUpper() + "</b></td>");
                        }
                    }
                    else
                    {
                        Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
                    }
                }
                Context.Response.Write(" ");
                Context.Response.Write("</tr>");
                for (int l = 0; l < 100; l++)
                {

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        Context.Response.Write("<tr align = 'Left'>");
                        for (k = 0; k < j; k++)
                        {
                            if (k < 1)
                            {
                                for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
                                {
                                    if (l < 1)
                                    {
                                        Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + dr[iCol].ToString() + "</td>");
                                    }
                                    else
                                    {
                                        Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
                                    }
                                }
                            }
                            else
                            {
                                Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
                            }
                        }
                    }
                }
                Context.Response.Write(" ");
                Context.Response.Write("</tr>");
                Context.Response.Write(" ");
                Context.Response.Write("</table>");

                Response.Buffer = true;
                Context.Response.Flush();                       
                Context.Response.Close();
                Context.Response.End();
                //ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "window.setTimeout('window.location.reload(true);',5000);", true);
            }
        }
    }
}
catch (Exception ex)
{
    Response.Write(ex.Message.ToString());
}
finally
{
    ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "a.aspx;", true);
}
}

Uhm. 嗯。 You do not. 你不。 Your excel objects are running in the ASP.NET process ON THE WEB SERVER. 您的excel对象正在Web服务器上的ASP.NET进程中运行。 There's no way to make the dialog show up on the CLIENT (BROWSER) side. 无法在CLIENT(BROWSER)端显示对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM