@ 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