您的位置:首页 > 编程语言 > Java开发

POI实现将表格数据保存到excel2007

2017-06-16 15:26 302 查看
最近由于项目需要将表格数据保存由excel2003转移到excel2007中(因为excel2007可以保存百万级的数据),之前采用的是jxl来操作excel,现在需改用POI来操作excel,现完成编码及测试工作,顺便记录下,以备后忘。

jar包:



这是保存数据的接口代码:

private static int exception = 0;

public static void exportToExcel_SKT(String sheetname, KTable table) {
String fileName = DialogManager.invokeFileDlg(SWT.SAVE, null,
!Assert.isNull(sheetname) ? sheetname : "" + ".xlsx", new String[] { "*.xlsx" }); //$NON-NLS-1$ //$NON-NLS-2$
if (fileName == null || "".equals(fileName)) { //$NON-NLS-1$
return;
}
int colimns = table.getModel().getColumnCount();// 列数
int rows = table.getModel().getRowCount();// 行数
table.getModel().getFixedHeaderRowCount();
try {
MyProgressMonitorDialog progressDialog = new MyProgressMonitorDialog(Display.getCurrent().getActiveShell());
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
// TODO Auto-generated method stub
monitor.beginTask("正在导出......", IProgressMonitor.UNKNOWN);
FileOutputStream outputStream = null;
String[] str1;
exception = 0;
long min = 0,ming = 0;;
try {
Workbook wb = new SXSSFWorkbook(1000);
outputStream = new FileOutputStream(fileName);
Sheet sheet = wb.createSheet("0");
for (int i = 0; i < rows; i++) {
str1 = new String[colimns];
Row row = sheet.createRow(i);
for (int j = 0; j < colimns; j++) {
long s1 = System.currentTimeMillis();
row.createCell(j).setCellValue(table.getModel().getContentAt(j, i).toString());
long e1 = System.currentTimeMillis();
ming = ming+(e1-s1);
}
}
wb.write(outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
exception = 1;
MessageDialog.openInformation(null, "", e.getMessage()); //$NON-NLS-1$
LoggerUtil.error(ExportTable.class, e, e.getMessage());
} catch (IOException e) {
exception = 1;
MessageDialog.openInformation(null, "", e.getMessage()); //$NON-NLS-1$
LoggerUtil.error(ExportTable.class, e, e.getMessage());
}
LoggerUtil.logger(ExportTable2007_2003.class, sheetname+"min:"+min+"ming:"+ming);
}
};
progressDialog.run(true, false, runnable);
if (exception == 1) {
DialogManager.invokeMessageBox("提示框", "导出出错", SWT.ICON_INFORMATION | SWT.OK);
} else {
DialogManager.invokeMessageBox("提示框", "导出成功", SWT.ICON_INFORMATION | SWT.OK);
}

} catch (Exception e) {
MessageDialog.openInformation(null, "", e.getMessage()); //$NON-NLS-1$
LoggerUtil.error(ExportTable.class, e, e.getMessage());
}

}


以上代码只是本人项目中实现保存表格数据到excel2007中的一个接口代码,大家需要借鉴的话;只需要提取其中一段来参考即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel2007 POI java
相关文章推荐