@author YHC
格式化單元格數據:
[java]
//創建Excel
Workbook wb = new HSSFWorkbook();
//創建工作薄
Sheet sheet = wb.createSheet("format sheet");
//創建樣式
CellStyle style;
//創建格式化數據對象
DataFormat format = wb.createDataFormat();
//創建行
Row row;
//創建單元格
Cell cell;
//
short rowNum = 0;
//
short colNum = 0;
//創建行
row = sheet.createRow(rowNum++);
//創建單元格
cell = row.createCell(colNum);
//設置Cell值
cell.setCellValue(11111.25);
//創建樣式
style = wb.createCellStyle();
//設置格式化數據樣式,保留一位小數
style.setDataFormat(format.getFormat("0.0"));
//應用此樣式
cell.setCellStyle(style);
//創建新行
row = sheet.createRow(rowNum++);
//創建單元格
cell = row.createCell(colNum);
//設置值
cell.setCellValue(11111.25);
//創建樣式
style = wb.createCellStyle();
//設置格式化數據格式, 模式中的"#"表示如果該位存在字符,則顯示字符,如果不存在,則不顯示。
//0表示該位有值則顯示,沒有則補0
style.setDataFormat(format.getFormat("#,##0.0000"));
//應用此樣式
cell.setCellStyle(style);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
//創建Excel
Workbook wb = new HSSFWorkbook();
//創建工作薄
Sheet sheet = wb.createSheet("format sheet");
//創建樣式
CellStyle style;
//創建格式化數據對象
DataFormat format = wb.createDataFormat();
//創建行
Row row;
//創建單元格
Cell cell;
//
short rowNum = 0;
//
short colNum = 0;
//創建行
row = sheet.createRow(rowNum++);
//創建單元格
cell = row.createCell(colNum);
//設置Cell值
cell.setCellValue(11111.25);
//創建樣式
style = wb.createCellStyle();
//設置格式化數據樣式,保留一位小數
style.setDataFormat(format.getFormat("0.0"));
//應用此樣式
cell.setCellStyle(style);
//創建新行
row = sheet.createRow(rowNum++);
//創建單元格
cell = row.createCell(colNum);
//設置值
cell.setCellValue(11111.25);
//創建樣式
style = wb.createCellStyle();
//設置格式化數據格式, 模式中的"#"表示如果該位存在字符,則顯示字符,如果不存在,則不顯示。
//0表示該位有值則顯示,沒有則補0
style.setDataFormat(format.getFormat("#,##0.0000"));
//應用此樣式
cell.setCellStyle(style);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
運行後效果:
設置打印區域:
[java]
//創建Excel
orkbook wb = new HSSFWorkbook();
//創建工作簿
Sheet sheet = wb.createSheet("Sheet1");
//設置第一個工作簿上的打印區域 方法1
/// wb.setPrintArea(0, "$A$1:$C$2");
//設置第一個工作簿上的打印區域 方法2
//這裡後面的四個參數和合並單元格的參數的意思是一樣的
wb.setPrintArea(
0, //工作薄 下標0開始
0, //起始列 下標0開始
1, //終止列 下標0開始
0, //起始行 下標0開始
0 //終止行 下標0開始
);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
//創建Excel
Workbook wb = new HSSFWorkbook();
//創建工作簿
Sheet sheet = wb.createSheet("Sheet1");
//設置第一個工作簿上的打印區域 方法1
/// wb.setPrintArea(0, "$A$1:$C$2");
//設置第一個工作簿上的打印區域 方法2
//這裡後面的四個參數和合並單元格的參數的意思是一樣的
wb.setPrintArea(
0, //工作薄 下標0開始
0, //起始列 下標0開始
1, //終止列 下標0開始
0, //起始行 下標0開始
0 //終止行 下標0開始
);
//寫入文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
設置後的效果圖:
上面應該說單元格的線條變色的是打印區域;
如果以上有錯誤的地方還請大傢指出!thanks!
作者:yhc13429826359