POI技術使用的源碼

/**
 * poi使用
 */
public class TestPoi {


public static void main(String[] args) throws Exception, IOException {
//1.創建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
//2.工作表
HSSFSheet sheet = wb.createSheet("first sheet");
wb.createSheet("second sheet");
//3.行
HSSFRow row = sheet.createRow(0);
//4.單元格
HSSFCell cell = row.createCell(0);
//給單元格中賦值
cell.setCellValue(false);//boolean
row.createCell(1).setCellValue(Calendar.getInstance());//calendar
//date
row.createCell(2).setCellValue(new Date());//date
//double
row.createCell(3).setCellValue(123456789.987654);
//string
String str = "dddddddddddddd" ;
row.createCell(4).setCellValue(new HSSFRichTextString(str));



//創建數據格式對象
HSSFDataFormat format = wb.createDataFormat();
//格式化數據
HSSFCellStyle style = wb.createCellStyle();
//設置樣式的數據格式,對日期格式化
style.setDataFormat(format.getFormat("yyyy-MM-dd hh:mm:ss"));
//應用樣式給單元格
row.getCell(1).setCellStyle(style);;
row.getCell(2).setCellStyle(style);



//double值格式化
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,###.000"));
row.getCell(3).setCellStyle(style);



//設置列寬(單位:1/20 點)
sheet.setColumnWidth(1, 3000);
//自動列寬
sheet.autoSizeColumn(2);

sheet.setColumnWidth(4, 7000);

//自動迴繞文本
style = wb.createCellStyle();
style.setWrapText(true);
row.getCell(4).setCellStyle(style);

//設置文本對齊方式
row = sheet.createRow(1);
row.createCell(0).setCellValue("左上");
row.createCell(1).setCellValue("中中");
row.createCell(2).setCellValue("右下");

//設置行高
row.setHeightInPoints(50);
sheet.setColumnWidth(0, 5000);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);

//設置對其方式--左上
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左對齊
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//上對齊
row.getCell(0).setCellStyle(style);

//設置對其方式--中中
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左對齊
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上對齊
row.getCell(1).setCellStyle(style);

//設置對其方式--右下
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//左對齊
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);//上對齊
row.getCell(2).setCellStyle(style);

//設置字體顏色和大小
style = row.getCell(1).getCellStyle();
HSSFFont font = wb.createFont();
font.setFontName("方正姚體");
font.setFontHeightInPoints((short)30);
font.setItalic(true);
font.setColor(HSSFColor.RED.index);
style.setFont(font);

//字體旋轉
style = row.getCell(1).getCellStyle();
style.setRotation((short)-30);

//設置邊框
row = sheet.createRow(2);
cell = row.createCell(0);
style = wb.createCellStyle();
style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);//雙實線
style.setTopBorderColor(HSSFColor.BLUE.index);
// style.setFillBackgroundColor(HSSFColor.DARK_BLUE.index);//填充背景色
// style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);//填充前景色
cell.setCellStyle(style);

//計算列
row = sheet.createRow(3);
row.createCell(0).setCellValue(13);
row.createCell(1).setCellValue(20);
row.createCell(2).setCellValue(19);
row.createCell(3).setCellFormula("sum(A4:C4)");
//移動行
sheet.shiftRows(1, 3, 2);

//拆分窗格
//1000:左側窗格的寬度
//2000:上側窗格的高度
//3:右側窗格開始顯示列的索引
//4:下側窗格開始顯示行的索引
//0:激活的面板區
//sheet.createSplitPane(1000,2000, 3, 4, 0);

//凍結窗格
//1:左側凍結的列數
//2:上側凍結的行數
//3:右側窗格開始顯示列的索引
//4:下側窗格開始顯示行的索引
//sheet.createFreezePane(1, 2, 3, 4);
//保存
CellRangeAddress d = new CellRangeAddress(1, 3, 1, 3);
sheet.addMergedRegion(d);
wb.write(new FileOutputStream(new File("f:/poi.xls")));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章