【问题标题】:exported excel sheet has no .xls file extension in Mozilla导出的 excel 工作表在 Mozilla 中没有 .xls 文件扩展名
【发布时间】:2014-02-01 05:48:44
【问题描述】:

我正在使用 VS 2008、C#.net、asp.net。 我正在将数据导出到 excel 2003。 导出的 excel 在 IE 和 Chrome 中是 .xls 类型。 但它不是 .xls 在 mozilla 中的类型。当打开该文件时,它会询问您要打开哪个程序? 是什么原因?

该程序如下所述。 ## 标题##

私有 void ExportDataSetToExcel() { 尝试 { 数据集 Ds = new DataSet(); 数据表 DT = new DataTable();

            string param = hidType.Get("Type").ToString();
            string[] codes = param.Split('.');
            if (codes.Length == 1)
                FillGrid(Convert.ToString(param));
            else
                FillDrillDownGrid(codes[1], codes[0]);
            if (hidType.Get("Type").ToString() == "2")
            {
                DT.Columns.Add("<b>Sub Group Code</b>");
                DT.Columns.Add("<b>Account Sub Group</b>");
            }
            else if (hidType.Get("Type").ToString() == "3")
            {
                DT.Columns.Add("<b>Group Code</b>");
                DT.Columns.Add("<b>Account Group</b>");
            }
            else
            {
                DT.Columns.Add("<b>Acc.Head Code</b>");
                DT.Columns.Add("<b>Account Head</b>");  
            }

                DT.Columns.Add("<b>Debit</b>");
                DT.Columns.Add("<b>Credit</b>");


            for (int j = 0; j < grdTrialBal.VisibleRowCount; j++)
            {
                DataRow DR;
                DR = DT.NewRow();
                if (hidType.Get("Type").ToString() == "2")
                {
                    DR["<b>Sub Group Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
                    DR["<b>Account Sub Group</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
                }
                else if (hidType.Get("Type").ToString() == "3")
                {
                    DR["<b>Group Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
                    DR["<b>Account Group</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
                }
                else
                {
                    DR["<b>Acc.Head Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
                    DR["<b>Account Head</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
                }
                if (Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt")) != 0)
                { 
                 DR["<b>Debit</b>"] = Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt"));
                }
                if (Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt")) != 0)
                {
                 DR["<b>Credit</b>"] = Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt"));
                }



                CrAmount = CrAmount + Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt"));
                DrAmount = DrAmount + Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt"));

                DT.Rows.Add(DR);
            }
            DataRow Dr1;
            Dr1 = DT.NewRow();

            if (hidType.Get("Type").ToString() == "2")
            {
                Dr1["<b>Sub Group Code</b>"] = "";
                Dr1["<b>Account Sub Group</b>"] = "<b>Total</b>";
            }
            else if (hidType.Get("Type").ToString() == "3")
            {
                Dr1["<b>Group Code</b>"] = "";
                Dr1["<b>Account Group</b>"] = "<b>Total</b>";
            }
            else
            {
                Dr1["<b>Acc.Head Code</b>"] = "";
                Dr1["<b>Account Head</b>"] = "<b>Total</b>";
            }

            Dr1["<b>Debit</b>"] = "<b>" + DrAmount.ToString("#0.#0") + "</b>";
            Dr1["<b>Credit</b>"] = "<b>" + CrAmount.ToString("#0.#0") + "</b>";

            DT.Rows.Add(Dr1);

            DataGrid dg = new DataGrid();

            dg.DataSource = DT;
            dg.DataBind();

            String strFileName = "Trial Balance Account Group Wise.xls";
            if (hidType.Get("Type").ToString() == "2")
            {
                strFileName = "Trial Balance Account Sub Group Wise.xls";
            }
            else if (hidType.Get("Type").ToString() == "3")
            {
                strFileName = "Trial Balance Account Group Wise.xls";
            }
            else
            {
                strFileName = "Trial Balance Account Head Wise.xls";
            }
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            CommonFunctions cf = new CommonFunctions();
            String[] aTo = mskToDate1.Text.Split(new[] { '/', '-' });
            String dTo;
            dTo = aTo[0] + "/" + cf.getMonthName(Convert.ToInt32(aTo[1])) + "/" + aTo[2];

            if (hidType.Get("Type").ToString() == "2")
            {
                htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Sub Group Wise - As on :" + dTo + " </font></u></b>");
            }
            else if (hidType.Get("Type").ToString() == "3")
            {
                htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Group Wise - As on :" + dTo + " </font></u></b>");
            }
            else
            {
                htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Head Wise - As on : " + dTo + " </font></u></b>");
            }

            dg.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

            dg = null;
            dg.Dispose();
        }
        catch (Exception ex)
        {

        }
    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    在双引号中设置文件名

    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + strFileName+ "\"");
    

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 2017-03-21
      • 2016-10-26
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      相关资源
      最近更新 更多