public void createDailyReport(ShipmentAgentEnum shipmentAgent) {
SXSSFWorkbook wb = new SXSSFWorkbook(-1);
SXSSFSheet sh = wb.createSheet();
int rownum = 0;
Font font = wb.createFont();
wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("������");
CellStyle labelCellStyle = wb.createCellStyle();
labelCellStyle.setFont(font);
//������
SXSSFRow row = sh.createRow(rownum++);
row.setHeightInPoints(40);
SXSSFCell labelCell = row.createCell(0);
labelCell.setCellType(Cell.CELL_TYPE_STRING);
labelCell.setCellValue("\"������������\"������������������������");
labelCell.setCellStyle(labelCellStyle);
sh.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
wb.createFont();
font.setFontHeightInPoints((short) 16);
font.setFontName("������");
CellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setFont(font);
wb.createFont();
font.setFontHeightInPoints((short) 16);
font.setFontName("������");
CellStyle dataCellStyle = wb.createCellStyle();
dataCellStyle.setFont(font);
dataCellStyle.setWrapText(true);
//������������
row = sh.createRow(rownum++);
row.setHeightInPoints(30);
SXSSFCell titleCell = row.createCell(0);
titleCell.setCellValue("To " + shipmentAgent.getCompanyName());
font = wb.createFont();
font.setFontHeightInPoints((short) 18);
font.setFontName("������");
labelCellStyle.setFont(font);
titleCell.setCellStyle(labelCellStyle);
sh.addMergedRegion(CellRangeAddress.valueOf("$A$2:$F$2"));
row = sh.createRow(rownum++);
SXSSFCell cell = row.createCell(0);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("������");
cell = row.createCell(1);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("���������������������");
cell = row.createCell(2);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("���������");
cell = row.createCell(3);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("���������������(������)");
cell = row.createCell(4);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("������������������");
cell = row.createCell(5);
cell.setCellStyle(headerCellStyle);
cell.setCellValue("������������");
LocalDate now = LocalDate.now();
LocalDateTime today = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0);
List<BookingDiscountApplyRecord> bookingDiscountApplyRecords = bookingDiscountApplyRecordRepository.findByShipmentAgentAndCreatedTimeBetween(shipmentAgent, today, today.plusDays(1));
int[] widthArray = new int[6];
widthArray[0] = 5;
widthArray[3] = 16;
for (int i = 0; i < bookingDiscountApplyRecords.size(); i++) {
BookingDiscountApplyRecord bookingDiscountApplyRecord = bookingDiscountApplyRecords.get(i);
row = sh.createRow(rownum++);
cell = row.createCell(0);
cell.setCellStyle(dataCellStyle);
cell.setCellValue(i + 1);
cell = row.createCell(1);
cell.setCellStyle(dataCellStyle);
String ladingBillNo = bookingDiscountApplyRecord.getLadingBillNo();
cell.setCellValue(ladingBillNo);
if (ladingBillNo.length() > widthArray[1]) {
widthArray[1] = ladingBillNo.length();
}
cell = row.createCell(2);
cell.setCellStyle(dataCellStyle);
cell.setCellValue("���������");
widthArray[2] = 6;
cell = row.createCell(3);
cell.setCellStyle(dataCellStyle);
double totalDiscount = Math.round(bookingDiscountApplyRecord.totalDiscount());
cell.setCellValue(totalDiscount);
/*int w = String.valueOf(totalDiscount).length();
if (w>widthArray[3]){
widthArray[3]=w;
}*/
cell = row.createCell(4);
StringBuilder sb = new StringBuilder();
List<Coupon> coupons = bookingDiscountApplyRecord.getCoupons();
coupons.forEach(coupon -> {
sb.append(coupon.genCouponNumber() + "\n");
});
cell.setCellValue(new XSSFRichTextString(sb.toString()));
if (20 > widthArray[4]) {
widthArray[4] = 20;
}
cell.setCellStyle(dataCellStyle);
cell = row.createCell(5);
cell.setCellStyle(dataCellStyle);
String dateStr = bookingDiscountApplyRecord.getCreatedTime().format(DateTimeFormatter.ISO_DATE_TIME);
cell.setCellValue(dateStr);
if (22 > widthArray[5]) {
widthArray[5] = 22;
}
}
for (int i = 0; i < widthArray.length; i++) {
sh.setColumnWidth(i, widthArray[i] * 256 * 2);
}
FileOutputStream out = null;
String newFileName = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + "." + "xlsx";
try {
out = new FileOutputStream("daily_statements/" + newFileName);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// dispose of temporary files backing this workbook on disk
wb.dispose();
String receiptName = shipmentAgent.getCompanyName();
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
message.setFrom(new InternetAddress("
[email protected]"));
} catch (MessagingException e) {
e.printStackTrace();
}
try {
helper.setTo(shipmentAgent.getCompanyMail());
// helper.setTo("
[email protected]");
StringBuilder mailBody = new StringBuilder();
mailBody.append(receiptName)
.append("���\n")
.append("\t������������������������������������������������ ������������")
.append("\n")
.append("\t������������������")
.append("\n\n")
.append("Sent From:")
.append("���������������������������������������������");
helper.setSubject("������������������-" + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE));
helper.setText(mailBody.toString());
FileSystemResource attachmentFile = new FileSystemResource(new File("daily_statements/" + newFileName));
helper.addAttachment(newFileName, attachmentFile);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
mailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}