java - Apache POI无法在Linux中生成Excel

标签 java linux excel tomcat apache-poi

我有一个servlet作为excel下载用户列表,如下所示

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=filename.xls");
    File excelFile = new File(filename);
    HSSFWorkbook workbook = new HSSFWorkbook(excelFile);
    // ...
    // populate workbook with user list from Database here
    // ...
    workbook.write(response.getOutputStream()); // Write workbook to response.
    workbook.close();

这段代码在windows下运行没有任何问题。但是当我试图在ubuntu服务器的tomcat 7上部署时,当我执行(下载excel)时,它抛出filenotfoundexception。excel文件不是在webapps/(我的应用程序文件夹)中创建的,而是在windows中创建excel文件。
我已经将tomcat的webapps及其子目录设置为777,并将所有者更改为tomcat7用户。
ApachePOI版本是3.10-final

最佳答案

我在Linux上使用相同版本的桌面应用程序,它在我的情况下运行良好。
这是我的代码片段。用于将数据导出到excell。

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Export translated data as XLS");
        fileChooser.setTitle("Export XLS File of " + toLang);
        fileChooser.getExtensionFilters().addAll(
                new ExtensionFilter("XLS Files", "*.xls"));
        fileChooser.setInitialFileName(Common
                .replaceWhiteSpaceAndSlash(fromlang)
                + "_"
                + Common.replaceWhiteSpaceAndSlash(toLang) + ".xls");
        fileChooser.setInitialDirectory(new File(System
                .getProperty("user.home") + "/Desktop"));

        File selectedFile = fileChooser.showSaveDialog(null);

        File file = new File(filename);
        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook = Workbook.createWorkbook(file,
                wbSettings);

        workbook.createSheet("myExcellData", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
        WritableFont groupFont = new WritableFont(
                WritableFont.createFont("Arial"),
                WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.GREEN);

        WritableFont ledgerFont = new WritableFont(
                WritableFont.createFont("Arial"),
                WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.GRAY_80);
        this.headFormat = new WritableCellFormat(ledgerFont);
        this.headFormat.setWrap(true);
        this.langFormat = new WritableCellFormat(groupFont);
        this.langFormat.setWrap(true);
        Label label;
        Label labelhead;
        Label labelhead1;
        Label labelhead2;
        Label labelhead3;
        Label label1;
        Label label2, label3;
        int i = 1;

        this.currentRow = 1;
        for (KalculateParam obj : data) {
            labelhead = new Label(0, 0, obj.getLang(), this.headFormat);
            labelhead1 = new Label(0, 1, "KEY", this.headFormat);
            labelhead2 = new Label(1, 1, "VALUE", this.headFormat);
            labelhead3 = new Label(2, 1, "PROPERTY", this.headFormat);
            label = new Label(0, i, obj.getKey(), this.langFormat);
            label1 = new Label(1, i, obj.getValue(), this.langFormat);
            label2 = new Label(2, i, obj.getPropertyName(), this.langFormat);
            // label3 = new Label( 3, i, obj.getLang(),this.langFormat);
            Cell cell=new Cell();

            excelSheet.addCell(labelhead);
            excelSheet.addCell(labelhead1);
            excelSheet.addCell(labelhead2);
            excelSheet.addCell(labelhead3);
            excelSheet.addCell(label);
            excelSheet.addCell(label1);
            excelSheet.addCell(label2);
            // excelSheet.addCell(label3);
            i++;
        }

        workbook.write();
        workbook.close();

    } catch (Exception e) {
        Common.showError(e);
        e.printStackTrace();
    }
    return "fine";
}

private void addData(WritableSheet sheet, int row, String s)
        throws WriteException, RowsExceededException {
    Label label;
    label = new Label(row, row, s, this.headFormat);
    sheet.addCell(label);
}

现在导入excell数据
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Import Translated data to XLS");
            fileChooser.getExtensionFilters().addAll(
                    new ExtensionFilter("XLS Files", "*.xls"));
            fileChooser.setInitialDirectory(new File(System
                    .getProperty("user.home") + "/Desktop"));
            File selectedFile = fileChooser.showOpenDialog(null);


           List<String> dataList = new ArrayList<String>();

    try {

        FileInputStream fis = new FileInputStream(file);
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                cell = cellIterator.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING: {
                    dataList.add(cell.getStringCellValue());
                }
                    break;
                }
            }
        }
        return dataList;
    } catch (FileNotFoundException ee) {
        ee.printStackTrace();
    } catch (IOException ee) {
        ee.printStackTrace();
    }

我们需要你的代码,这样我就可以让你知道你是否失踪了。我想在环境的基础上不会有任何例外。

关于java - Apache POI无法在Linux中生成Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524022/

相关文章:

java - Hibernate 搜索与循环关系

java - 如何解决这个 "java.lang.IndexOutOfBoundsException"错误?

java - 为什么我的 Java 首选项代码不能在所有计算机上运行?

C - Linux - 自定义内核模块来迭代进程的子进程会破坏内核日志和计算机

vba - 选择 A 列中最后使用的单元格,然后将其扩展到 H 列

vba - 使用 Excel VBA 循环浏览文件夹中的 .csv 文件并将文件名复制到最后一列的单元格中

java - Spring MVC 与 Spring Data JPA

linux - 如何从Linux内核空间获取用户名

java - 如何从JAVA运行linux命令?

无法在具有相同功能的 c 和 excel 中得到相同的答案