问题
I have the following method which exports table data values in a XLSX file.
public static void export(String filePath, Collection<String> variableNamesList, List<Map<GroupByStruct, List<List<String>>>> tablesToExportList) {
BufferedOutputStream fileOutputStream = null;
if (filePath.endsWith(XLS_EXTENSION)) {
filePath = filePath.replace(XLS_EXTENSION, XLSX_EXTENSION);
}
if (!filePath.endsWith(XLS_EXTENSION) && !filePath.endsWith(XLSX_EXTENSION)) {
filePath = filePath + XLSX_EXTENSION;
}
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(SHEET_NAME);
int beginRow = 1;
int beginColumn = 1;
int nbColumns = 0;
// Writes variables names into sheet title
beginRow = exportVariablesTitle(sheet, variableNamesList, beginRow, beginColumn);
List<TableObject> tableObjectList = convertToTableObjectList(tablesToExportList);
// Export data into sheet
for (TableObject tableObject : tableObjectList) {
tableObject.exportTableObject(sheet, beginRow, beginColumn);
beginRow += tableObject.getNbRows() + 2;
nbColumns = Math.max(nbColumns, tableObject.getnbColumns());
}
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
else {
file.delete();
file.createNewFile();
}
fileOutputStream = new BufferedOutputStream(new FileOutputStream(file));
workbook.write(fileOutputStream);
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
catch (IOException ioe) {
DebugUtil.warning(ioe);
}
}
private static int exportVariablesTitle(XSSFSheet sheet, Collection<String> variableNamesList, int beginRow, int beginColumn) {
XSSFRow row = sheet.getRow(beginRow);
if (row == null) row = sheet.createRow(beginRow);
int curCol = beginColumn;
XSSFCell cell = row.getCell(curCol);
if (cell == null) cell = row.createCell(curCol);
// Adds variables row label
cell.setCellStyle(getTitleBorderStyle(sheet.getWorkbook()));
cell.setCellValue(VARIABLES_ROW_LABEL);
cell.setCellType(Cell.CELL_TYPE_STRING);
curCol++;
// Adds a variable name per column into the same row
for (String varName : variableNamesList) {
cell = row.getCell(curCol);
if (cell == null) cell = row.createCell(curCol);
cell.setCellStyle(getTitleBorderStyle(sheet.getWorkbook()));
cell.setCellValue(varName);
cell.setCellType(Cell.CELL_TYPE_STRING);
curCol++;
}
return beginRow + 2;
}
In fact the excel file is exported and it works correctly if we stay in linux. But if we go through the windows to open the file we have problems, error messages and we see that it is the voids or certain symbols like infinity which are not managed.
Anyone have an idea?
回答1:
Faced the same issue, got it right by writing the file on the server and then sending it.
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet();
// makes the border bold
XSSFFont headerFont = workbook.createFont();
headerFont.setBold(true);
XSSFCellStyle headerCellStyle = (XSSFCellStyle) workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
XSSFRow headerRow = sheet.createRow(0);
for (int index = 0; index < data.size(); index++) {
XSSFCell cell = headerRow.createCell(index);
cell.setCellValue("somevalue");
cell.setCellStyle(headerCellStyle);
}
int rowIdx = 1;
for (int celIdx = 0; celIdx < data.size(); celIdx++) {
XSSFRow row = sheet.createRow(rowIdx++);
row.createCell(celIdx).setCellValue("somevalue");
// Resize all columns to fit the content size
sheet.autoSizeColumn(celIdx);
}
String reportName = "*.xlsx";
try (FileOutputStream outputStream = new FileOutputStream(reportName)) {
workbook.write(outputStream);
outputStream.close();
return reportName;
}
}
来源:https://stackoverflow.com/questions/60831645/apache-poi-export-problem-from-linux-and-reading-from-windows