View plaincopy to clipboardprint?
-
- ClassExampleoflegalsoft
-
- {
-
- /// <Summary>
-
- /// Read the Excel document
-
- /// </Summary>
-
- /// <Param name = "path"> file name </param>
- /// <Returns> Returns a dataset </returns>
-
- PublicDataset exceltods (StringPath)
-
- {
-
- StringStrconn ="Provider = Microsoft. Jet. oledb.4.0 ;"+"Data Source ="+ Path +";"+"Extended properties = Excel 8.0 ;";
- Oledbconnection conn =NewOledbconnection (strconn );
-
- Conn. open ();
-
- StringStrexcel ="";
-
- Oledbdataadapter mycommand =Null;
-
- Dataset DS =Null;
- Strexcel ="Select * from [sheet1 $]";
-
- Mycommand =NewOledbdataadapter (strexcel, strconn );
-
- DS =NewDataset ();
-
- Mycommand. Fill (DS,"Table1");
-
- ReturnDS;
- }
-
- /// <Summary>
-
- /// Write the Excel document
-
- /// </Summary>
-
- /// <Param name = "path"> file name </param>
-
- Public BoolSavefp2toexcel (StringPath)
- {
-
- Try
-
- {
-
- StringStrconn ="Provider = Microsoft. Jet. oledb.4.0 ;"+"Data Source ="+ Path +";"+"Extended properties = Excel 8.0 ;";
- Oledbconnection conn =NewOledbconnection (strconn );
-
- Conn. open ();
-
- System. Data. oledb. oledbcommand cmd =NewOledbcommand ();
-
- Cmd. Connection = conn;
- // Cmd. commandtext = "Update [sheet1 $] Set Name =" 2005-01-01 "where employee ID =" date "";
-
- // Cmd. executenonquery ();
-
- For(IntI = 0; I <fp2.sheets [0]. rowcount-1; I ++)
-
- {
- If(Fp2.sheets [0]. cells [I, 0]. Text! ="")
-
- {
-
- Cmd. commandtext ="Insert into [sheet1 $] (employee ID, name, department, position, 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 ("An error occurred while writing data to excel :"+ Ex. Message );
-
- }
-
- Return False;
-
- }