修复java apache poi中zip格式邮件中.xlsx文件中弹出的错误
我使用ApachePOI3.11生成.xlsx文件。之后,我们被要求将其压缩并通过电子邮件发送 但是我们试图在收到邮件后打开文件 “我们发现'FileName.xlsx'中的某些内容有问题。是否要删除?” 让我们尽可能地恢复?如果你相信 要创建此工作簿,请单击“是” 弹出窗口。并恢复我们的数据。但我不明白为什么会发生这种错误 我们甚至尝试使用fileOutputStream“Temp.xlsx”生成此代码,这次没有错误弹出窗口。但是当我们将工作簿写入字节输出流时。它给了我们这个错误。我们甚至将mimeType设置为“application/vnd.openxmlformats officedocument.spreadsheetml.sheet” 我们已经附加了代码片段和弹出图像 请进一步指导我们 以下代码仅供参考:修复java apache poi中zip格式邮件中.xlsx文件中弹出的错误,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我使用ApachePOI3.11生成.xlsx文件。之后,我们被要求将其压缩并通过电子邮件发送 但是我们试图在收到邮件后打开文件 “我们发现'FileName.xlsx'中的某些内容有问题。是否要删除?” 让我们尽可能地恢复?如果你相信 要创建此工作簿,请单击“是” 弹出窗口。并恢复我们的数据。但我不明白为什么会发生这种错误 我们甚至尝试使用fileOutputStream“Temp.xlsx”生成此代码,这次没有错误弹出窗口。但是当我们将工作簿写入字节输出流时。它给了我们这个错误。我们甚至将m
@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++;
}
}
@覆盖
public ByteArrayOutputStream createworkbookformailxssf(ExtractionRequestBean ExtractionRequestBean,ByteArrayOutputStream outputstream)抛出FileNotFoundException,IOException{
XSSF工作簿xwb=新XSSF工作簿();
XSSFCellStyle headerStyle=电子表格生成器。设置headerStyle(xwb);
电子表格生成器。设置编号样式(xwb);
电子表格生成器。设置编号CIMALSTYLE(xwb);
电子表格生成器。设置日期样式(xwb);
XSSFSheet sheet=xwb.createSheet(extractionRequestBeans.getFileName());
SpreadSheetGenerator.createHeader(sheet,extractionRequestBeans.getHeaderValues(),headerStyle);
createRows(extractionRequestBeans,sheet,xwb);
SpreadSheetGenerator.settingAutosizeColumns(sheet,extractionRequestBeans.getHeaderValues());
写入(输出流);
write(xwb.toString().getBytes());
outputstream.close();
返回输出流;
}
}
公共静态XSSFCellStyle设置头样式(XSSF工作簿xwb){
XSSFCellStyle=xwb.createCellStyle();
xssfont font=xwb.createFont();
//font.setBoldweight(font.BOLDWEIGHT\u BOLD);
style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
//setFillPattern(XSSFCellStyle.SOLID_前台);
style.setFont(字体);
回归风格;
}
公共静态XSSFCellStyle设置编号样式(XSSF工作簿xwb){
XSSFCellStyle=xwb.createCellStyle();
DataFormat format=xwb.createDataFormat();
style.setDataFormat(format.getFormat(“0”));
回归风格;
}
公共静态XSSFCellStyle设置编号CimalStyle(XSSF工作簿xwb){
XSSFCellStyle=xwb.createCellStyle();
DataFormat format=xwb.createDataFormat();
style.setDataFormat(format.getFormat(“0.00”);
回归风格;
}
公共静态XSSFCellStyle设置日期样式(XSSF工作簿xwb){
XSSFCellStyle=xwb.createCellStyle();
DataFormat format=xwb.createDataFormat();
style.setDataFormat(format.getFormat(“m/d/yy h:mm”);
回归风格;
}
公共静态void createHeader(XSSFSheet工作表,列表标题值,XSSFCellStyle样式){
addHeaderRow(表,0,HeaderValue,样式);
}
私有静态void addHeaderRow(XSSFSheet sheet,int i,List HeaderValue,XSSFCellStyle样式){
info(“开始:[电子表格生成器-createHeaderRow-开始生成标题]”;
XSSFRow rowhead=sheet.createRow((int)0);
int cellIndex=0;
for(字符串标题标签:标题值){
addHeaderCell(rowhead、cellIndex、headerLabel、style);
cellIndex++;
}
info(“结束:[电子表格生成器-createHeaderRow-开始生成标题]”;
}
私有静态void addHeaderCell(XSSFRow rowhead、int cellIndex、字符串headerLabel、XSSFCellStyle样式){
if(headerLabel!=null&!headerLabel.equalsIgnoreCase(“”){
rowhead.createCell(cellIndex).setCellValue(headerLabel.toString());
}否则{
rowhead.createCell(cellIndex).setCellValue(“”);
}
rowhead.getCell(cellIndex.setCellStyle(style);
}
公共静态无效设置AutoSizeColumns(XSSFSheet表单,列表标题值){
对于(int autoSizeIndex=0;autoSizeIndex