APACHE POI:从linux导出问题并从Windows读取 [英] APACHE POI : Export problem from linux and reading from windows

查看:114
本文介绍了APACHE POI:从linux导出问题并从Windows读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法将表数据值导出到XLSX文件中.

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;

}

实际上,excel文件已导出,如果我们留在Linux中,它将正常工作. 但是,如果我们通过窗口打开文件,则会遇到问题,错误消息,并且会发现空白或某些符号(如无穷大)无法管理.

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.

有人有主意吗?

推荐答案

遇到相同的问题,可以通过在服务器上写入文件然后发送来解决问题.

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;
        }
    }

这篇关于APACHE POI:从linux导出问题并从Windows读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆