This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

1. Background

Yang Lele began to be in a daze again, he found that the work is very easy, most of the time is CRUD, but more than CRUD is a more awesome way, after all, a high-end programmer, is the use of the simplest programming method, CV da method. Lele also found the problem, however, always copy and paste, also let oneself many basic things to remember, and if left the project to do, like export a excel, always paste others, although very simple, but after a long time still don’t understand, so lele this no paste of other people’s code, wrote one yourself.

Ii. Derived Practice

HSSFWorkbook workbook = new HSSFWorkbook(); // Create a sheet HSSFSheet sheet0 = workbook.createsheet ("sheet0"); // Create a row HSSFRow row = sheet0.createrow (0); CellStyle cellStyle = buildCellStyle(workbook); // Create a different column on the created row row.createcell (0).setCellValue(" route code "); Row.createcell (1).setcellValue (" English ship name "); Row.createcell (2).setcellValue (" Chinese ship name "); Row.createcell (3).setCellValue(" import voyage "); Row.createcell (4).setCellValue(" exit voyage "); Row.createcell (5).setcellValue (" pre-arrival "); Row.createcell (6).setCellValue(" Arrival time "); Row. CreateCell (7). SetCellValue (" Hong Kong "); Row. CreateCell (8). SetCellValue (" the committee "); for (int i = 0; i < 9; i++) { row.getCell(i).setCellStyle(cellStyle); sheet0.setColumnWidth(i, 252 * 15 + 900); } for (int i = 0; i < sailingScheduleEntitys.size(); i++) { HSSFRow row1 = sheet0.createRow(i + 1); row1.createCell(0).setCellValue(sailingScheduleEntitys.get(i).getTradeRule().getCode()); row1.createCell(1).setCellValue(sailingScheduleEntitys.get(i).getShipEnName()); row1.createCell(2).setCellValue(sailingScheduleEntitys.get(i).getShipCnName()); row1.createCell(3).setCellValue(sailingScheduleEntitys.get(i).getImpVoyageCode()); row1.createCell(4).setCellValue(sailingScheduleEntitys.get(i).getExpVoyageCode()); row1.createCell(5).setCellValue(sailingScheduleEntitys.get(i).getEtdArrivedBerth()); row1.createCell(6).setCellValue(threadLocal.get().format(sailingScheduleEntitys.get(i).getEtdArrivedDate())); row1.createCell(7).setCellValue(sailingScheduleEntitys.get(i).getTradeRule().getLastPortName()); row1.createCell(8).setCellValue(client); for (int j = 0; j < 9; j++) { row1.getCell(j).setCellStyle(cellStyle); sheet0.setColumnWidth(j, 252 * 15 + 900); }}Copy the code

This is a poI table with a basic style. Although it is very simple, it is better to make one from scratch, after all, you can think about a lot of things, such as the above code can be optimized in many places.