Preface
Previously also used in C # to Excel, with OLE DB or offic COM components, but always very troublesome, relying on more restrictions, so decisively looking for open source solutions, Java above already have very mature poi, so, found the move. NET--npoi, so that you can not rely on anything, just use this library.
Introduction
Open Source protocol: Apache 2.0
With Npoi, you can read and write Word/excel documents on a machine that does not have Office installed or the appropriate environment. Npoi is built on the POI 3.x version, which can read and write Word/excel documents without Office installation.
Use
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingNpoi. Ss. Usermodel;usingNpoi. XSSF. Usermodel;usingNpoi. HSSF. Usermodel;usingSystem.IO;usingSystem.Data;namespacenetutilitylib{ Public classexcelhelper:idisposable {Private stringFileName =NULL;//file name PrivateIworkbook Workbook =NULL; PrivateFileStream fs =NULL; Private BOOLdisposed; PublicExcelhelper (stringfileName) { This. FileName =FileName; Disposed=false; } /// <summary> ///Import DataTable data into Excel/// </summary> /// <param name= "Data" >the data to import</param> /// <param name= "Iscolumnwritten" >whether the column name of the DataTable is to be imported</param> /// <param name= "SheetName" >The name of the sheet of Excel to import</param> /// <returns>number of imported data rows (contains column name that row)</returns> Public intDatatabletoexcel (DataTable data,stringSheetName,BOOLIscolumnwritten) { inti =0; intj =0; intCount =0; Isheet sheet=NULL; FS=NewFileStream (FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); if(Filename.indexof (". xlsx") >0)//version 2007Workbook =NewXssfworkbook (); Else if(Filename.indexof (". xls") >0)//Version 2003Workbook =NewHssfworkbook (); Try { if(Workbook! =NULL) {Sheet=workbook. Createsheet (SheetName); } Else { return-1; } if(Iscolumnwritten = =true)//column name to write to DataTable{IRow row= Sheet. CreateRow (0); for(j =0; J < Data. Columns.count; ++j) {row. Createcell (j). Setcellvalue (data. COLUMNS[J]. ColumnName); } Count=1; } Else{Count=0; } for(i =0; I < data. Rows.Count; ++i) {IRow row=sheet. CreateRow (count); for(j =0; J < Data. Columns.count; ++j) {row. Createcell (j). Setcellvalue (data. ROWS[I][J]. ToString ()); } ++count; } workbook. Write (FS); //writing to Excel returncount; } Catch(Exception ex) {Console.WriteLine ("Exception:"+Ex. Message); return-1; } } /// <summary> ///import data from Excel into a DataTable/// </summary> /// <param name= "SheetName" >the name of the Excel workbook sheet</param> /// <param name= "Isfirstrowcolumn" >whether the first row is a column name for a DataTable</param> /// <returns>the DataTable returned</returns> PublicDataTable exceltodatatable (stringSheetName,BOOLisfirstrowcolumn) {Isheet sheet=NULL; DataTable Data=NewDataTable (); intStartRow =0; Try{FS=NewFileStream (FileName, FileMode.Open, FileAccess.Read); if(Filename.indexof (". xlsx") >0)//version 2007Workbook =NewXssfworkbook (FS); Else if(Filename.indexof (". xls") >0)//Version 2003Workbook =NewHssfworkbook (FS); if(SheetName! =NULL) {Sheet=workbook. Getsheet (SheetName); if(Sheet = =NULL)//if the specified sheetname corresponding sheet is not found, an attempt is taken to obtain the first sheet{Sheet= Workbook. Getsheetat (0); } } Else{Sheet= Workbook. Getsheetat (0); } if(Sheet! =NULL) {IRow firstrow= Sheet. GetRow (0); intCellcount = Firstrow.lastcellnum;//the number of the last cell in a row is the total number of columns if(isfirstrowcolumn) { for(inti = Firstrow.firstcellnum; i < Cellcount; ++i) {Icell cell=Firstrow.getcell (i); if(Cell! =NULL) { stringCellvalue =cell. Stringcellvalue; if(Cellvalue! =NULL) {DataColumn column=NewDataColumn (Cellvalue); Data. Columns.Add (column); } }} StartRow= Sheet. Firstrownum +1; } Else{StartRow=sheet. Firstrownum; } //the label of the last column intRowCount =sheet. Lastrownum; for(inti = StartRow; I <= RowCount; ++i) {IRow row=sheet. GetRow (i); if(Row = =NULL)Continue;//rows with no data default is nullDataRow DataRow=data. NewRow (); for(intj = row. Firstcellnum; J < Cellcount; ++j) {if(Row. Getcell (j)! =NULL)//Similarly, cells that do not have data are null by defaultDATAROW[J] =row. Getcell (j). ToString (); } data. Rows.Add (DataRow); } } returndata; } Catch(Exception ex) {Console.WriteLine ("Exception:"+Ex. Message); return NULL; } } Public voidDispose () {Dispose (true); Gc. SuppressFinalize ( This); } protected Virtual voidDispose (BOOLdisposing) { if(! This. Disposed) { if(disposing) {if(FS! =NULL) fs. Close (); } FS=NULL; Disposed=true; } } }}
The official tutorial is also very perfect, here refers to the Garden Friends Code, for reference only ....
Reference:
http://www.npoi.info/
Http://www.cnblogs.com/luxiaoxun/p/3374992.html
C # Open source Excel action library--npoi