Excel
Some days ago, a lot of friends said that need C # Export to Excel code, now share to everyone
///<summary>
///Read the Excel document
///</summary>
///< param name= "Path" > file name </param>
///<returns> Returns a dataset </returns>
public DataSet exceltods (string Path)
{
string strconn = "provider= Microsoft.Jet.OLEDB.4.0 "+" "Data source=" + Path + ";" + "Extended Properties=excel 8.0;";
oledbconnection conn = new OleDbConnection (strconn);
conn. Open ();
string strexcel = ";
oledbdataadapter mycommand = null;
dataset ds = null;
strexcel= "SELECT * from [sheet1$]";
mycommand = new OleDbDataAdapter (Strexcel, strconn);
ds = new DataSet ();
mycommand.fill (ds, "table1");
return ds;
}
<summary>
Writing to Excel documents
</summary>
<param name= "Path" > File name </param>
public bool Savefp2toexcel (string Path)
{
Try
{
String strconn = "Provider=Microsoft.Jet.OLEDB.4.0 +" + "Data source=" + Path + ";" + "Extended Properties=excel 8.0;";
OleDbConnection conn = new OleDbConnection (strconn);
Conn. Open ();
System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
Cmd. Connection =conn;
Cmd.commandtext = "Update [sheet1$] SET name = ' 2005-01-01 ' WHERE work number = ' Date '";
Cmd. ExecuteNonQuery ();
for (int i=0;i<fp2. Sheets [0]. rowcount-1;i++)
{
if (FP2. Sheets [0]. cells[i,0]. Text!= "")
{
Cmd.commandtext = "INSERT into [sheet1$] (work number, name, department, title, date, time) VALUES ('" +FP2). Sheets [0]. cells[i,0]. text+ "', '" +
FP2. Sheets [0]. cells[i,1]. text+ "', '" +FP2. Sheets [0]. cells[i,2]. text+ "', '" +FP2. Sheets [0]. cells[i,3]. text+
"', '" +FP2. Sheets [0]. cells[i,4]. text+ "', '" +FP2. Sheets [0]. cells[i,5]. text+ "')";
Cmd. ExecuteNonQuery ();
}
}
Conn. Close ();
return true;
}
catch (System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine ("Error writing to Excel:" +ex.) message);
}
return false;
}
This method is currently the most effective, if you do not understand where you can write communication
A