• 实习任务——导出excel


    将grid组件中显示的数据导出为excel文件

    首先使用sql语句,将数据库中的信息查出来,然后根据查询条件进行筛选过滤,将得到的信息进行解析(因为在数据库中保存的是代码,需要解析为文字),在grid组件中表示出来。

    然后对于这些信息需要一个导出excel功能,导出为excel表,显示所有的信息。

    首先需要一个导出excel的方法

    public class LicenseEnableExport {
    	/**
    	 * 生成sheet
    	 * 
    	 */
    		private String filename;
    
    		private String sheetname;
    
    		public LicenseEnableExport(Grid2 grid, String filename, String sheetname) {
    			this.filename = filename;
    			this.sheetname = sheetname;
    			doExport(grid);
    		}
    
    		public void doExport(Grid2 grid) {
    			final BatchExportor exportor = new BatchExportor(new GridDataIterator(grid));
    			exportor.setToSingleFile(true);
    
    			Display.getCurrent().exportFile(filename, "application/vnd.ms-excel", 0, new Exporter() {
    				public void run(OutputStream outputStream) throws IOException {
    					try {
    						exportor.Export(outputStream);
    					} catch (Exception e) {
    						e.printStackTrace();
    					}finally{
    						outputStream.close();
    					}
    				}
    			});
    		}
    
    		private class GridDataIterator implements GridIterator {
    
    			private final int pageCount;
    			private int page;
    			private Grid2 grid;
    
    			public GridDataIterator(Grid2 grid2) {
    				this.pageCount = 1;
    				this.page = 0;
    				this.grid = grid2;
    			}
    
    			public GridData getGridData() {
    				return grid.getModel().toGridData();
    			}
    
    			public String getTitle() {
    				return sheetname;
    			}
    
    			public boolean next() throws ExcelException {
    
    				this.page++;
    				return this.page <= this.pageCount;
    			}
    		}
    
    	
    }
    
    

    其次需要一个按钮监听,调用导出excel的方法,同时说明输出的excel文件名和输出的excel文件工作表名。
    以下为代码

    	/**
    	 * 导出excel按钮监听
    	 * @param mouseEvent
    	 */
    	protected void on_btn_7_Click(MouseEvent mouseEvent) {
    		// TODO Auto-generated method stub
    		
    			// 初始化grid2中的数据
    			initGrid1();
    			setData1(xmlist);
    			controls.cmp_5.layout();
    
    
    		Composite gridComp = controls.cmp_5;
    	
    		if (gridComp.getChildren().length <= 0) {
    			MessageDialog.alert("提示", "无导出数据");
    			return;
    		}
    		Grid2 epu = (Grid2) gridComp.getChildren()[0];
    		LicenseEnableExport exportUtil = new LicenseEnableExport(epu, "开采底数管理列表.xls", "开采底数管理列表");
    		exportUtil.doExport(epu);
    		
    	}
    
  • 相关阅读:
    终端不显示 git branch 名字
    多线程下bufferedwriter若不关闭并不能记下所有log
    anaconda prompt execute jupyter notebook, can't open notebook
    conda 创建新环境下载包失败
    failed to install jupyter_contrib_nbextensions
    failed to install nb_conda
    Windows Server 2012R2 修复CVE-2016-2183(SSL/TLS)漏洞的办法
    SSL/TLS协议信息泄露漏洞(CVE-2016-2183)解决办法
    记录win NFS公网映射开放端口
    出题器
  • 原文地址:https://www.cnblogs.com/mingey/p/6391062.html
Copyright © 2020-2023  润新知