Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel文件“;开放式&引用;另存为;对话框未在asp.net中显示_Asp.net_Vb.net_Excel_Export To Excel - Fatal编程技术网

Excel文件“;开放式&引用;另存为;对话框未在asp.net中显示

Excel文件“;开放式&引用;另存为;对话框未在asp.net中显示,asp.net,vb.net,excel,export-to-excel,Asp.net,Vb.net,Excel,Export To Excel,我已搜索S/O,但无法找到有助于解决问题的解决方案。我正在将数据从asp.net应用程序导出到xls文件。当用户单击“导出数据”按钮时,我需要显示“打开/另存为/取消”对话框 我如何做到这一点 oSheet = Nothing oRange = Nothing oWB.SaveAs(sFileName.ToString()) oWB.Close() oWB = Nothing oXL.Quit()

我已搜索S/O,但无法找到有助于解决问题的解决方案。我正在将数据从asp.net应用程序导出到xls文件。当用户单击“导出数据”按钮时,我需要显示“打开/另存为/取消”对话框

我如何做到这一点

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

嗯。你没有。excel对象正在WEB服务器上的ASP.NET进程中运行。无法使对话框显示在客户端(浏览器)上。

您可以通过多种方式实现。您提供的代码示例与此无关,但显然您创建了文件并将其保存到磁盘。由于您有文件路径,因此可以使用。您需要添加内容配置以打开“另存为”对话框

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)

试试这个代码块。你可能需要对它进行微调。代码的
响应
块与对话框有关:

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 
public void导出()
{
尝试
{
如果(会话[“ExcelGrid”]!=null)
{
数据集DS=null;
如果(会话[“ExcelGrid”]!=null)
{
DS=(数据集)会话[“ExcelGrid”];
如果(DS.Tables[0].Rows.Count>0)
{
int j=0;
int k=0;
字符串文件名=string.Concat(DateTime.Now.ToString(),“Order”);
DataTable dt=DS.表[0];
dt.Columns.Remove(“用户角色”);
dt.Columns.Remove(“iControlDeviationID”);
dt.列。删除(“代码”);
dt.列。删除(“SiteMcCategory”);
dt.列。删除(“站点子类别”);
dt.列。删除(“iDeviationID”);
dt.Columns.Remove(“iControlPointID”);
dt.列。移除(“SFacity”);
dt.Columns.Remove(“sItemCategory1”);
删除dt.列(“站点子类别1”);
dt.柱。移除(“sRegion”);
dt.柱。移除(“Scontry”);
dt.柱。移除(“sItem”);
dt.列[“dRegisterDate”]。设置序号(5);
Context.Response.AddHeader(“内容处置”、“附件;文件名=“+filename+”.xls”);
Context.Response.ContentType=“应用程序/vnd.ms excel”;
Context.Response.Charset=“”;
Context.Response.ContentEncoding=System.Text.Encoding.GetEncoding(“ISO-8859-1”);
Context.Response.Write(“”);
对于(j=0;j<30;j++)
{
if(j<1)
{
对于(int i=0;ipublic 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);
}
}