Npoi operation Excel 002: Reading Excel

Source: Internet
Author: User
Tags rowcount

This article describes how to read Excel through Npoi.
DLLs that need to be prepared see: http://blog.csdn.net/yysyangyangyangshan/article/details/42614181
Environment. Net2.0,excel Version 2003.
Npoi reading Excel is simple, just grab a few main points of Excel.
The general Excel consists of these parts, the Book,sheet page, and then the column in the sheet page. Reading Excel is the first to find the book, and then the sheet, and then according to sheet in the first few rows of the column to read the content.
Look directly at the code below.
Let's look at a method that is primarily used to read Excel content. After reading this method, you can use code to read the contents of Excel flexibly.
Here's how to read the contents of an Excel table as a DataTable. Of course, in practice, it is not necessary to read as a DataTable, to read a row, a column, or a cell to read separately.
    <summary>///Read Excel Save As DataTable///</summary>//<param name= "FilePath" ; Excel file path </param>///<param Name= "StartRow" > First few lines start reading </param>//<returns></retur ns> public static DataTable doreadexceldatatable (String Filepath,int startrow) {DataTable dt            = new DataTable (); if (!            File.exists (FilePath)) {return dt;            } Hssfworkbook workbook = null;            Hssfsheet sheet = null;            FileStream fs = new FileStream (FilePath, FileMode.Open, FileAccess.Read);            workbook = new Hssfworkbook (FS); Sheet = workbook.            Getsheetat (0); Hssfrow row1 = sheet.            GetRow (StartRow); int cellcount = Row1.            Lastcellnum; This is where the column name is read, and if the column name is not required, comment this code for (int i = Row1. Firstcellnum; I < Row1. Lastcellnum; i++) {DataColumn Columitem = new DataColumn (row1.Getcell (i).                Stringcellvalue); Dt.            Columns.Add (Columitem); } int rowCount = sheet.            Lastrownum; for (int i = (sheet. Firstrownum + 1); I <= sheet. Lastrownum; i++) {Hssfrow row = sheet.                GetRow (i); DataRow dtrow = dt.                NewRow (); for (int j = row. Firstcellnum; J < Cellcount; J + +) {if (row. Getcell (j)! = null) {Dtrow[j] = row. Getcell (j).                    ToString (); }} dt.            Rows.Add (Dtrow);            } sheet = null;            workbook = null;        return DT; }
As the code above, reading Excel simply needs to follow the Book-sheet-row or cell to get the content you want.

Here's an example to see how to read it in detail.
For example, the following reading of the Excel content is completely taken out and displayed on the page.

This code is as follows:
Read the class for the Excel method:
 public class Npoihelper {//<summary>//Read Excel Save As DataTable///</summary>        <param name= "FilePath" >excel file path </param>///<param Name= "StartRow" > First few lines start reading </param>         <returns></returns> public static DataTable doreadexceldatatable (String filepath,int startrow)            {DataTable dt = new DataTable (); if (!            File.exists (FilePath)) {return dt;            } Hssfworkbook workbook = null;            Hssfsheet sheet = null;            FileStream fs = new FileStream (FilePath, FileMode.Open, FileAccess.Read);            workbook = new Hssfworkbook (FS); Sheet = workbook.            Getsheetat (0); Hssfrow row1 = sheet.            GetRow (StartRow); int cellcount = Row1.            Lastcellnum; This is where the column name is read, and if the column name is not required, comment this code for (int i = Row1. Firstcellnum; I < Row1. Lastcellnum; i++) {DataColumn Columitem = new DataColumn (row1. Getcell (i).                Stringcellvalue); Dt.            Columns.Add (Columitem); } int rowCount = sheet.            Lastrownum; for (int i = (sheet. Firstrownum + 1); I <= sheet. Lastrownum; i++) {Hssfrow row = sheet.                GetRow (i); DataRow dtrow = dt.                NewRow (); for (int j = row. Firstcellnum; J < Cellcount; J + +) {if (row. Getcell (j)! = null) {Dtrow[j] = row. Getcell (j).                    ToString (); }} dt.            Rows.Add (Dtrow);            } sheet = null;            workbook = null;        return DT; }    }

Background code:
   public partial class _default:system.web.ui.page    {        protected void Page_Load (object sender, EventArgs e)        { C4/>if (! IsPostBack)            {                initialpage ();            }        }        private void Initialpage ()        {            string excelfile = HttpRuntime.AppDomainAppPath.ToString ();            Excelfile + = @ "\excelfile\testnpoiread.xls";            DataTable dt = npoihelper.doreadexceldatatable (excelfile, 0);            Dgexceldatas.datasource = DT;            Dgexceldatas.databind ();        }    }

Front Display code:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Testnopireadexcel._default" %><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
As follows:


Project code Download: http://download.csdn.net/detail/yysyangyangyangshan/8351175

Npoi operation Excel 002: Reading 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.