gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:17 25 4
gpt4 key购买 nike

我有一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com