Java中Excel导出工具类

导出EXcel工具类如下:

package com.yyw.utils;

import jxl.Sheet;
import jxl.Workbook;
import jxl.biff.DisplayFormat;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Excel处理工具类
 */
public class ExcelUtils {

	private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * Excel模板下载
     * @param filePath
     * @param response
     * @param fileName
     * @return
     * @throws Exception
     */
    public static boolean downLoadFile(String filePath,HttpServletResponse response,
    		String fileName)throws Exception {
        File file = new File(filePath);
        
        response.setContentType("application/msexcel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\""
                + new String(fileName.getBytes("GB2312"), "ISO8859-1") + "\"");
        response.setContentLength((int)file.length());
        
        byte[] buffer = new byte[4096];//缓冲区
        BufferedOutputStream output = null;
        BufferedInputStream input = null;
        
        try {
        	 output = new BufferedOutputStream(response.getOutputStream());
             input = new BufferedInputStream(new FileInputStream(file));
             int n = -1;

            //遍历,开始下载
            while ((n = input.read(buffer, 0, 4096)) > -1) {
                output.write(buffer, 0, n);
             }
            output.flush();   
            response.flushBuffer();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("Excel模板下载异常:"+e.getMessage());
		}finally{
			 //关闭流
	         if (input != null){
				 input.close();
			 }
	         if (output != null){
				 output.close();
			 }
	    }
	    return true;
    }

  //控制Excel里面每个sheet单元最多有多少行
  	private static int MAX_ROWS = 50000;
  	
  	/**
  	 * 填充Excel各单元格的值
  	 * @param 
  	 * @param list 数据list
  	 * @param os 输出流
  	 * @param colNames 第一行列字段的中文名称
  	 * @param colParams 各列对应对象的属性
  	 * @param sheetName sheet的名称
  	 */
  	public static  void setExcel(List list,OutputStream os,String[] colNames,String[] colParams,
  			String sheetName,Integer col)throws Exception{
  	
  		try {
  			WritableWorkbook workbook = Workbook.createWorkbook(os);
  			//设定一个sheet里面只能放50000条数据
  			int num = list.size()/MAX_ROWS + 1;
  			
  			for(int i=0;i list.size()){
  					num_end = list.size();
  				}
  				for(int k=num_start;k
	 * @param list 需要导出的数据
	 * @param os 输出流
	 * @param colNames 第一行列字段的中文名称
	 * @param colParams 各列对应对象的属性
	 * @param sheetName sheet的名称
	 * @param remakeMap 每一列对应的的数字代表什么意思,例如 sex 1,男;2,女;,导出时希望显示汉字而不是显示数字
	 */
	public static  void setExcelWithExpectNum(List list,OutputStream os,String[] colNames,String[] colParams,
									String sheetName,Integer col,Map> remakeMap)throws Exception{

		try {
			WritableWorkbook workbook = Workbook.createWorkbook(os);
			//设定一个sheet里面只能放50000条数据
			int num = list.size()/MAX_ROWS + 1;

			for(int i=0;i list.size()){
					num_end = list.size();
				}
				for(int k=num_start;k> entry : remakeMap.entrySet()){
							if (temp.equals(entry.getKey())){
								value = entry.getValue().get(value);
							}
						}
						if(StringUtil.isEmpty(value) || "null".equals(value)){
							label = new jxl.write.Label(colNum++,rowNum+1,"",wcf_cell);
						}else{
							label = new jxl.write.Label(colNum++,rowNum+1,value,wcf_cell);
						}
						sheet.addCell(label);
					}

					rowNum++;
				}

			}

			workbook.write();
			workbook.close();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("填充Excel各单元格异常:"+e.getMessage());
		}

	}
  	/**
  	 * 填充Excel各单元格的值
  	 * @param 
  	 * @param list
  	 * @param os
  	 * @param colNames 第一行列字段的中文名称
  	 * @param colParams 各列对应对象的属性
  	 * @param sheetName sheet的名称
  	 */
  	public static  void setToExcel(List list,OutputStream os,String[] colNames,String[] colParams,
  			String sheetName)throws Exception{
  	
  		try {
  			WritableWorkbook workbook = Workbook.createWorkbook(os);
  			//设定一个sheet里面只能放50000条数据
  			int num = list.size()/MAX_ROWS + 1;
  			
  			for(int i=0;i list.size()){
  					num_end = list.size();
  				}
  				for(int k=num_start;k void setExcelByCol(List list,OutputStream os,String[] colNames,String[] colParams,
  			String sheetName,Object[] colStyle)throws Exception{
  	
  		try {
  			WritableWorkbook workbook = Workbook.createWorkbook(os);
  			//设定一个sheet里面只能放50000条数据
  			int num = list.size()/MAX_ROWS + 1;
  			
  			for(int i=0;i list.size()){
  					num_end = list.size();
  				}
  				for(int k=num_start;k> parseExcelData(InputStream inputStream,String[] colProperty,String[] colName) throws Exception{
  		 if(colProperty.length == 0){
  			 throw new Exception("每一列对应的属性值不能为空!");
  		 }
			 
  		 List> list = new ArrayList>();
  		 Workbook workbook = Workbook.getWorkbook(inputStream);  //处理输入流
  		 Sheet[] sheet = workbook.getSheets();
  		 if (sheet != null) {
//  	 for(int i=0;i map = new LinkedHashMap();
  				 for(int k=0;k

引用如下:

 //存放信息的数组
 ArrayList> acountSelf_data1 = (ArrayList>)acountSelf_data.get("acountSelf_data");
 //excel表头
 String[] colNames = { 	"公司名称", "账期起始日期","账期停用日期","信用额度","省份","客户类型","门店数","MTD账期GMV","MTD自营现金GMV","MTD活动现金GMV","MTD合计GMV","剩余额度","MTD额度使用率","额度占用率","招商","运营","2017-12GMV","2018-01GMV","2018-02GMV","2018-03GMV","2018-04GMV","2018-05GMV","2018-06GMV","2018-07GMV","GMV"};
 //表头对应的字段名称
 String[] colParams = { 	"buyer_name", "start_date",	"end_date",	"credit_limit", "RCV_REG_PROV", "param_name", "CUST_Count","gmv_mtd_zq","gmv_mtd_xj1","gmv_mtd_xj2","gmv_mtd_all","avl_limit","rate_mtd_use","rate_use","op","bd","2017-12GMV","2018-01GMV","2018-02GMV","2018-03GMV","2018-04GMV","2018-05GMV","2018-06GMV","2018-07GMV","GMV"};
 //25为一共几列,sheetname是sheet名称
 ExcelUtils.setExcel(acountSelf_data1,out,colNames,colParams,null,25);

你可能感兴趣的:(Java,方法,类)