返回信息流public void run() {
FileDialog fileDialog = new FileDialog(workbenchWindow.getShell(),
SWT.SAVE);
fileDialog.setText("导出报表");
fileDialog.setFilterNames(new String[] { "Excel报表" });
fileDialog.setFilterExtensions(new String[] { "*.xls" });
if (null != fileDialog.open()) {
String fileName = fileDialog.getFilterPath() + File.separator
+ fileDialog.getFileName();
System.out.println(fileName);
saveFile(fileName);
}
}
@SuppressWarnings("deprecation")
private void saveFile(String filePath) {
HSSFWorkbook workbook;
try {
// 创建Excel工作薄
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "统计表");
// 设置单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 第一行,标题
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellValue("学号");
cell = row.createCell((short) 1);
cell.setCellStyle(cellStyle);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellStyle(cellStyle);
cell.setCellValue("总分");
// 2
row = sheet.createRow(1);
cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellValue("学号");
cell = row.createCell((short) 1);
cell.setCellStyle(cellStyle);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellStyle(cellStyle);
cell.setCellValue("总分");
//写入文件
FileOutputStream fileOutput = new FileOutputStream(filePath);
workbook.write(fileOutput);
fileOutput.flush();
fileOutput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
以上是一个继承自 org.eclipse.jface.action.Action的action的run方法及调用的saveFile代码,实现的功能是:点击菜单中的"导出报表"按钮之后弹出一个对话框,在对话框中选择保存路径和保存文件名之后,将数据保存到该文件中.
程序进入saveFile方法以后,在红色那行跳转到了一个很莫名奇妙的地方,下面的语句就不再执行了..
但是我测试过这段代码放在独立的java application中是可以正确生成excel的,不知道是哪里出了问题?
这是一条镜像帖。来源:北邮人论坛 / java / #19759同步于 2011/8/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
eclipse rcp开发中使用poi生成excel的问题
cb
2011/8/22镜像同步2 回复
订阅后,新回复会通过你的通知中心匿名送达。