我从一个方法得到一个excel文件作为返回值。我想将excel文件作为附件发送到邮件中,但我没有在系统中保存该文件的选项。所以实际上,我没有文件名。如何使用javamail将该文件作为附件发送?
digitalDeliveryReportSheet = digitalWorkbook.createSheet("Local Media");
headerStyle = digitalWorkbook.createCellStyle();
headerFont = digitalWorkbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
cell = null;
XSSFRow rowLm;
rowNum = 1;
colNum = 0;
mergeRowFrom = 0; // --> added
mergeRowTo = 0; // --> added
mergeColFrom = 1; // --> added
mergeColTo = 2; // --> added
contentStatus = new ArrayList<String>();
contentStatus.add("Delivered");
contentStatus.add("Completed");
rowLm = digitalDeliveryReportSheet.createRow(0);
rowLm.createCell(colNum++).setCellValue("Delivery Date");
colNum = 1;
width = 3000;
try {
width = Integer.parseInt(ApplicationUtil.getProperty(ApplicationConstants.REPORTS_DEFAULT_COLUMN_WIDTH));
} catch (Exception e) {
LOG.warn("Exception while fetching REPORTS_DEFAULT_COLUMN_WIDTH", e);
}
for (String header : shelfCodeList) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
cell.setCellValue(header);
CellRangeAddress cellRangeAddress = new CellRangeAddress(mergeRowFrom, mergeRowTo, mergeColFrom, mergeColTo); // --> added
digitalDeliveryReportSheet.addMergedRegion(cellRangeAddress); // --> added
mergeColFrom+=2;
mergeColTo+=2;
colNum++;
//sheet.autoSizeColumn(colNum);
digitalDeliveryReportSheet.setColumnWidth(colNum, width);
}
cellsToCover = shelfCodeList.size()*2;
rowLm = digitalDeliveryReportSheet.createRow(1);
colNum = 1;
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
for(int i = 1; i <= cellsToCover; i++) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
if(i % 2 != 0) {
cell.setCellValue(contentStatus.get(0));
}
else {
cell.setCellValue(contentStatus.get(1));
}
}
countByShelf = new HashMap<Long, Integer>();
completedCountByShelf = new HashMap<Long, Integer>();
rowNum++;
for (String date : deliveryDateList) {
colNum = 0;
rowLm = digitalDeliveryReportSheet.createRow(rowNum++);
cell = rowLm.createCell(colNum++);
cell.setCellValue(date);
for (int i=0; i < shelfNumberList.size(); i++) {
cell = rowLm.createCell(colNum++);
long shelfNumber = shelfNumberList.get(i);
countByShelf = map.get(date);
completedCountByShelf = byShelf.get(date);
if(countByShelf != null) {
if(countByShelf.get(shelfNumber) != null) {
cell.setCellValue(countByShelf.get(shelfNumber));
}
}
if(completedCountByShelf!=null) {
if(completedCountByShelf.get(shelfNumber) != null) {
cell = rowLm.createCell(colNum);
cell.setCellValue(completedCountByShelf.get(shelfNumber));
}
}
colNum++;
}
}
mergeRowFrom = rowNum; // --> added
mergeRowTo = rowNum; // --> added
mergeColFrom = 1; // --> added
mergeColTo = 2; // --> added
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
colNum = 1;
for (String header : shelfCodeList) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
cell.setCellValue(header);
CellRangeAddress cellRangeAddress = new CellRangeAddress(mergeRowFrom, mergeRowTo, mergeColFrom, mergeColTo); // --> added
digitalDeliveryReportSheet.addMergedRegion(cellRangeAddress); // --> added
mergeColFrom+=2;
mergeColTo+=2;
colNum++;
//sheet.autoSizeColumn(colNum);
digitalDeliveryReportSheet.setColumnWidth(colNum, width);
}
rowNum++;
colNum = 1;
rowLm = digitalDeliveryReportSheet.createRow(rowNum);
for(int i = 1; i <= cellsToCover; i++) {
cell = rowLm.createCell(colNum++);
cell.setCellStyle(headerStyle);
if(i % 2 != 0) {
cell.setCellValue(contentStatus.get(0));
}
else {
cell.setCellValue(contentStatus.get(1));
}
}
rowNum++;
colNum = 0;
rowLm = digitalDeliveryReportSheet.createRow(rowNum++);
cell = rowLm.createCell(colNum++);
cell.setCellValue("Total By Column");
for (long shelfNumber : shelfNumberList) {
cell = rowLm.createCell(colNum++);
if (totalByColumn.get(shelfNumber) != null && totalByColumn.get(shelfNumber) > 0) {
cell.setCellValue(totalByColumn.get(shelfNumber));
}
if(totalCompletedByColumn.get(shelfNumber) != null && totalCompletedByColumn.get(shelfNumber) > 0) {
cell = rowLm.createCell(colNum);
cell.setCellValue(totalCompletedByColumn.get(shelfNumber));
}
colNum++;
}
} else {
throw new AssetLibraryValidationException(errorMessages);
}
return digitalWorkbook;
这是返回工作簿的代码,如何发送返回的工作簿而不将其保存在系统中。
1条答案
按热度按时间t3psigkw1#
您需要将数据转换为某种可导出格式,例如excel文件格式、csv格式等,并且需要将数据转换为内存中的字节数组。一旦在字节数组中有了正确格式的数据,就可以使用bytearraydatasource将其附加到javamail消息。稍加搜索就会发现一些例子,但关键是: