How do i transfer html form data to excel
I'm having many html forms, on which i had saved some personal data on it. Now, i would like to transfer that data on my Excel sheet. Can any body provide me the correct solution for the above issue? Have any body transfer their Html forms data to Excel sheet? Kindly provide me the solution for teh above issue.
Re: How do i transfer html form data to excel
Hey you can try the following code in order to transfer your Html form data to Excel sheet.
Quote:
DataSet ds=objAdmin.getErrorsInDetail();
Response.AppendHeader("Content-Disposition", "attachment; filename=Error.xls") ;
Response.ContentType="application/ms-excel";
Response.Write("<table>");
Response.Write("<tr>");
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
Response.Write("<td>"+ ds.Tables[0].Columns[i].ColumnName +"</td>");
}
Response.Write("</tr>");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
Response.Write("<tr>");
DataRow row=ds.Tables[0].Rows[i];
for(int j=0;j<ds.Tables[0].Columns.Count;j++)
{
Response.Write("<td>"+ row[j]+"</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
Response.End();
Re: How do i transfer html form data to excel
The XML file that is generated can be opened directly in Excel. For illustration purposes, the DataSet object is created from the Microsoft Access Northwind sample database by using the Jet OLEDB Provider. However, similar code works with any DataSet object that you create with Visual C# 2005 or Visual C# .NET.
1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Select Windows Application from the Visual C# Projects types. Form1 is created by default.
2. On the View menu, select Toolbox to display the Toolbox and add a button to Form1.
3. Double-click Button1. The code window for the Form appears.
4. Add the following using directives to the top of Form1.cs:
Quote:
using System.Data.OleDb;
using System.Xml;
5. Add the following private member variable to the Form1 class:
Quote:
private string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ " C:\\Program Files\\Microsoft Office\\Office10\\Samples\\"
+ "Northwind.mdb;";
Note You may need to modify the path to Northwind.mdb in the connection string to match your installation.
6. Add the following code to the button1_Click handler:
Quote:
//Connect to the data source.
OleDbConnection objConn = new OleDbConnection (strConn);
try
{
objConn.Open();
//Fill a dataset with records from the Customers table.
OleDbCommand objCmd = new OleDbCommand(
"Select CustomerID, CompanyName, ContactName, "
+ "Country, Phone from Customers", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmd;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset);
//Create the FileStream to write with.
System.IO.FileStream fs = new System.IO.FileStream(
"C:\\Customers.xml", System.IO.FileMode.Create);
//Create an XmlTextWriter for the FileStream.
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(
fs, System.Text.Encoding.Unicode);
//Add processing instructions to the beginning of the XML file, one
//of which indicates a style sheet.
xtw.WriteProcessingInstruction("xml", "version='1.0'");
//xtw.WriteProcessingInstruction("xml-stylesheet",
// "type='text/xsl' href='customers.xsl'");
//Write the XML from the dataset to the file.
objDataset.WriteXml(xtw);
xtw.Close();
//Close the database connection.
objConn.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
Note You must change the code in Visual Studio 2005. By default, Visual C# adds one form to the project when you create a Windows Forms project. The form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write the code in Form1.cs. The Form1.designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox.
7. Press F5 to build and run the program.
8. Click Button1 to create the XML file, then close Form1 to end the program.
9. Start Excel 2002 or Excel 2003 and open the C:\Customers.xml output file.
10. After you have observed how the XML has been parsed into rows and columns in the new workbook, close the file and quit Excel.