java poi createcell_java POI创建Excel示例(xslx和xsl区别 )

1 packagecom.caoyuanzhanlang.excelhandle;2

3 importjava.io.File;4 importjava.io.FileOutputStream;5

6 importorg.apache.poi.hssf.util.HSSFColor;7 importorg.apache.poi.ss.usermodel.Cell;8 importorg.apache.poi.ss.usermodel.CellStyle;9 importorg.apache.poi.ss.usermodel.Font;10 importorg.apache.poi.ss.usermodel.Row;11 importorg.apache.poi.ss.usermodel.Sheet;12 importorg.apache.poi.ss.usermodel.Workbook;13 importorg.apache.poi.xssf.usermodel.XSSFWorkbook;14

15 public classNewExcel {16

17 private String excelPath = "data.xlsx";18

19 public static void main(String[] args) throwsException{20 NewExcel excel = newNewExcel();21 if(excel.createExcelFile()) {22 System.out.println("data.xlsx is created successfully.");23 }24 }25 public booleancreateExcelFile() {26 boolean isCreateSuccess = false;27 Workbook workbook = null;28 try{29 //XSSFWork used for .xslx (>= 2007), HSSWorkbook for 03 .xsl

30 workbook = new XSSFWorkbook();//HSSFWorkbook();//WorkbookFactory.create(inputStream);

31 }catch(Exception e) {32 System.out.println("It cause Error on CREATING excel workbook: ");33 e.printStackTrace();34 }35 if(workbook != null) {36 Sheet sheet = workbook.createSheet("testdata");37 Row row0 = sheet.createRow(0);38 for(int i = 0; i < 11; i++) {39 Cell cell_1 =row0.createCell(i, Cell.CELL_TYPE_STRING);40 CellStyle style =getStyle(workbook);41 cell_1.setCellStyle(style);42 cell_1.setCellValue("HELLO" + i + "Column");43 sheet.autoSizeColumn(i);44 }45 for (int rowNum = 1; rowNum < 200; rowNum++) {46 Row row =sheet.createRow(rowNum);47 for(int i = 0; i < 11; i++) {48 Cell cell =row.createCell(i, Cell.CELL_TYPE_STRING);49 cell.setCellValue("cell" + String.valueOf(rowNum+1) + String.valueOf(i+1));50 }51 }52 try{53 FileOutputStream outputStream = newFileOutputStream(excelPath);54 workbook.write(outputStream);55 outputStream.flush();56 outputStream.close();57 isCreateSuccess = true;58 } catch(Exception e) {59 System.out.println("It cause Error on WRITTING excel workbook: ");60 e.printStackTrace();61 }62 }63 File sss = newFile(excelPath);64 System.out.println(sss.getAbsolutePath());65 returnisCreateSuccess;66 }67 privateCellStyle getStyle(Workbook workbook){68 CellStyle style =workbook.createCellStyle();69 style.setAlignment(CellStyle.ALIGN_CENTER);70 style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);71 //设置单元格字体

72 Font headerFont = workbook.createFont(); //字体

73 headerFont.setFontHeightInPoints((short)14);74 headerFont.setColor(HSSFColor.RED.index);75 headerFont.setFontName("宋体");76 style.setFont(headerFont);77 style.setWrapText(true);78

79 //设置单元格边框及颜色

80 style.setBorderBottom((short)1);81 style.setBorderLeft((short)1);82 style.setBorderRight((short)1);83 style.setBorderTop((short)1);84 style.setWrapText(true);85 returnstyle;86 }87 publicString getExcelPath() {88 return this.excelPath;89 }90

91 public voidsetExcelPath(String excelPath) {92 this.excelPath =excelPath;93 }94 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值