POI Excel 09

Source: Internet
Author: User

@ Author YHC

Format cell data:


[Java]
// Create an Excel file
Workbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("format sheet ");
// Create a style
CellStyle style;
// Create a formatted data object
DataFormat format = wb. createDataFormat ();
// Create a row
Row row;
// Create a cell
Cell cell;
//
Short rowNum = 0;
//
Short colNum = 0;
// Create a row
Row = sheet. createRow (rowNum ++ );
// Create a cell
Cell = row. createCell (colNum );
// Set the Cell value
Cell. setCellValue (11111.25 );
// Create a style
Style = wb. createCellStyle ();
// Set the format of the data and retain a decimal number.
Style. setDataFormat (format. getFormat ("0.0 "));
// Apply this style
Cell. setCellStyle (style );

// Create a new row
Row = sheet. createRow (rowNum ++ );
// Create a cell
Cell = row. createCell (colNum );
// Set the value
Cell. setCellValue (11111.25 );
// Create a style
Style = wb. createCellStyle ();
// Set the format of the formatted data. "#" in the format indicates that if the character exists, the character is displayed. If the character does not exist, the character is not displayed.
// 0 indicates that the bit is displayed if it has a value. If it does not, the value is 0.
Style. setDataFormat (format. getFormat ("#,## 0.0000 "));
// Apply this 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 ("format sheet ");
// Create a style
CellStyle style;
// Create a formatted data object
DataFormat format = wb. createDataFormat ();
// Create a row
Row row;
// Create a cell
Cell cell;
//
Short rowNum = 0;
//
Short colNum = 0;
// Create a row
Row = sheet. createRow (rowNum ++ );
// Create a cell
Cell = row. createCell (colNum );
// Set the Cell value
Cell. setCellValue (11111.25 );
// Create a style
Style = wb. createCellStyle ();
// Set the format of the data and retain a decimal number.
Style. setDataFormat (format. getFormat ("0.0 "));
// Apply this style
Cell. setCellStyle (style );

// Create a new row
Row = sheet. createRow (rowNum ++ );
// Create a cell
Cell = row. createCell (colNum );
// Set the value
Cell. setCellValue (11111.25 );
// Create a style
Style = wb. createCellStyle ();
// Set the format of the formatted data. "#" in the format indicates that if the character exists, the character is displayed. If the character does not exist, the character is not displayed.
// 0 indicates that the bit is displayed if it has a value. If it does not, the value is 0.
Style. setDataFormat (format. getFormat ("#,## 0.0000 "));
// Apply this style
Cell. setCellStyle (style );

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

 

 

 

Set the print area:


[Java]
// Create an Excel file
Orkbook wb = new HSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ("Sheet1 ");
// Method 1
/// Wb. setPrintArea (0, "$ A $1: $ C $2 ");
 
// Method 2 for setting the print area on the first workbook
// The following four parameters and merged cell parameters mean the same.
Wb. setPrintArea (
0, // starts from 0
0, // start column subscript 0
1, // start with 0 ending column subscript
0, // start row subscript 0
0 // start with 0 ending row subscript
);
 
// 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 ("Sheet1 ");
// Method 1
/// Wb. setPrintArea (0, "$ A $1: $ C $2 ");

// Method 2 for setting the print area on the first workbook
// The following four parameters and merged cell parameters mean the same.
Wb. setPrintArea (
0, // starts from 0
0, // start column subscript 0
1, // start with 0 ending column subscript
0, // start row subscript 0
0 // start with 0 ending row subscript
);

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

 


The above should be said that the cell line color is the printing area;

If there are any errors above, please point them out! Thanks!

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.