本文主要是介绍RCP项目_poi下载Excel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
poi所用的包:
poi-3.8-20120326.jar
poi-examples-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
xmlbeans-2.3.0.jar
xwork-core-2.2.1.1.jar
注意:要在plugin.xml文件的Runtime中的ClassPath加入包,没有加载会访问不到
/** * 存储sheet的数据 * @author suhuafu * */ public class PaperDataVO { public PaperDataVO() { } public PaperDataVO(String workbookName, List<String[]> list) { this.workbookName = workbookName; this.list = list; } private String workbookName="default";//工作表名称 private List<String[]> list = new ArrayList<String[]>();//动态值队 public String getWorkbookName() { return workbookName; }
@Override public void widgetSelected(SelectionEvent e) { int count = table.getColumnCount(); TableItem[] items = table.getItems(); final List<String[]> list = new ArrayList<String[]>(); String[] str = null; for (int i = 0; i < items.length; i++) { str = new String[count]; for (int j = 0; j < count; j++) { str[j] = items[i].getText(j); } list.add(str); } //创建文件夹对话框 DirectoryDialog direcory = new DirectoryDialog(getSite().getShell()); direcory.open(); final String path = direcory.getFilterPath(); //打开处理窗口 ProgressMonitorDialog pmd = new ProgressMonitorDialog(getSite().getShell()); //创建进度条对象 IRunnableWithProgress rwp = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor){ //totalWork为IProgressMonitor.UNKNOWN时表示不知道任务的总量 //将在进度条上显示一个来回移动的进度条 monitor.beginTask("正在下载..." , IProgressMonitor.UNKNOWN); //TODO 相应的业务逻辑 monitor.subTask("读取数据..."); if(path != ""){ List<PaperDataVO> pdvs = new ArrayList<PaperDataVO>(); PaperDataVO paper = new PaperDataVO(); paper.setWorkbookName("报销单"); paper.setList(list); pdvs.add(paper); OutputStream tempOut = null; InputStream in = null; try { //获取插件,获取文件的相对路径 in = Platform.getBundle("com.welldell.rcp.render.db") .getEntry("fileTemplate/renderMain.xlsx").openStream(); //生成文件路径 String src = path+"\\报销单"+ DateFormatUtil.formatDateToString(new Date(System.currentTimeMillis()), DateFormatUtil.V_LONG_YMD) +".xlsx"; tempOut = new FileOutputStream(src); WriteExcelUtil.buildExcel(in, tempOut, pdvs); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException ioe) { // TODO Auto-generated catch block ioe.printStackTrace(); } } monitor.subTask("处理完成!"); monitor.done(); } }; try { pmd.run(true, false, rwp); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } });
public static void buildExcel(InputStream ins, OutputStream os, List<PaperDataVO> pdvs) { if (pdvs == null || pdvs.size() == 0) { return; } try { Sheet sheet = null; Row row = null;//行 Cell cell = null;//单元格 Workbook wb = new XSSFWorkbook(ins); int sheetNum = wb.getActiveSheetIndex();//模板sheet的数量 for (int i = 0; i < pdvs.size(); i++) { PaperDataVO pdv = pdvs.get(i); if(sheetNum < i){ sheet = wb.getSheetAt(wb.getActiveSheetIndex());//获取激活中的sheet }else{ sheet = wb.getSheetAt(i); if(sheet == null) sheet = wb.createSheet(pdv.getWorkbookName()+i); } List<String[]> list = pdv.getList(); int rowNum = list.size()+1; int cellNum = sheet.getRow(0).getPhysicalNumberOfCells(); for (int j = 1; j < rowNum ; j++) {//循环行 row = sheet.createRow(j); for (int k = 0; k < cellNum; k++) {//循环行的列 cell = row.createCell(k); cell.setCellValue(list.get(j-1)[k]); } } } wb.write(os); os.flush(); os.close(); } catch (Exception ex) { ex.printStackTrace(); } }
这篇关于RCP项目_poi下载Excel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!