Asp.net Excel file operation instance

Source: Internet
Author: User

Share an article about. net connection operations for excel databases and data reading operations. If you have any need, refer to this example.

The Code is as follows: Copy code
Provider = Microsoft. Jet. OleDb.4.0; Data Source = Excel file location; Extended Properties = 'excel 8.0; HDR = NO ;'

In Extended Properties, Excel 8.0 indicates the version number. Microsoft Jet 4.0 ole db Provider supports the Excel 3.0, 4.0, 5.0, and 8.0 database types. That is to say, it cannot connect to an Excel 2007 file. For an Excel 2007 file, you can save it as 97-2003.

HDR = NO indicates that the Excel area does not contain the title, that is, the first row of the area is the data row. In this case, F1 indicates the first field, F2 indicates the second field, and so on.

There are still many details about using this database-like method to Operate Excel, which will be described in the following articles.

Note:

Excel is also an attribute file-type database. release resources after use.

Instance operations

 

The Code is as follows: Copy code

String SQL = @ "select * from [Sheet1 $]";

OleDbConnection conn = new OleDbConnection ("Provider = Microsoft. Jet. OleDb.4.0; Data Source =" +
Server. MapPath ("foo.xls") + ";" +
"Extended Properties = 'excel 8.0; HDR = NO ;'");
Conn. Open ();
OleDbCommand cmd = new OleDbCommand (SQL, conn );
OleDbDataReader reader = cmd. ExecuteReader ();

While (reader. Read ())
{
Response. Write (reader. GetString (0) + "<br/> ");
}

Reader. Close ();
Reader. Dispose ();
Cmd. Dispose ();
Conn. Close ();
Conn. Dispose ();

Note the Data Type

If no data type is specified for the cell in Excel, OLEDB obtains the Data Type of the current cell by guessing. By default, the Data Type of a cell is predicted by scanning the first 8 rows of the current row. To change the default number of scanned rows, specify the corresponding value for MAXSCANROWS in the connection string extension attribute.

If we specify the cell data type as a number, the data is automatically converted to the number type after being written to Excel, even if it is inserted as a string in an SQL statement, for example: set F18 = '000000 '.

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.