Я использую Spring Boot для экспорта файла в Excel с помощью Apache Poi, но я устанавливаю заголовки (имена столбцов из базы данных) как статические. Мне нужно установить это значение как исходящее из базы данных. Я использую JPQL Query для подключения списка результатов
Сервисный код
@Override
public File exportPOFile(POFilterVO poFilterVO) throws NoDatabaseConnectedException, IOException, ParseException {
LOGGER.info("Method Name : exportPOFile", POServiceImpl.class);
List<Object[]> poList = this.omPurchaseOrderRepository.getPOList(poFilterVO, "");
File file = null;
if (poList != null && !poList.isEmpty()) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle rheaderStyle = workbook.createCellStyle();
HSSFCellStyle lheaderStyle = workbook.createCellStyle();
HSSFCellStyle cheaderStyle = workbook.createCellStyle();
HSSFCellStyle alignLeft = workbook.createCellStyle();
HSSFCellStyle alignRight = workbook.createCellStyle();
HSSFCellStyle alignCenter = workbook.createCellStyle();
CellStyle cellStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
short dateFormat = createHelper.createDataFormat().getFormat("dd/MM/yyyy");
cellStyle.setDataFormat(dateFormat);
HSSFSheet sheet = workbook.createSheet();
HSSFCellStyle my_style_0 = workbook.createCellStyle();
my_style_0.setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));
Calendar calendar = new GregorianCalendar();
calendar.set(1982, Calendar.NOVEMBER, 25);
sheet.createFreezePane(0, 1);
HSSFFont headerFont = workbook.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
rheaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
rheaderStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
rheaderStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
rheaderStyle.setFont(headerFont);
rheaderStyle.setAlignment(CellStyle.ALIGN_RIGHT);
lheaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
lheaderStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
lheaderStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
lheaderStyle.setFont(headerFont);
lheaderStyle.setAlignment(CellStyle.ALIGN_LEFT);
cheaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cheaderStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
cheaderStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
cheaderStyle.setFont(headerFont);
cheaderStyle.setAlignment(CellStyle.ALIGN_CENTER);
alignLeft.setAlignment(CellStyle.ALIGN_LEFT);
alignRight.setAlignment(CellStyle.ALIGN_RIGHT);
alignCenter.setAlignment(CellStyle.ALIGN_CENTER);
HSSFRow row = sheet.createRow(0);
HSSFCell cell0 = row.createCell(0);
cell0.setCellStyle(rheaderStyle);
cell0.setCellValue("PO No");
HSSFCell cell1 = row.createCell(1);
cell1.setCellStyle(rheaderStyle);
cell1.setCellValue("PO Date");
HSSFCell cell2 = row.createCell(2);
cell2.setCellStyle(rheaderStyle);
cell2.setCellValue("Contract No");
HSSFCell cell3 = row.createCell(3);
cell3.setCellStyle(rheaderStyle);
cell3.setCellValue("Customer");
HSSFCell cell4 = row.createCell(4);
cell4.setCellStyle(rheaderStyle);
cell4.setCellValue("Company");
int j = 0;
int rowNumber = 1;
for (Object[] obj : poList) {
OmPurchaseOrderVO omPurchaseOrderVO = new OmPurchaseOrderVO();
BeanUtils.copyProperties(obj[0], omPurchaseOrderVO);
MmCompanyVO mmCompanyVO = new MmCompanyVO();
BeanUtils.copyProperties(obj[1], mmCompanyVO);
omPurchaseOrderVO.setCompanyName(mmCompanyVO.getCode() + "-" + mmCompanyVO.getName());
MmCustomerVO mmCustomerVO = new MmCustomerVO();
BeanUtils.copyProperties(obj[2], mmCustomerVO);
omPurchaseOrderVO.setCustomerName(mmCustomerVO.getCustomerId() + "-" + mmCustomerVO.getName());
HSSFRow nextrow = sheet.createRow(rowNumber);
HSSFCell rcell0 = nextrow.createCell(0);
HSSFCell rcell1 = nextrow.createCell(1);
HSSFCell rcell2 = nextrow.createCell(2);
HSSFCell rcell3 = nextrow.createCell(3);
HSSFCell rcell4 = nextrow.createCell(4);
rcell0.setCellStyle(alignCenter);
if (omPurchaseOrderVO.getPurchaseOrderNumber() != null) {
rcell0.setCellValue(String.valueOf(omPurchaseOrderVO.getPurchaseOrderNumber()));
}
rcell1.setCellStyle(alignLeft);
if (omPurchaseOrderVO.getPurchaseOrderDate() != null) {
rcell1.setCellValue(new SimpleDateFormat("yyyy-MM-dd")
.parse(String.valueOf(omPurchaseOrderVO.getPurchaseOrderDate())));
rcell1.setCellStyle(cellStyle);
}
rcell2.setCellStyle(alignRight);
if (omPurchaseOrderVO.getRunningContractId() != null) {
rcell2.setCellValue(String.valueOf(omPurchaseOrderVO.getRunningContractId()));
}
rcell3.setCellStyle(alignRight);
if (omPurchaseOrderVO.getCustomerName() != null) {
rcell3.setCellValue(omPurchaseOrderVO.getCustomerName());
}
rcell4.setCellStyle(alignRight);
if (omPurchaseOrderVO.getCompanyName() != null) {
rcell4.setCellValue(omPurchaseOrderVO.getCompanyName());
}
rowNumber++;
}
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
sheet.autoSizeColumn(3);
sheet.autoSizeColumn(4);
file = new File("filePath");
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
}
return file;
}
код в хранилище
public List<Object[]> getPOList(POFilterVO poFilterVO, String action) {
String queryString = "SELECT OMP,MC,OMP.mmCustomer,RC FROM OmPurchaseOrder OMP "
+ " LEFT JOIN OMP.omRunningContract RC JOIN MmCompany MC ON MC.companyId=OMP.companyId JOIN MmCustomer MCUST "
+ " ON MCUST.customerId=OMP.mmCustomer.customerId WHERE OMP.tenantId=" + poFilterVO.getTenantId();
queryString += " order by OMP.purchaseOrderId desc";
LOGGER.info("Query for PO List Filter :",queryString);
Query query = entityManager.createQuery(queryString.toString());
if (action.equals(OrderManagementEnum.PAGINATE.getApKey())) {
int firstResult = poFilterVO.getPage() * poFilterVO.getSize();
query.setFirstResult(firstResult);
query.setMaxResults(poFilterVO.getSize());
}
return query.getResultList();
}