Npoi reads Excel files

Source: Internet
Author: User

Get the data in Excel and put it into datatable

 

If (this. fuupload. hasfile)
{

// Create an hssfworkbook using an existing Excel File Based on the path, that is, the entire Excel File
Hssfworkbook workbook = new hssfworkbook (this. fuupload. filecontent );

// Obtain the first sheet in Excel
Hssfsheet sheet = Workbook. getsheetat (0 );
 
Datatable table = new datatable ();
// Obtain the first line of the sheet.
Hssfrow headerrow = sheet. getrow (0 );

// The number of the last square in a row, that is, the total number of Columns
Int cellcount = headerrow. lastcellnum;

For (INT I = headerrow. firstcellnum; I <cellcount; I ++)
{
Datacolumn column = new datacolumn (headerrow. getcell (I). stringcellvalue );
Table. Columns. Add (column );
}
// The label of the last column, that is, the total number of rows
Int rowcount = sheet. lastrownum;

For (INT I = (sheet. firstrownum + 1); I <sheet. lastrownum; I ++)
{
Hssfrow ROW = sheet. getrow (I );
Datarow = table. newrow ();

For (Int J = row. firstcellnum; j <cellcount; j ++)
{
If (row. getcell (j )! = NULL)
Datarow [J] = row. getcell (j). tostring ();
}

Table. Rows. Add (datarow );
}

Workbook = NULL;
Sheet = NULL;

This. gvexcel. datasource = table;
This. gvexcel. databind ();
}

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.