我在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();
}
我們需要你的代碼,這樣我可以讓你知道你是否missing.I認爲不會有環境的基礎上的任何異常。
上面的代碼流響應,沒有文件(在您的發佈代碼中)。 –
*「它在Windows中創建excel文件」* - 你是什麼意思? – Andremoniy
workbook.write(response.getOutputStream());將excel作爲tomcat的webapps /(project)目錄下的文件寫入,並將其傳遞給響應。 –