java poi 模板_java-poi创建模板

packagecom.jy.demo.web;importjava.io.FileOutputStream;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.ss.util.CellRangeAddress;importorg.apache.poi.xssf.usermodel.XSSFCellStyle;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classTestExcel {public static void main(String[] args) throwsException {

Workbook wb= new XSSFWorkbook(); //or new XSSFWorkbook();

Sheet sheet= wb.createSheet("sheet1");

Row rowt= sheet.createRow(0);

Cell cellt= rowt.createCell(0);

cellt.setCellValue("学号");

Cell cell1t= rowt.createCell(1);

cell1t.setCellValue("姓名");

Cell cell2t11= rowt.createCell(2);

cell2t11.setCellValue("性别");

Cell cell2t= rowt.createCell(3);

cell2t.setCellValue("科目");

Cell cell2t1= rowt.createCell(4);

cell2t1.setCellValue("成绩");//创建单元格样式对象

XSSFCellStyle alignStyle =(XSSFCellStyle) wb.createCellStyle();

alignStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//居中对齐

alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

cellt.setCellStyle(alignStyle);int y = 2;int num = 0;for (int i = 1; i <= 30; i = i + 3) {

num++;

Row row1=sheet.createRow(i);

Cell cell1= row1.createCell(0);

sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + y + ":$A$"+ (y + 2)));

cell1.setCellValue("00" +num);

cell1.setCellStyle(alignStyle);

Cell cell2= row1.createCell(1);

sheet.addMergedRegion(CellRangeAddress.valueOf("$B$" + y + ":$B$"+ (y + 2)));

cell2.setCellValue("张三");

cell2.setCellStyle(alignStyle);

Cell cell21= row1.createCell(2);

sheet.addMergedRegion(CellRangeAddress.valueOf("$C$" + y + ":$C$"+ (y + 2)));

cell21.setCellValue("男");

cell21.setCellStyle(alignStyle);

y= y + 3;

Cell cell3= row1.createCell(3);

cell3.setCellValue("语文");

Row row2= sheet.createRow(i + 1);

Cell cellr2= row2.createCell(3);

cellr2.setCellValue("数学");

Row row3= sheet.createRow(i + 2);

Cell cellr3= row3.createCell(3);

cellr3.setCellValue("英语");

Cell cell4= row1.createCell(4);

cell4.setCellValue("1222");

Cell cell41= row2.createCell(4);

cell41.setCellValue("98");

Cell cell411= row3.createCell(4);

cell411.setCellValue("981");

}

FileOutputStream fileOut= new FileOutputStream("E:\\workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值