@ 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