<summary>
Reading an 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 an Excel document
</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 ("Write Excel Error:" +ex. Message); }
return false;
}
Use Access as a back-end database support.