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
    评论
### 回答1: C代码可以使用第三方库,如LibXL或POI,将数据保存Excel文件中。 以LibXL为例,首先需要在代码中引用库文件,并创建一个Excel文件的对象。 然后可以通过以下代码创建一个工作表,并设置表头内容: ```c BookHandle book = xlCreateBook(); // 创建一个Excel文件对象 SheetHandle sheet = xlBookAddSheet(book, "Sheet1", NULL); // 创建一个工作表对象 xlSheetWriteStr(sheet, 1, 1, "姓名", NULL); // 设置表头内容 xlSheetWriteStr(sheet, 1, 2, "年龄", NULL); xlSheetWriteStr(sheet, 1, 3, "性别", NULL); ``` 接下来可以将数据写入表格中,假设有一个包含学生信息的结构体数组students[]: ```c for (int i = 0; i < student_count; i++) { xlSheetWriteStr(sheet, i+2, 1, students[i].name, NULL); // 写入姓名 xlSheetWriteNum(sheet, i+2, 2, students[i].age, NULL); // 写入年龄 xlSheetWriteStr(sheet, i+2, 3, students[i].gender, NULL); // 写入性别 } ``` 最后,保存Excel文件并释放相关资源: ```c xlBookSave(book, "students.xlsx"); // 保存Excel文件 xlBookRelease(book); // 释放Excel文件对象 ``` 这样就可以将数据保存Excel文件中了。 ### 回答2: C代码可以使用许多不同的方法将数据保存Excel中。其中一种常见的方法是使用开源库LibreOffice Calc来完成此任务。 首先,需要将LibreOffice Calc安装在计算机上,并将相关的头文件和库文件添加到C代码中。 然后,在C代码中,可以使用LibreOffice Calc提供的API来连接到Excel工作簿,并将数据保存到工作表中。 以下是一个简单的示例代码,用于将数据保存Excel工作表中: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uno.h> // LibreOffice Calc的头文件 int main() { uno_Bootstrap(); uno_Any desktop; uno_Any document; // 连接到LibreOffice Calc uno_getDesktop(&desktop); uno_getComponentByName(&desktop, "com.sun.star.frame.Desktop", &desktop); uno_invoke(&desktop, "loadComponentFromURL", &document, "private:factory/scalc", 0, NULL); uno_Any sheets; uno_getProperty(&document, "Sheets", &sheets); uno_Any sheet; uno_getByIndex(&sheets, 0, &sheet); // 获得第一个工作表 uno_Any cells; uno_getProperty(&sheet, "Cells", &cells); // 设置要保存数据 const char* data = "Hello, World!"; uno_invoke(&cells, "setString", NULL, 1, 1, uno_CreateAnyString(data)); // 保存Excel工作表 uno_invoke(&document, "storeAsURL", NULL, "output.xls", 0, NULL); // 断开与LibreOffice Calc的连接 uno_release(&cells); uno_release(&sheet); uno_release(&sheets); uno_release(&document); uno_release(&desktop); uno_Exit(); return 0; } ``` 以上的代码使用LibreOffice Calc的API连接到Excel工作簿,并将字符串"Hello, World!"保存到第一个工作表的单元格(1,1)中。然后,将工作簿保存为名为"output.xls"的Excel文件。 这只是一个简单的示例代码,实际上,根据需求的复杂程度,可能需要更多的代码来处理更多的数据和更多的操作。但是,上述代码提供了一个基本的框架,可以帮助你开始将数据保存Excel

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值