Npoi use

npoi write this office, I personally a bit inconvenient, but because of the need to use so had to use.

the reason:

1, no document

2, the online version of different cases

3, do not deal with complex row

 

Comparison with other online tools, the benefit does not depend on Microsoft's office is a set of components can be written and read to excel.

 

The first core is IWorkbook objects

It can be operated by excel in two formats, one is * .xls (HSSFWorkbook), one is * .xlsx (XSSFWorkbook)

IWorkbook wk = new HSSFWorkbook();

IWorkbook wk = new XSSFWorkbook();

  

The second target is the core of Isheet

It can be operated by two excel workbook format, one is * .xls (HSSFSheet), one is the * .xlsx (XSSFSheet)

ISheet sheet = wk.CreateSheet(“”);

Or acquiring an existing workbook

ISheet sheet = wk.GetSheetAt(int indx)

Isheet is primarily responsible for excel workbook

 

The third core is Irow / Icell Object

IRow objects can achieve control of the column

ICell object can implement control of the cell line

IRow by the need for new workbook

IRow row = sheet.CreateRow(int indx);

ICell need to explicitly listed by IRow location of the cell lines to be set, the cell can only be created by IRow

ICell cell = row.CreateCell(int indx);

Three core can be operated on a basic write excel

Case:

Wk = null IWorkbook; 

ISheet Sheet = null; 

// Get File Format 

String Extension = Path.GetExtension (filePath); 

// get a different format to be written in accordance with different versions excel 

if (extension.Equals ( ". Xls" ) ) 

{ 

    wk = HSSFWorkbook new new (); 

} 

the else 

{ 

    wk = new new XSSFWorkbook (); 

} 

// Create Excel workbook 

Sheet = wk.CreateSheet ( "new new Sheet"); 

// Create the specified column 

IRow row = sheet.CreateRow ( INDX int) 

// Create the specified row 

ICell = row.CreateCell Cell (INDX int) 

 

// make a good excel to the system generating 

the MemoryStream the MemoryStream new new MS = (); 

wk.Write (MS); 

the using (varfs newFileStream = ( filePath, FileMode.Create, FileAccess.Write, FileShare.NONE, 4096, to true)) 

{

    byte[] b = ms.ToArray();

    await fs.WriteAsync(b, 0, b.Length);

    ms.Close();

    fs.Flush();

    fs.Close();

}

  

 

Guess you like

Origin www.cnblogs.com/ancm/p/11106713.html