C # about importing Execl

Source: Internet
Author: User

Because of the work needs, often to add new products, involving a large number of the need to consider batch processing.

This driver corresponds to the EXECL format of XLS

String strconn = "provider=microsoft.jet.oledb.4.0;" + "Data source=" + Filenameurl + "; Extended properties= ' Excel 8.0; Hdr=yes; Imex=1 ' ";

This driver corresponds to the EXECL format of xlsx hdr=yes/no with title/no caption IMEX=0/1/2 write/read/write read all available

String strconn = "Provider=microsoft.ace.oledb.12.0;data source=" + Filenameurl + "; Extended properties= ' Excel 12.0; Hdr=yes;imex=1, ' ";

There are still problems with the way that OLE DB is called, mainly when there are 10 or more file types in a column that are numbers, and then the numbers have letters that cannot be read. Even if setting this column text is not valid, some people say you can modify the value inside the registry. But I'm looking for a registry that doesn't have those nodes, and this method is only valid if you want to install Office. Quite a few limitations. Find information to see the predecessors recommended Npoi, find the official website study:

Official website: Http://npoi.codeplex.com/SourceControl/latest#NPOI.Examples/ImportXlsToDataTable/Form1.cs

The first step is to download the components and import the NPOI.dll.

The second section refers to namespaces. Using Npoi. HSSF.   Usermodel; Using Npoi. Ss. Usermodel;
The third step, describes the Workbook object: Hssfworkbook Hssfworkbook;

Fourth, read the contents of the data:

protected void Button1_Click (object sender, EventArgs e)

{//Open. xls file

using (FileStream fs = File.openread (@ "E:/*.xls"))

{

Hssfworkbook wk = new Hssfworkbook (FS); Writes the data in the XLS file to wk

for (int i = 0; i < wk. Numberofsheets; i++)//numberofsheets is the total number of tables in *.xls

{

Isheet sheet = wk.   Getsheetat (i); Reading the current table data

for (int j = 0; J <= sheet. Lastrownum; J + +)//lastrownum is the total number of rows in the current table

{

IRow row = sheet.  GetRow (j); Read current row data

if (row! = null)

{

for (int k = 0; k <= Row. Lastcellnum; k++)//lastcellnum is the total number of columns in the current row

{

                                                               icell cell = row. Getcell (k); //current form                                                        

if (cell! = NULL)

{//Read cells as cell values for further processing

}

}

}

}

}

}

}

The above is simply reading the values of the cells in the execl, pending further learning to manipulate the document using Npoi.

C # about importing Execl

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.