I have succeeded in creating XML file from an Excel file using the following C# code:
protected void Button5_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
OleDbConnection ole = new OleDbConnection();
string s = Server.MapPath("../admin/ProductOptions");
s = s + "\\" + FileUpload1.FileName;
System.IO.File.Delete(s);
FileUpload1.PostedFile.SaveAs(s);
string path = s;
ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;" + "\"";
OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.Visible = true;
string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
Session["ss"] = ds;
write_to_xml(ds,filepath);
}
else
{
Label2.Visible = true;
Label2.Text="[Please Select a file]";
}
}
But the problem is when this code is converting the Excel Data to XML data, then dots are itself converted into Hash(Only First Row). I know the reason but don't know the solution.
It`s happening because of dots in Excel file when converted into XML tags them implicitly converted to HASH.......
Kindly suggest me, how can I stop this conversion?
Finally got the solution:
When OLEDB Adapter fills the data in DataSet, it converts DOT into HASH.
Now I have stored that data into a DataTable(dt) and then accessed the column name and replace HASH with DOT (using Replace method of String) and create a new DataTable(dt2) with new column names.
After this using two for loops, I have inserted data from first DataTable(dt) to new Datatable(dt2). (*one loop for rows and another one for columns)
Following is the full code for that function:
if (FileUpload1.HasFile)
{
OleDbConnection ole = new OleDbConnection();
string s = Server.MapPath("../admin/ProductOptions");
s = s + "\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(s);
string path = s;
ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;IMEX=2;READONLY=FALSE;" + "\" ";
OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
DataTable dt = (DataTable)ds.Tables[0];
DataTable dt2 = new DataTable("dt2");
Session["dt"] = null;
for (int i = 0; i < dt.Columns.Count; i++)
{
string s2 = dt.Columns[i].ToString();
s2 = s2.Replace("#", ".");
string ProductName = s2.ToString();
if (Session["dt"] == null)
{
DataColumn dCol1 = new DataColumn(ProductName, typeof(System.String));
dt2.Columns.Add(dCol1);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
dt2.Rows.Add();
for (int x = 0; x < dt.Columns.Count; x++)
{
dt2.Rows[i][x] = dt.Rows[i][x];
}
}
System.IO.File.Delete(s);
GridView1.DataSource = dt2;
GridView1.DataBind();
GridView1.Visible = true;
string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
// Session["ss"] = ds;
write_to_xml(dt2,filepath);
}
else
{
Label2.Visible = true;
Label2.Text="[Please Select a file]";
}
Following is the code for write_to_xml()
:
public void write_to_xml(DataTable dt, string path)
{
dt.WriteXml(path);
}
Any query or alternative solution would be appreciated... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With