Monday, 10 October 2016

XML : Exporting gridview into excel, then convert it to xml (?) [duplicate]

This question already has an answer here:

I'm building a local webform to export gridview into .xml file. I tried it already and, works just fine when i open it in Ms. Excel. And heres what it looks like when i edit it in notepad++. You can see the code goes all the way to the right, which is not good. What "they" want is, when they export the .xml file should looks like this in notepad ++. I know it can be done by: 1). Open any .xls .xlsx files. 2). Save as .xml file.

So, is there any possible way to do those 2 steps above programatically in c# asp.net ?

My code below.

   private void ExporttoExcel( DataTable table)      {              HttpContext.Current.Response.Clear();          HttpContext.Current.Response.ClearContent();          HttpContext.Current.Response.ClearHeaders();          HttpContext.Current.Response.Buffer = true;          HttpContext.Current.Response.ContentType = "application/xml";          HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");          HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename= Att " + txtPeriodeAwal.Text + " to " + txtPeriodeAkhir.Text + ".xls");            HttpContext.Current.Response.Charset = "UTF-8";          HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;                //sets font          HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");          HttpContext.Current.Response.Write("<BR><BR><BR>");          //sets the table border, cell spacing, border color, font of the text, background, foreground, font height          HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +            "borderColor='#000000' cellSpacing='0' cellPadding='0' " +            "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");          //am getting my grid's column headers          int columnscount = GVHostDtl.Columns.Count;            for (int j = 0; j < columnscount; j++)          {      //write in new column              HttpContext.Current.Response.Write("<Td>");              //Get column headers  and make it as bold in excel columns              HttpContext.Current.Response.Write("<B>");              HttpContext.Current.Response.Write(GVHostDtl.Columns[j].HeaderText.ToString());              HttpContext.Current.Response.Write("</B>");              HttpContext.Current.Response.Write("</td>");          }          HttpContext.Current.Response.Write("</tr>");          foreach (DataRow row in table.Rows)          {//write in new row              HttpContext.Current.Response.Write("<tr>");              for (int i = 0; i < table.Columns.Count; i++)              {                  HttpContext.Current.Response.Write("<td>");                  HttpContext.Current.Response.Write(row[i].ToString());                  HttpContext.Current.Response.Write("</td>");              }                HttpContext.Current.Response.Write("</tr>");          }          //HttpContext.Current.Response.Write("</Table>");          //HttpContext.Current.Response.Write("</font>");          HttpContext.Current.Response.Flush();          HttpContext.Current.Response.End();      }    protected void GetData()      {            SortedList sl = new SortedList();          DateTime da = DateTime.Parse(Request.Form[txtPeriodeAwal.UniqueID]);          sl.Add("@datafrom-date", da);          DateTime db = DateTime.Parse(Request.Form[txtPeriodeAkhir.UniqueID]);          sl.Add("@datato-date", db);              DataView dv = ((DataTable)bll.GetDataDwnldGrid(sl)).DefaultView;          if (ViewState["sort"] != null)          {              dv.Sort = ViewState["sort"].ToString();          }              GVHostDtl.DataSource = dv;          GVHostDtl.DataBind();      protected void btnExport_Click(object sender, EventArgs e)      {              GetData();          DataSet ds = new DataSet();          DataTable dtxml = ((DataView)GVHostDtl.DataSource).Table;          ExporttoExcel(dtxml);        }    

No comments:

Post a Comment