Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi Friends,
I am using VS 2008,C#.net,asp.net.
I am exporting the data to excel 2003.
The exported excel is of the type .xls in IE and Chrome.
But it is not .xls type in mozilla.When open that file, it is asking which program you want to open?
What is the reason?

the program is mentioned below.

C#
private void ExportDataSetToExcel()
      {
          try
          {
              DataSet Ds = new DataSet();
              DataTable 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)
          {

          }
      }
Posted
Updated 31-Jan-14 18:53pm
v3

use

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + strFileName+ "\"");
 
Share this answer
 
This might be helpfull for you....

C#
protected void GenerateExcel(DataTable dta)
   {
           using (XLWorkbook wb = new XLWorkbook())
           {

               wb.Worksheets.Add(dta, "Sheet1");
               Response.Clear();
               Response.Buffer = true;
               Response.Charset = "";
               Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
               Response.AddHeader("content-disposition", "attachment;filename=EmploymentTracker.xls");
               using (MemoryStream MyMemoryStream = new MemoryStream())
               {
                   wb.SaveAs(MyMemoryStream);
                   MyMemoryStream.WriteTo(Response.OutputStream);
                   Response.Flush();
                   Response.End();
               }
           }
       
   }

HTML



Please mark this as answer if this helps... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900