我使用apache poi 3.11生成.xlsx文件。之后我们被要求拉链并通过电子邮件发送。
但是我们尝试在电子邮件中打开文件,我们得到“我们在'FileName.xlsx'中发现了一些内容的问题。你想让我们尽可能多地恢复吗?如果你相信的话此工作簿的来源,单击是“弹出窗口。并恢复我们的数据。但我不明白为什么会出现这种错误。
我们甚至尝试使用fileOutputStream“Temp.xlsx”生成此代码,这次没有错误弹出窗口。但是当我们将工作簿写入byteoutputstream时。它给了我们这个错误。我们甚至将mimeType设置为“application / vnd.openxmlformats-officedocument.spreadsheetml.sheet” 我们附加了代码段和弹出窗口图像。
请进一步指导我们!
以下是代码:
@Override
public ByteArrayOutputStream createWorkBookForEmailXSSF(ExtractionRequestBean extractionRequestBeans, ByteArrayOutputStream outputstream) throws FileNotFoundException, IOException {
XSSFWorkbook xwb = new XSSFWorkbook();
XSSFCellStyle headerStyle= SpreadSheetGenerator.settingHeaderStyle(xwb);
SpreadSheetGenerator.setttingNumberStyle(xwb);
SpreadSheetGenerator.setttingNumberDecimalStyle(xwb);
SpreadSheetGenerator.setttingDateStyle(xwb);
XSSFSheet sheet = xwb.createSheet(extractionRequestBeans.getFileName());
SpreadSheetGenerator.createHeader(sheet, extractionRequestBeans.getHeaderValues(), headerStyle);
SpreadSheetGenerator.createRows(extractionRequestBeans, sheet, xwb);
SpreadSheetGenerator.settingAutosizeColumns(sheet, extractionRequestBeans.getHeaderValues());
xwb.write(outputstream);
outputstream.write(xwb.toString().getBytes());
outputstream.close();
return outputstream;
}
}
public static XSSFCellStyle settingHeaderStyle(XSSFWorkbook xwb) {
XSSFCellStyle style = xwb.createCellStyle();
XSSFFont font = xwb.createFont();
//font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
//style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style.setFont(font);
return style;
}
public static XSSFCellStyle setttingNumberStyle(XSSFWorkbook xwb) {
XSSFCellStyle style = xwb.createCellStyle();
DataFormat format = xwb.createDataFormat();
style.setDataFormat(format.getFormat("0"));
return style;
}
public static XSSFCellStyle setttingNumberDecimalStyle(XSSFWorkbook xwb) {
XSSFCellStyle style = xwb.createCellStyle();
DataFormat format = xwb.createDataFormat();
style.setDataFormat(format.getFormat("0.00"));
return style;
}
public static XSSFCellStyle setttingDateStyle(XSSFWorkbook xwb) {
XSSFCellStyle style = xwb.createCellStyle();
DataFormat format = xwb.createDataFormat();
style.setDataFormat(format.getFormat("m/d/yy h:mm"));
return style;
}
public static void createHeader(XSSFSheet sheet, List<String> headerValues, XSSFCellStyle style) {
addHeaderRow(sheet, 0, headerValues, style);
}
private static void addHeaderRow(XSSFSheet sheet, int i, List<String> headerValues, XSSFCellStyle style) {
logger.info("Start : [SpreadSheetGenerator - createHeaderRow - Start Generating Header]");
XSSFRow rowhead = sheet.createRow((int) 0);
int cellIndex = 0;
for (String headerLabel : headerValues) {
addHeaderCell(rowhead, cellIndex, headerLabel, style);
cellIndex++;
}
logger.info("End : [SpreadSheetGenerator - createHeaderRow - Start Generating Header]");
}
private static void addHeaderCell(XSSFRow rowhead, int cellIndex, String headerLabel, XSSFCellStyle style) {
if (headerLabel != null && !headerLabel.equalsIgnoreCase("")) {
rowhead.createCell(cellIndex).setCellValue(headerLabel.toString());
} else {
rowhead.createCell(cellIndex).setCellValue("");
}
rowhead.getCell(cellIndex).setCellStyle(style);
}
public static void settingAutosizeColumns(XSSFSheet sheet, List<String> headerValues) {
for (int autoSizeIndex = 0; autoSizeIndex < headerValues.size(); autoSizeIndex++) {
sheet.autoSizeColumn(autoSizeIndex);
}
}
public static void createRows(ExtractionRequestBean extractionRequestBean, XSSFSheet sheet, XSSFWorkbook hwb) {
logger.info("Start : [SpreadSheetGenerator - createRows - Start Generating Excel Rows ]");
int rowIndex = 1;
for (Object[] obj : extractionRequestBean.getDatas()) {
addRow(sheet, rowIndex, obj, extractionRequestBean.getCellStyleMap(), hwb);
rowIndex++;
}
logger.info("End : [SpreadSheetGenerator - createRows - End Generating Excel Rows]");
}
private static void addRow(XSSFSheet sheet, int rowIndex, Object[] obj, Map<Integer, Integer> cellStyleMap, XSSFWorkbook hwb) {
XSSFRow row = sheet.createRow((int) rowIndex);
int cellIndex = 0;
addCell(obj, row, cellIndex, cellStyleMap, hwb);
}
@SuppressWarnings("deprecation")
private static void addCell(Object[] obj, XSSFRow row, int cellIndex, Map<Integer, Integer> cellStyleMap, XSSFWorkbook hwb) {
for (Object object : obj) {
if (cellStyleMap.get(cellIndex) == HSSFCellStyleEnum.ONLYSTRING.getStyleCode()) {
row.createCell(cellIndex).setCellValue(object != null ? object.toString() : "");
} else if (cellStyleMap.get(cellIndex) == HSSFCellStyleEnum.NUMERICWITHDECIMAL.getStyleCode()) {
row.createCell(cellIndex).setCellValue(object != null ? Double.valueOf(object.toString()) : 0d);
} else if (cellStyleMap.get(cellIndex) == HSSFCellStyleEnum.NUMERICWITHOUTDECIMAL.getStyleCode()) {
row.createCell(cellIndex).setCellValue(object != null ? Double.valueOf(object.toString()) : 0d);
} else if (cellStyleMap.get(cellIndex) == HSSFCellStyleEnum.ONLYDATE.getStyleCode()) {
row.createCell(cellIndex).setCellValue(object != null ? DateUtil.formatDateOnly(new Date(object.toString())) : "");
}
cellIndex++;
}
}