使用easy-Excel实现Excel的读取与导出

一.Excel数据的读取

1.java代码

package com.immo.framework.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.easy.excel.ExcelContext;
import org.easy.excel.result.ExcelImportResult;

import com.immo.framework.pojo.ygyy.TMonitorDrugList;

public class ExcelUtils {
	public static void main(String[] args) {
		String path = "D:/监控药物数据.xls";
		List list = new ExcelUtils().getExcelList(path);
		for(TMonitorDrugList tMonitorDrugList : list){
			System.out.println("tMonitorDrugList----"+tMonitorDrugList);
		}
	}
	
	//Excel的读取
	public List getExcelList(String path){
		List notEmptyList = null;
		try {
			InputStream excelIn =  new FileInputStream(path);
			ExcelContext excelContext = new ExcelContext("excel-config.xml");
			ExcelImportResult result = excelContext.readExcel("tmonitorDrug", 0, excelIn);
			List list= result.getListBean();
			 notEmptyList = new ArrayList();
			for(TMonitorDrugList tMonitorDrugList : list){
				if(tMonitorDrugList.isEmpty()){
					notEmptyList.add(tMonitorDrugList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return notEmptyList;
	}
}


2.配置excel-config.xml,这里类似于映射,把表格的表头映射成javabean的属性对应。



	
	
	
	
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
		
		
		
		
		
		
		
	
	


二.Excel的导出。

1.javaBean

public class User {  
    private int userId;  
    private String name;  
    private String sex;  
    private String address;  
      
      
        public int getUserId() {  
        return userId;  
    }  
  
  
    public void setUserId(int userId) {  
        this.userId = userId;  
    }  
  
  
    public String getName() {  
        return name;  
    }  
  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
  
    public String getSex() {  
        return sex;  
    }  
  
  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
  
  
    public String getAddress() {  
        return address;  
    }  
  
  
    public void setAddress(String address) {  
        this.address = address;  
    }  
  
  
}  

2.导出到Excel调用的方法。

/** 
 * 导出Excel 
 */  
public void exportUserInfoToExcel() throws IOException{  
    OutputStream out = response.getOutputStream();  
    response.setContentType("application/x-download");  
    //设置导出文件名称  
    response.setHeader("Content-Disposition", "attachment;filename="+DateUtil.getNow("yyyyMMddHHmmssSSS")+".xls");  
      
    Excel excel = new Excel();  
      
    //查询内容list,userService.getUserList方法自己实现  
    List list = userService.getUserList();  
    //表头数组  
    String[] header = {"用户ID","用户名","性别","地址"};  
    //表单title  
    String title = "用户信息列表";  
    int headerLen = header.length;  
    //单元格边框颜色  
    Color borderColor = Color.GREY_50_PERCENT;  
    //表头字体  
    IFontEditor headFont = new IFontEditor(){  
        public void updateFont(Font font){  
            font.fontHeightInPoints(14)  
            .boldweight(BoldWeight.BOLD)  
            .color(Color.WHITE);  
        }  
    };  
    //标题字体  
    IFontEditor titleFont = new IFontEditor(){  
        public void updateFont(Font font){  
            font.fontHeightInPoints(30)  
            .boldweight(BoldWeight.BOLD)  
            .color(Color.DARK_BLUE);  
        }  
    };  
    //设置表单信息  
    excel.setWorkingSheet(0)//设置第1个工作表为工作状态  
         .sheetName(title+"1")   
         .fitToPage(true)  
         .horizontallyCenter(true)  
         .printGridlines(false)  
         .displayGridlines(true)  
         .autobreaks(true)  
         .printSetup(new IPrintSetup(){  
            public void setup(HSSFPrintSetup printSetup) {  
                printSetup.setLandscape(true);//是否打印背景  
                printSetup.setFitHeight((short)1);//调整缩放  
                printSetup.setFitWidth((short)1);//调整缩放  
            }  
        });  
      
    //设置标题内容,标题行列合并,标题样式  
    excel.row(0).height(60);  
    excel.cell(0, 0).value(title)  
        .align(Align.CENTER)  
        .font(titleFont);  
    excel.region(0, 0, 0, headerLen-1).merge();//合并标题的单元格  
      
    //设置表头及样式  
    for(int i=0;i

3.前端页面的输入方法。
  
  
js添加内容:  
jConfirm('确定要导出excel文件?',  '消息', function(rs) {  
       if (rs == true) {  
         var exportXlsButton = document.getElementById("exportXlsButton");  
         exportXlsButton.href = url; //url地址  
         exportXlsButton.click();                
  }  
});
//url地址指向上面的方法即可。



你可能感兴趣的:(小工具)