POI实现将表格数据保存到excel2007
2017-06-16 15:26
302 查看
最近由于项目需要将表格数据保存由excel2003转移到excel2007中(因为excel2007可以保存百万级的数据),之前采用的是jxl来操作excel,现在需改用POI来操作excel,现完成编码及测试工作,顺便记录下,以备后忘。
jar包:
这是保存数据的接口代码:
以上代码只是本人项目中实现保存表格数据到excel2007中的一个接口代码,大家需要借鉴的话;只需要提取其中一段来参考即可。
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中的一个接口代码,大家需要借鉴的话;只需要提取其中一段来参考即可。
相关文章推荐
- Java—POI实现数据导入Excel提示用户是保存,打开
- java SpringMVC中 POI读取数据库数据并写入Excel表格中,并实现下载功能
- JqGrid 学习笔记---实现在jqGrid表格上动态的加行、删行,最后点击“保存”按钮,与后台交互,保存数据
- POI实现读写Excel2007完整示例--每分钟约处理7000行数据
- 使用poi实现导出excel数据表格
- DhtmlxGrid组件应用---结合Ajax实现对表格数据的无刷新操作
- 用Swing实现数据表格功能
- XML+XSLT实现表格颜色交替,某列背景根据数据显示不同颜色
- 利用userData实现客户端保存表单数据
- 利用userData实现客户端保存表单数据
- JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 基于Ajax技术实现的下拉数据表格组件
- 转载:Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 用HTC组件实现可拖拽的数据表格
- 在实际开发中碰到的小问题,保存数据时提示:对象必须实现 IConvertible
- 源码——JavaScript实现的数据表格冻结列、调整列宽和客户端排序
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)