The project needs a function that provides Excel data display and download, which is not used for list data display and cannot be implemented by for. Each cell can only be implemented by hardcoding. There are four styles in the Excel data display, so four styles are created.
The final effect is as follows:
//Query payment, data source. For binding data to Excel
RfPayment rfPayment=rfFinanceManager.findRfPaymentById(shopCode, paymentId);
XSSFWorkbook workbook = null;
byte[] bytes=null;
try{
workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheet name ");
//set font
XSSFFont font = workbook.createFont();
//font size
font.setFontHeightInPoints((short) 14);
//font
font.setFontName("宋体");
//bold
font.setBold( true);
//color
font.setColor(IndexedColors.BLACK.index);
/** title style*/
XSSFCellStyle styleTitle = workbook.createCellStyle();
styleTitle.setFont(font);
// center text
styleTitle.setAlignment(HorizontalAlignment.CENTER);
/** general style*/
XSSFCellStyle styleAuto = workbook.createCellStyle();
styleAuto.setFont(font );
//Text left-align
styleAuto.setAlignment(HorizontalAlignment.LEFT);
/** General style underlined*/
XSSFCellStyle styleAutoMedium = workbook.createCellStyle();
styleAutoMedium.setFont(font);
//Text left-aligned
styleAutoMedium.setAlignment( HorizontalAlignment.LEFT);
//Thick underline
styleAutoMedium.setBorderBottom(BorderStyle.MEDIUM);
/** General style border black*/
XSSFCellStyle styleAutoThin = workbook.createCellStyle();
styleAutoThin.setFont(font);
//Text left alignment
styleAutoThin.setAlignment(HorizontalAlignment.LEFT);
// Thick underline
styleAutoThin.setBorderTop(BorderStyle.THIN);
styleAutoThin.setBorderBottom(BorderStyle.THIN);
styleAutoThin.setBorderLeft(BorderStyle.THIN);
styleAutoThin.setBorderRight(BorderStyle.THIN);
/** shop name style*/
XSSFCellStyle styleShop = workbook .createCellStyle();
styleShop.setFont(font);
//Text left alignment
styleShop.setAlignment(HorizontalAlignment.LEFT);
//Background yellow
styleShop.setFillForegroundColor(IndexedColors.YELLOW.index);//Foreground color
styleShop.setFillPattern(FillPatternType.SOLID_FOREGROUND);//Filling method, front color filling
//The first row and the first column are the title
//The first row and the fourth column are merged
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Online Banking Refund Application");
cell.setCellStyle(styleTitle);
row = sheet.createRow(1);
cell = row.createCell(0);
cell. setCellValue("Department:");
cell.setCellStyle (styleAuto);
cell = row.createCell (1);
// 名称 SalesEntity
salesEntityParam = new SalesEntity ();
salesEntityParam.setCode (rfPayment.getShopCode ());
List <SalesEntity> salesEntities = salesEntityManager.findListByParam (salesEntityParam);
cell.setCellValue (! CollectionUtils.isEmpty (salesEntities)? salesEntities.iterator (). next (). getName (): rfPayment.getShopCode ());
cell.setCellStyle (styleShop);
cell = row.createCell (2);
cell.setCellValue (":");
cell.setCellStyle (styleAuto);
cell = row.createCell (3);
cell.setCellValue ("");
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellValue("退款单创建日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getRefundCreateTimeString().substring(0, 10));
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(3);
cell = row.createCell(0);
cell.setCellValue("导出日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellValue(DateFormatHelper.dateToStr(new Date(), "yyyy-MM-dd"));
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(4);
cell = row.createCell(0);
cell.setCellValue("姓名:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellValue(userName);//
cell.setCellStyle(styleAutoMedium);
cell = row.createCell(2);
cell.setCellValue("员工代码:");
cell.setCellStyle(styleAuto);
cell = row.createCell(3);
cell.setCellValue(jobNumber);//
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(6);
cell = row.createCell(0);
cell.setCellValue("付款对象:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getPayee());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(7);
cell = row.createCell(0);
cell.setCellValue("退款申请编码:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getRfDocNo());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellValue("付款原由:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellValue(sysConfigInit.getSysConfigValue(SysConfigConstants.RF_TYPE, rfPayment.getRefundCategory()));
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(8);
cell = row.createCell(0);
cell.setCellValue("OMS订单号:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getOrderDocNo());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellValue("相关退货单号:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellValue(rfPayment.getRefundCategory().equals(Constants.STRING_NUMBER_3)?"":rfPayment.getRoDocNo());
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(9);
cell = row.createCell(0);
cell.setCellValue("平台订单编号:");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getPfDocNo());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(10);
cell = row.createCell(1);
cell.setCellValue("支付金额(元)");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellValue("备注");
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(11);
cell = row.createCell(0);
cell.setCellValue("退款单总金额");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(MoneryTransition.number2CNMontrayUnit(rfPayment.getPayTotal()));
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellValue("¥"+rfPayment.getPayTotal().setScale(2, BigDecimal.ROUND_FLOOR).toString());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellValue("");
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(12);
cell = row.createCell(0);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(13);
cell = row.createCell(0);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(14);
cell = row.createCell(0);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(15);
cell = row.createCell(0);
cell.setCellValue("银行转帐(户名)");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getPayee());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(16);
cell = row.createCell(0);
cell.setCellValue("(银行帐号)");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getAccount());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(17);
cell = row.createCell(0);
cell.setCellValue("(银行名称)");
cell.setCellStyle(styleAutoThin);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getBank());
cell.setCellStyle(styleAutoThin);
cell = row.createCell(2);
cell.setCellStyle(styleAutoThin);
cell = row.createCell(3);
cell.setCellStyle(styleAutoThin);
row = sheet.createRow(19);
cell = row.createCell(0);
cell.setCellValue("申请人:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellValue(rfPayment.getRefundCreateUser().equals(Long.valueOf("-666"))?"系统创建":omsUserManager.findUserById(Long.valueOf(rfPayment.getRefundCreateUser())).getUserName());
cell.setCellStyle(styleAutoMedium);
cell = row.createCell(2);
cell.setCellValue("日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(3);
cell.setCellValue(rfPayment.getRefundCreateTimeString().substring(0, 10));
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(20);
cell = row.createCell(0);
cell.setCellValue("部门主管:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellStyle(styleAutoMedium);
cell = row.createCell(2);
cell.setCellValue("日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(3);
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(21);
cell = row.createCell(0);
cell.setCellValue("财务总监:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellStyle(styleAutoMedium);
cell = row.createCell(2);
cell.setCellValue("日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(3);
cell.setCellStyle(styleAutoMedium);
row = sheet.createRow(22);
cell = row.createCell(0);
cell.setCellValue("总经理:");
cell.setCellStyle(styleAuto);
cell = row.createCell(1);
cell.setCellStyle(styleAutoMedium);
cell = row.createCell(2);
cell.setCellValue("日期:");
cell.setCellStyle(styleAuto);
cell = row.createCell(3);
cell.setCellStyle(styleAutoMedium);
for (int i = 0; i < 4; i++){
//宽度自适应
sheet.autoSizeColumn(i);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
bytes = os.toByteArray();
//Save the file to the server, because it does not need to be saved locally, so comment this section
// File file = new File( "D://" + System.currentTimeMillis() + ".xlsx");
// FileOutputStream out = new FileOutputStream(file);
// workbook.write(out);
// out.close();
}catch ( EncryptedDocumentException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
String uuid = UUID.randomUUID().toString();
logger.error(String.format("Export refund application", uuid), e.getMessage() );
throw new RuntimeException(String.format("Refund Expense Details Edit", uuid), e);
}finally {
try{
workbook.close();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Record the number of
exports rfPaymentExportSchemesManager.insertExportNum(rfPayment);
//Download file
String fileNmae = System.currentTimeMillis() + ".xlsx";
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", new String(fileNmae.getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
return entity;
The jar package of poi is used. Version 3.12