OLE DB Operations Excel

Source: Internet
Author: User
Tags file copy ole

Ways to manipulate Excel using OLE DB

OleDbConnection conn = null;
Try
{
filename represents the file path of Excel to manipulate, and if Excel does not exist, it is now created and can be created through a template file copy.
String strconn;
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data source=" + FileName + ";" +
"Extended properties= ' Excel 8.0; Hdr=no;imex=0 ' ";

conn = new OleDbConnection (strconn);
Conn. Open ();
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand ();
Cmd. Connection = conn;
Insert data from A3 to H3 in Excel Sheet1
Cmd.commandtext = "INSERT INTO [Sheet1$a3:h3] (F1,F2,F3,F4,F5,F6,F7,F8) VALUES ('" + a200. Date + "', '"
+ a200. Previouscloseprice + "', '" + a200. Openprice + "', '" + a200. High + "', '" + a200. Low + "', '" + a200. Close + "', '" +
A200. Change + "', '" + a200. Changerate + "')";
Cmd. ExecuteNonQuery ();
Conn. Close ();
}
catch (Exception e)
{
IF (conn! = null)
Conn. Close ();
Console.WriteLine (E.tostring ());
}

Note:
1) When using an Excel workbook, by default, the first row in the range is the header row (or field name). If the first range does not contain a caption, you can specify Hdr=no in the extended properties of the connection string.
If you specify Hdr=no,jet in the connection string, the OLE DB provider will automatically name the field for you (F1 represents the first field, F2 represents the second field, and so on);
2) Imex=1 all read-in data
As characters, other values (0, 2) Please refer to the relevant Help documentation; 3) If an "installable ISAM cannot be found" error occurs, the connection string is usually an error.

3. Reading data from an Excel file
String sql = "SELECT * FROM [sheet1$]";
Doolesql (SQL, "Test.xls");

4. Update the data in the Excel file
String sql = "Update [sheet1$] set fieldname1= ' 333 ' where fieldname2= ' B3 '";
Doolesql (SQL, "Test.xls");

5. Inserting data into an Excel file
String sql = "INSERT INTO [sheet1$] (fieldname1,fieldname2,...) VALUES (' A ', ' B ',...) ';
Doolesql (SQL, "Test.xls");

6. Delete data from Excel files: This method is not recommended
7, for non-standard structure of Excel table, you can specify the scope of sheet in Excel
1) Read data: String sql = "SELECT * FROM [Sheet1$a3:f20]";
2) Update data: String sql = "Update [SHEET1$A9:F15] set fieldname= ' 333 ' where anotherfieldname= ' B3 '";
3) Insert data: String sql = "INSERT INTO [SHEET1$A9:F15] (fieldname1,fieldname2,...) VALUES (' A ', ' B ',...) ';
4) Delete data: do not advocate
Note: 1) The code can be modified on its own, 2) if the "Action must use an updatable query" error occurs, there may be an error in the "field" reference in the SQL statement to the Excel file, or the Excel file does not
Have "modify" permission; 3) If the "cannot extend the selected range" error appears, there may be an error in the "scope" referenced by the Excel file.

OLE DB Operations Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.