Hai,
I am converting the xml files into csv format.I had succeded that from sql server to csv format.But I do no from xml to csv.Below I has done from sql server to notepad in csv format.
Plz Please see this.Can I able to do the same format for xml.
In the below code I has put a grid view and get the values from sql.
4 Four buttons are there to convert to excel,notepad.
Plz Please help me to do the same in xml to csv.
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; ">
<tr>
<td colspan="2" bgcolor="#ccccff" align="center" style="height: 74px">
<h1>.NetIndia Group</h1>
Export To Excel </td>
</tr>
<tr>
<td style="width: 100px" valign="top">
<asp:GridView ID="gvEmployee" runat="server">
</td>
<td style="width: 100px" valign="top">
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Export To Excel using HtmlForm" /><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ExportToExcel" /><asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="ExportTOExcelUsingXML" /><asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Export To Excel by Rendering" /></td>
</tr>
<tr>
<td align="center" colspan="2" style="height: 24px"><br />
<table style="border:1px solid #aa0033; font-size:small" align="center">
<tr>
<td rowspan="2">
<img src="Image/groups_medium.gif" alt="Google Groups"
style="width: 192px; height: 54px;">
</td>
<td align="center">.NetIndia</td>
</tr>
<tr><td align="center"><a href="http://groups.google.com/group/TechdotNetIndia">Browse Archives</a> at <a href="http://groups.google.com">groups.google.com</a>
</td></tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gvEmployee.DataSource = GetData();
Page.DataBind();
}
public DataTable GetData()
{
SqlCommand com1 = null;
SqlConnection con1 = null;
con1 = new SqlConnection("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
con1.Open();
com1 = new SqlCommand("SELECT [ID], [NAME], [AGE], [PLACEOFBIRTH] from RAJ", con1);
DataTable dt = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(com1);
ada.Fill(dt);
return dt;
}
#region Using VerifyRenderingInServerForm
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gvEmployee.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
#endregion
#region Write As XML with .XLS extensions
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = GetData();
dt.TableName = "Employees";
dt.WriteXml(Server.MapPath(".")+ @"\finalData.xls", XmlWriteMode.IgnoreSchema);
Response.Redirect("finalData.xls");
}
#endregion
#region Render Dt as Excel
protected void Button3_Click(object sender, EventArgs e)
{
DataTable dt = GetData();
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
#endregion
#region Uisng HtmlForm object
protected void Button4_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gvEmployee);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
#endregion
}