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

最近由于项目需要将表格数据保存由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中的一个接口代码,大家需要借鉴的话;只需要提取其中一段来参考即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值