POI Excel 10 _ tool class function, move the Row up and down in Sheet, set Sheet to select

Source: Internet
Author: User

@ Author YHC

Convenience functions:

These convenience functions provide practical program functions, such as setting the area for table border merging, and changing style attributes, rather than re-creating a new style:


[Java]
// Create an Excel file
Workbook wb = new HSSFWorkbook (); // or create a new XSSFWorkbook ()
// Create a workbook
Sheet sheet1 = wb. createSheet ("new sheet ");
 
// Create a merged Region
// Create a row
Row row = sheet1.createRow (1 );
// Create a row
Row row2 = sheet1.createRow (2 );
// Create a cell
Cell cell = row. createCell (1 );
// Set the cell value
Cell. setCellValue ("This is a test of merging ");
// Create a merged Region
CellRangeAddress region = CellRangeAddress. valueOf ("B2: E5 ");
// Add a merged area to the workbook.
Sheet1.addMergedRegion (region );
 
// Set the cell border and color
Final short borderMediumDashed = CellStyle. BORDER_MEDIUM_DASHED;
// Bottom border
RegionUtil. setBorderBottom (borderMediumDashed, region, sheet1, wb );
// Border
RegionUtil. setBorderTop (borderMediumDashed, region, sheet1, wb );
// Border on the Left border
RegionUtil. setBorderLeft (borderMediumDashed, region, sheet1, wb );
// Border on the Right border
RegionUtil. setBorderRight (borderMediumDashed, region, sheet1, wb );
// Bottom border color
RegionUtil. setBottomBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Upper border color
RegionUtil. setTopBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Left border color
RegionUtil. setLeftBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Right border color
RegionUtil. setRightBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
 
// Display the usage of some CellUtil tool classes
CellStyle style = wb. createCellStyle ();
// Set indentation
Style. setIndention (short) 4 );
// Create cells, specify values, and styles
CellUtil. createCell (sheet1.createRow (7), 1,
"This is the value of the cell", style );
// Create the second cell and specify the value
Cell cell2 = CellUtil. createCell (sheet1.createRow (8), 1,
"This is the value of the cell ");
// Set the cell horizontal center Style
CellUtil. setAlignment (cell2, wb, CellStyle. ALIGN_CENTER );
 
// Write a file
FileOutputStream fileOut = new FileOutputStream ("workbook.xls ");
Wb. write (fileOut );
FileOut. close ();

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

// Create a merged Region
// Create a row
Row row = sheet1.createRow (1 );
// Create a row
Row row2 = sheet1.createRow (2 );
// Create a cell
Cell cell = row. createCell (1 );
// Set the cell value
Cell. setCellValue ("This is a test of merging ");
// Create a merged Region
CellRangeAddress region = CellRangeAddress. valueOf ("B2: E5 ");
// Add a merged area to the workbook.
Sheet1.addMergedRegion (region );

// Set the cell border and color
Final short borderMediumDashed = CellStyle. BORDER_MEDIUM_DASHED;
// Bottom border
RegionUtil. setBorderBottom (borderMediumDashed, region, sheet1, wb );
// Border
RegionUtil. setBorderTop (borderMediumDashed, region, sheet1, wb );
// Border on the Left border
RegionUtil. setBorderLeft (borderMediumDashed, region, sheet1, wb );
// Border on the Right border
RegionUtil. setBorderRight (borderMediumDashed, region, sheet1, wb );
// Bottom border color
RegionUtil. setBottomBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Upper border color
RegionUtil. setTopBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Left border color
RegionUtil. setLeftBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );
// Right border color
RegionUtil. setRightBorderColor (IndexedColors. AQUA. getIndex (), region,
Sheet1, wb );

// Display the usage of some CellUtil tool classes
CellStyle style = wb. createCellStyle ();
// Set indentation
Style. setIndention (short) 4 );
// Create cells, specify values, and styles
CellUtil. createCell (sheet1.createRow (7), 1,
"This is the value of the cell", style );
// Create the second cell and specify the value
Cell cell2 = CellUtil. createCell (sheet1.createRow (8), 1,
"This is the value of the cell ");
// Set the cell horizontal center Style
CellUtil. setAlignment (cell2, wb, CellStyle. ALIGN_CENTER );

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

 

 


Move the Row position in the workbook


[Java]
// Create an Excel file
Workbook wb = new HSSFWorkbook (); // or create a new XSSFWorkbook ()
// Create a workbook
Sheet sheet = wb. createSheet ("row sheet ");
 
// Create cells in order
For (int I = 0; I <11; I ++ ){
Row row = sheet. createRow (I );
Cell cell = row. createCell (0 );
Cell. setCellValue (I );
}

// Move the Row 6-11 to 0-5 on the top of the workbook. Note that the row will overwrite 0-5.
Sheet. shiftRows (5, 10,-5 );
// Write a file
FileOutputStream fileOut = new FileOutputStream ("workbook.xls ");
Wb. write (fileOut );
FileOut. close ();

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

// Create cells in order
For (int I = 0; I <11; I ++ ){
Row row = sheet. createRow (I );
Cell cell = row. createCell (0 );
Cell. setCellValue (I );
}

// Move the Row 6-11 to 0-5 on the top of the workbook. Note that the row will overwrite 0-5.
Sheet. shiftRows (5, 10,-5 );
// Write the file www.2cto.com
FileOutputStream fileOut = new FileOutputStream ("workbook.xls ");
Wb. write (fileOut );
FileOut. close (); comparison before and after moving: Before and After moving, note:

 

 


Set workbook Selection


[Java]
Workbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("row sheet ");
// Set the selected
Sheet. setSelected (true );

Workbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("row sheet ");
// Set the selected
Sheet. setSelected (true );
If there is a write error, please point it out! Thanks
Author: yhc13429826359
 

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.