NPOI2.1.1 simple use, npoi2.1.1 use

Source: Internet
Author: User

NPOI2.1.1 simple use, npoi2.1.1 use

In. the NPOI class library is used to export data to Excel in. net, but there are many NPOI versions and the changes are large. The official code is incomplete, so here is a simple record.

1. Class Library package used:

using NPOI.SS.UserModel;using NPOI.HSSF.UserModel;

2. Create a worksheet (3)

HSSFWorkbook hssfworkbook = new HSSFWorkbook();ISheet sheet = hssfworkbook.CreateSheet("Sheet1");hssfworkbook.CreateSheet("Sheet2");hssfworkbook.CreateSheet("Sheet3");
3. Use CreateRow () to create a row object, CreateCell () to create a cell object, and then set the content of the cell through SetCellValue.

The following is a simple example: Set the title Font of the worksheet and merge the first row and the first five columns.

// Title IRow row = sheet. createRow (0); // create the row object ICell cell = row. createCell (0); // create the cell Object cell. setCellValue (title); // set the cell content // set the cell font ICellStyle style = hssfworkbook. createCellStyle (); style. alignment = NPOI. SS. userModel. horizontalAlignment. center; IFont font = hssfworkbook. createFont (); font. fontHeight = 20*20; style. setFont (font); cell. cellStyle = style; // merge the contents of columns 0th to columns 4th into sheet. addMergedRegion (new NPOI. SS. util. cellRangeAddress (0, 0, 0, 4 ));
4. Write the content of the WorkBook object to a file

            FileStream</span> file = new FileStream(filePath, FileMode.Create);            hssfworkbook.Write(file);            file.Close();


The following is a simple example:

/// <Summary> /// export the contents of the DataGridView to an excel file /// </summary> /// <param name = "dgv"> DataGridView object </param> /// <param name = "title"> set title </param> /// <param name = "filePath"> saved file path </param> // /<returns> </returns> public static int SaveToExcel (DataGridView dgv, string title, string filePath) {if (dgv = null | (dgv. rows. count = 0 & dgv. columns. count = 0) return 1; HSSFWorkbook hssfworkbook = new HSS FWorkbook (); ISheet sheet = hssfworkbook. createSheet ("Sheet1"); hssfworkbook. createSheet ("Sheet2"); hssfworkbook. createSheet ("Sheet3"); // Title IRow row = sheet. createRow (0); ICell cell = row. createCell (0); cell. setCellValue (title); ICellStyle style = hssfworkbook. createCellStyle (); style. alignment = NPOI. SS. userModel. horizontalAlignment. center; IFont font = hssfworkbook. createFont (); font. fontHei Ght = 20*20; style. setFont (font); cell. cellStyle = style; sheet. addMergedRegion (new NPOI. SS. util. cellRangeAddress (0, 0, 0, dgv. columns. count-1); // Header int r, c; row = sheet. createRow (1); for (c = 0; c <dgv. columns. count; c ++) {row. createCell (c ). setCellValue (dgv. columns [c]. headerText);} // content for (r = 0; r <dgv. rows. count; r ++) {row = sheet. createRow (r + 2); for (c = 0; c <dgv. columns. C Ount; c ++) {cell = row. CreateCell (c); if (dgv [c, r]. Value! = Null) cell. setCellValue (dgv [c, r]. value. toString () ;}} r = 0; FileStream file = null; try {file = new FileStream (filePath, FileMode. create); hssfworkbook. write (file);} catch (Exception ex) {MessageBox. show (ex. message); r = 1;} finally {if (file! = Null) file. Close ();} return r ;}






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.