C # using NPOI to Operate Excel,

Source: Internet
Author: User

C # using NPOI to Operate Excel,

Reference page:

Http://www.yuanjiaocheng.net/Jsp/first.html

Http://www.yuanjiaocheng.net/Struts/first.html

Http://www.yuanjiaocheng.net/Hibernate/first.html

Http://www.yuanjiaocheng.net/Entity/first.html

Http://www.yuanjiaocheng.net/Entity/jieshao.html

C # There are many ways to Operate Excel. Common methods include Microsoft's official OLE Automation and Apache POI. This section describes how to translate POI into NPOI of C.

POI is an API for Apache to operate Office through Java. It is very powerful for Excel, Word, and PPT operations. Then it is translated into NPOI of C #, which is similar to log4j and log4net.

It seems that office2007 and later XML formats are not supported before NPOI. net4.0, but the latest version is supported. You only need to download and reference the following five sets.

Here is a class for Excel operations. The class provides four methods, two exports, and two imports. You can use DataSet to export an Excel file with multiple sheets or DataTable to export an Excel file with one Sheet. The same is true for importing data. You can specify the Sheet index to export the able, or directly export all sheets to return a DataSet.

  

Public class ExcelHelper {// <summary> /// DataTable is returned Based on Excel and Sheet /// </summary> /// <param name = "filePath"> Excel file address </param> /// <param name = "sheetIndex"> Sheet index </param> /// <returns> DataTable </returns> public static DataTable GetDataTable (string filePath, int sheetIndex) {return GetDataSet (filePath, sheetIndex ). tables [0] ;}/// <summary> /// return DataSet /// </summary> /// <param name = "f IlePath "> Excel file address </param> // <param name =" sheetIndex "> Sheet Index, optional, all Sheet </param> /// <returns> DataSet </returns> public static DataSet GetDataSet (string filePath, int? SheetIndex = null) {DataSet ds = new DataSet (); IWorkbook fileWorkbook; using (FileStream fs = new FileStream (filePath, FileMode. open, FileAccess. read) {if (filePath. last () ='s ') {try {fileWorkbook = new HSSFWorkbook (fs);} catch (Exception ex) {throw ex ;}} else {try {fileWorkbook = new XSSFWorkbook (fs);} catch {fileWorkbook = new HSSFWorkbook (fs) ;}}for (int I = 0; I <fileWorkb Ook. NumberOfSheets; I ++) {if (sheetIndex! = Null & sheetIndex! = I) continue; DataTable dt = new DataTable (); ISheet sheet = fileWorkbook. getSheetAt (I); // header IRow header = sheet. getRow (sheet. firstRowNum); List <int> columns = new List <int> (); for (int j = 0; j 
Related Article

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.