POI Excel 03

Source: Internet
Author: User

@ Author YHC

Working thin border style:


[Java]
// Create an Excel file
Workbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("new sheet ");
 
// Create a Row object and add some cells to it. The Row subscript starts from 0.
Row row = sheet. createRow (1 );
 
// Create a cell and add the value to it
Cell cell = row. createCell (1 );
Cell. setCellValue (4 );
 
// This style is used to set the border style around the working thin area
CellStyle style = wb. createCellStyle ();
// Bottom Border
Style. setBorderBottom (CellStyle. SOLID_FOREGROUND );
// Bottom border color
Style. setBottomBorderColor (IndexedColors. BLACK. getIndex (); // BLACK
// Left border
Style. setBorderLeft (CellStyle. SOLID_FOREGROUND );
// Left border color
Style. setLeftBorderColor (IndexedColors. BLUE. getIndex (); // BLUE
// Right border
Style. setBorderRight (CellStyle. SOLID_FOREGROUND );
// Right border color
Style. setRightBorderColor (IndexedColors. YELLOW. getIndex (); // YELLOW
// Top border
Style. setBorderTop (CellStyle. BORDER_MEDIUM_DASHED); // dotted line
// Upper border color
Style. setTopBorderColor (IndexedColors. RED. getIndex (); // RED
// Set the cell style
Cell. setCellStyle (style );
 
// Write a file
FileOutputStream fileOut = new FileOutputStream ("workbook.xls ");
Wb. write (fileOut );
FileOut. close ();

// Create an Excel file
Workbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("new sheet ");

// Create a Row object and add some cells to it. The Row subscript starts from 0.
Row row = sheet. createRow (1 );

// Create a cell and add the value to it
Cell cell = row. createCell (1 );
Cell. setCellValue (4 );

// This style is used to set the border style around the working thin area
CellStyle style = wb. createCellStyle ();
// Bottom Border
Style. setBorderBottom (CellStyle. SOLID_FOREGROUND );
// Bottom border color
Style. setBottomBorderColor (IndexedColors. BLACK. getIndex (); // BLACK
// Left border
Style. setBorderLeft (CellStyle. SOLID_FOREGROUND );
// Left border color
Style. setLeftBorderColor (IndexedColors. BLUE. getIndex (); // BLUE
// Right border
Style. setBorderRight (CellStyle. SOLID_FOREGROUND );
// Right border color
Style. setRightBorderColor (IndexedColors. YELLOW. getIndex (); // YELLOW
// Top border
Style. setBorderTop (CellStyle. BORDER_MEDIUM_DASHED); // dotted line
// Upper border color
Style. setTopBorderColor (IndexedColors. RED. getIndex (); // RED
// Set the cell style
Cell. setCellStyle (style );

// Write a file
FileOutputStream fileOut = new FileOutputStream ("workbook.xls ");
Wb. write (fileOut );
FileOut. close (); effect after running:

 

 


Traverse all rows and cells:

Sometimes, you only want to traverse all rows or all cells in the workbook. This is allowed. You only need a simple loop to complete this function,

Fortunately, this is so simple. The Row object (Row) defines an internal class and CellIterator is used to process all cells in iteration (get any

Define a row, call row. cellIterator (), and the working thin provides the rowIterator () method to provide an iterator to traverse all rows.

Both Sheet (working thin) and Row (Row object) Implement the java. lang. Iterable class. Therefore, use


Built-in "foreach" support, simple use of foreach traversal, the Code is as follows:


[Java]
// Obtain the Workbook Based on the Workbook object
Sheet sheet = wb. getsheetat (0 );
// Get the row iterator and traverse all rows
For (iterator <row> rit = sheet. rowiterator (); rit. hasnext ();){
// Get the row object
Row row = rit. next ();
// Obtain all cells based on the row object and traverse all cells
For (iterator <cell> cit = row. celliterator (); cit. hasnext ();){
// Obtain the cell
Cell cell = cit. next ();
// Code for implementing the function...
}
}

// Obtain the Workbook Based on the Workbook object
Sheet sheet = wb. getsheetat (0 );
// Get the row iterator and traverse all rows
For (iterator <row> rit = sheet. rowiterator (); rit. hasnext ();){
// Get the row object
Row row = rit. next ();
// Obtain all cells based on the row object and traverse all cells
For (iterator <cell> cit = row. celliterator (); cit. hasnext ();){
// Obtain the cell
Cell cell = cit. next ();
// Code for implementing the function...
}
}


Author: yhc13429826359
 

 

 

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.