zoukankan      html  css  js  c++  java
  • NC 5导出Excel

    Excel导出功能
    
    NC中功能事件代码:
    @Override
    	protected void onBoRefresh() throws Exception {
    		UIFileChooser fc = new UIFileChooser();//文件选择器
    		fc.setDialogType(UIFileChooser.SAVE_DIALOG);// 指示 UIFileChooser 支持 "Save" 文件操作的类型值。
    //		fc.setFileSelectionMode(UIFileChooser.FILES_AND_DIRECTORIES);//指示显示文件和目录。
    		fc.setFileSelectionMode(UIFileChooser.FILES_ONLY);//指示仅显示文件。
    		fc.setFileFilter(new ExcelFileFilter());// 设置当前文件过滤器。
    		fc.setMultiSelectionEnabled(false);//设置文件选择器,以允许选择多个文件。
    		int i = fc.showSaveDialog(this.getBillUI()); //弹出一个 "Save File" 文件选择器对话框。
    //		fc.showOpenDialog(this.getBillUI()); //弹出一个 "Open File" 文件选择器对话框。
    		if(i == UIFileChooser.APPROVE_OPTION){
    			String filePatch = fc.getSelectedFile().getPath();
    			BillCardPanel cardPanel = this.getBillCardPanelWrapper().getBillCardPanel();
    			Boolean bool = ExportExcel.writeJxlByTableModel(filePatch, cardPanel);
    			if(bool){
    				MessageDialog.showHintDlg(this.getBillUI(), "提示", "导出信息成功!");
    				return;
    			}
    		}else{
    			System.out.println("撤销成功");
    		}
    	}
    文件过滤类:
    package nc.ui.ldzl.order;
    
    import java.io.File;
    
    import javax.swing.filechooser.FileFilter;
    
    /**
     * @author sj
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class ExcelFileFilter extends FileFilter {
    	private String filters = "xls";
    //	private String filters2 = "xlsx";
    	private String description = "Microsoft Excel (*.xls)";
    	/**
    	 * MyFileFilter 构造子注解。
    	 */
    	public ExcelFileFilter() {
    		super();
    	}
    	/**
    	 * Whether the given file is accepted by this filter.
    	 */
    	public boolean accept(java.io.File f)//此过滤器是否接受给定的文件。
    	{
    		if (f != null)
    		{
    			if (f.isDirectory())//测试此抽象路径名表示的文件是否是一个目录。
    			{
    				return true;
    			}
    			String extension = getExtension(f);
    			if(extension != null && extension.trim().equals(this.filters))
    			{
    				return true;
    			}
    		}
    		return false;
    	}
    	/**
    	 * The description of this filter. For example: "JPG and GIF Images"
    	 * @see FileView#getName
    	 */
    	public String getDescription()//此过滤器的描述。
    	{
    		return description;
    	}
    	
    	public static String getExtension(File f) {
    		if(f != null) {
    			String filename = f.getName();
    			int i = filename.lastIndexOf('.');
    			if(i>0 && i<filename.length()-1) {
    				return filename.substring(i+1).toLowerCase();
    			};
    		}
    		return null;
    	}
    }
    导出Excel代码类:
    package nc.ui.pub.util;
    
    import java.io.*;
    import nc.ui.pub.bill.BillCardPanel;
    import nc.ui.pub.bill.BillScrollPane.BillTable;
    import jxl.*;
    import jxl.write.*;
    import jxl.write.biff.RowsExceededException;
    /**
     * 用来导出Excel的工具
     */
    public class ExportExcel {
    
    	public static boolean writeJxlByTableModel(String filePatch,BillCardPanel cardPanel) {
    		BillTable table = (BillTable) cardPanel.getBillTable();
    		WritableWorkbook writableWorkbook = null;
    		OutputStream os = null;
    		if (table == null || table.getRowCount() <= 0) {
    			return false;
    		}
    		if (!filePatch.endsWith(".xls")) {
    			System.out.println("=======不是正确的xls格式,请核查==========");
    			return false;
    		}
    		try {
    			os = new FileOutputStream(filePatch);
    			// 创建可写簿
    			writableWorkbook = Workbook.createWorkbook(os);
    			// 创建工作表
    			WritableSheet ws = writableWorkbook.createSheet("楼号档案页签", 0);
    			// 创建一个内容 第一个整数为 列,第二个整数为 行
    			Label label = null;
    			int rows = table.getRowCount();
    			int cols = table.getColumnCount() - 1;
    			String[] headitem = new String[] { "vbillno", "pactname","reserve4" };//表头字段
    			for (int row = 0; row < rows + 1; row++) {
    				for (int col = 0; col < headitem.length; col++) {
    					if (row == 0) {
    						String headerName = cardPanel.getHeadItem(headitem[col]).getName();
    						label = new Label(col, row, headerName);
    						ws.addCell(label);
    						ws.setColumnView(col,headerName == null ? 10: (headerName.length() > 2 ? headerName.length() * 3 : headerName.length() * 6));
    					} else {
    						label = new Label(col, row, cardPanel.getHeadItem(headitem[col]).getValue());
    						ws.addCell(label);
    					}
    				}
    				for (int col = headitem.length; col < cols + headitem.length; col++) {
    					if (row == 0) {
    						String headerName = table.getTableHeader().getColumnModel().getColumn(col - headitem.length).getHeaderValue() == null ? "": 
    							table.getTableHeader().getColumnModel().getColumn(col - headitem.length).getHeaderValue().toString();
    						label = new Label(col, row, headerName);
    						ws.addCell(label);
    						ws.setColumnView(col,headerName == null ? 10: (headerName.length() > 2 ? headerName.length() * 3 : headerName.length() * 6));
    					} else {
    						label = new Label(col, row, table.getValueAt(row - 1,col - headitem.length) == null ? "" : table.getValueAt(row - 1, col - headitem.length).toString());
    						ws.addCell(label);
    					}
    				}
    			}
    			writableWorkbook.write();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (RowsExceededException e) {
    			e.printStackTrace();
    		} catch (WriteException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if(os != null){
    					os.close();
    				}
    				if(writableWorkbook != null){
    					writableWorkbook.close();
    				}
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (WriteException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		return true;
    	}
    }
    

      


    作者:冬瓜茶饮料
    出处:http://www.cnblogs.com/dongguacha/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组合
    动态网站项目(Dynamic Web Project)登录功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8)(js验证+cookie)
    Oracle数据库scott用户无法导入数据的解决方法
    Win10安装Oracle 11g后解决sqldeveloper缺少快捷方式的问题
    【转载】win10系统安装oracle11g详细步骤
    WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
    WPF学习笔记(7):DataGrid中数字自定义格式显示
    WPF学习笔记(6):DataSet更新后台数据库个别列失败的问题
    WPF学习笔记(5):两个DataGrid的滚动条实现同步滚动
    WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件
  • 原文地址:https://www.cnblogs.com/dongguacha/p/7216226.html
Copyright © 2011-2022 走看看