Excel多Sheet导出

Excel 多个Sheet 导出

       上次贴了几个关于上传下载的 发现以接口的方式贴过来,读起来很混乱。直接索性贴上工具类,附上测试,自己根据需要再去封装比较好。我直接从项目里抠过来了,同事兼哥们写的……

       1.1多sheetExcel工具类

package com.tm.util;

import java.util.List;
import java.util.Map;

public class ManySheetExcelUtils {

	private String sheetName;// sheet名称
	
	private List<PropSetter> props;// 属性设置
	
	private List<Map<String, Object>> datas;// 数据信息 

	/*******  getter、setter 省略  ******/
	public ManySheetExcelUtils() {
		super();
	}

	public ManySheetExcelUtils(String sheetName, List<PropSetter> props,
			List<Map<String, Object>> datas) {
		super();
		this.sheetName = sheetName;
		this.props = props;
		this.datas = datas;
	}
}

         1.2组成Excel数据工具类   

      

package com.tm.util;

/***
 * 组成list数据的实体
 * 
 * @author tablemiao
 *
 */
public class PropSetter {

	private String rOne; //第一行标题
	
	private String rTwo; //第二行标题
	
	private String prop;//对应的导出数据的字段名
	
	private String type;//数据类型
	
	private int width;//表格宽度
	
	private boolean color;//颜色

	/*****  Getter 、Setter 省略********/
	public PropSetter(String rOne, String prop, int width) {
		super();
		this.rOne = rOne;
		this.prop = prop;
		this.width = width;
	}
	
	public PropSetter(String rOne, String rTwo, String prop, int width) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.width = width;
	}

	public PropSetter(String rOne, String rTwo, String prop, int width,
			boolean color) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.width = width;
		this.color = color;
	}

	public PropSetter(String rOne, String rTwo, String prop, String type,
			int width) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.type = type;
		this.width = width;
	}

	public PropSetter() {
		super();
	}
	
}

 

       1.3 组装加测试

package com.tm.util;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 多sheet导出模板
 * */
public class ManySheetExportUtils {
	
	private static Logger logger = LoggerFactory.getLogger(CreateMapExcel.class);

	private static SXSSFWorkbook workbook = new SXSSFWorkbook();

	private static Sheet sheet;

	private static CellStyle titleStyle;

	private static CellStyle stringStyle;

	private static CellStyle longStyle;

	private static CellStyle doubleStyle;

	static {
		DataFormat format = workbook.createDataFormat();

		Font titleFont = workbook.createFont();
		titleFont.setFontName("微软雅黑");
		titleFont.setFontHeightInPoints((short) 10); // 字体大小
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);// 加粗

		Font contentFont = workbook.createFont();
		contentFont.setFontName("微软雅黑");
		contentFont.setFontHeightInPoints((short) 9);

		titleStyle = workbook.createCellStyle();
		titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
		titleStyle.setBorderBottom(CellStyle.BORDER_THIN);
		titleStyle.setBorderLeft(CellStyle.BORDER_THIN);
		titleStyle.setBorderRight(CellStyle.BORDER_THIN);
		titleStyle.setBorderTop(CellStyle.BORDER_THIN);
		titleStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);// 填暗红色
		titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		titleStyle.setFont(titleFont);
		titleStyle.setWrapText(true);

		stringStyle = workbook.createCellStyle();
		stringStyle.setAlignment(CellStyle.ALIGN_LEFT);
		stringStyle.setBorderBottom(CellStyle.BORDER_THIN);
		stringStyle.setBorderLeft(CellStyle.BORDER_THIN);
		stringStyle.setBorderRight(CellStyle.BORDER_THIN);
		stringStyle.setBorderTop(CellStyle.BORDER_THIN);
		stringStyle.setFont(contentFont);
		stringStyle.setWrapText(true);

		longStyle = workbook.createCellStyle();
		longStyle.setAlignment(CellStyle.ALIGN_LEFT);
		longStyle.setBorderBottom(CellStyle.BORDER_THIN);
		longStyle.setBorderLeft(CellStyle.BORDER_THIN);
		longStyle.setBorderRight(CellStyle.BORDER_THIN);
		longStyle.setBorderTop(CellStyle.BORDER_THIN);
		longStyle.setFont(contentFont);
		longStyle.setDataFormat(format.getFormat("0"));
		longStyle.setWrapText(true);

		doubleStyle = workbook.createCellStyle();
		doubleStyle.setAlignment(CellStyle.ALIGN_LEFT);
		doubleStyle.setBorderBottom(CellStyle.BORDER_THIN);
		doubleStyle.setBorderLeft(CellStyle.BORDER_THIN);
		doubleStyle.setBorderRight(CellStyle.BORDER_THIN);
		doubleStyle.setBorderTop(CellStyle.BORDER_THIN);
		doubleStyle.setFont(contentFont);
		doubleStyle.setDataFormat(format.getFormat("0.00"));
		doubleStyle.setWrapText(true);
	}

	/**
	 * @param
	 * 		utils  全部属性信息
	 * 
	 * */
	public static SXSSFWorkbook createExcel(List<ManySheetExcelUtils> utils) {
		long startTime = System.currentTimeMillis();
		
		Cell cell;
		Row row;
		for (int i = 0; i < utils.size(); i++) {
			sheet = workbook.createSheet();
			workbook.setSheetName(i, utils.get(i).getSheetName());// 根据属性创建sheet页
			logger.info("第" + i + "次sheet创建完成");
			Row rowOne = sheet.createRow(0);
			rowOne.setHeight((short) 350);
			List<PropSetter> props = utils.get(i).getProps();
			for (int j = 0; j < props.size(); j++) {// 标题的设置
				cell = rowOne.createCell(j);
				sheet.setColumnWidth(j, props.get(j).getWidth()); // 宽度
				cell.setCellStyle(titleStyle);
				cell.setCellValue(props.get(j).getrOne()); // 标题
			}
			logger.info("第" + i + "次表头信息创建完成");
			// 内容设置
			List<Map<String, Object>> datas = utils.get(i).getDatas();
			logger.info("准备执行第" + i + "次数据填充操作");
			if (datas.size() != 0) {
				for (int m = 0; m < datas.size(); m++) {
					row = sheet.createRow(m + 1);
					row.setHeight((short) 310);
					for (int n = 0; n < props.size(); n++) {
						cell = row.createCell(n);
						Object value = datas.get(m).get(props.get(n).getProp());
						if (value == null) {
							cell.setCellValue("");
							cell.setCellStyle(stringStyle);
						} else {
							try {
								cell.setCellType(HSSFCell.CELL_TYPE_STRING);
								cell.setCellValue(Long.valueOf(String.valueOf(value)));
								cell.setCellStyle(longStyle);
							} catch (Exception e) {
								try {
									cell.setCellType(HSSFCell.CELL_TYPE_STRING);
									cell.setCellValue((Double.valueOf(String.valueOf(value))));
									cell.setCellStyle(doubleStyle);
								} catch (Exception e1) {
									cell.setCellType(HSSFCell.CELL_TYPE_STRING);
									cell.setCellValue(String.valueOf(value));
									cell.setCellStyle(stringStyle);
								}
							}
						}
					}
				}
			}
		}
		logger.info("导出总计耗时: {}", System.currentTimeMillis() - startTime + "毫秒!");
		return workbook;
	}

	@Test
	public void test() throws Exception {
		List<PropSetter> props = new ArrayList<PropSetter>();
		props.add(new PropSetter("客户ID", "id", 3000));
		props.add(new PropSetter("客户名称", "name", 4000));
		props.add(new PropSetter("客户地市", "city", 4000));
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		map = new HashMap<String, Object>();
		map.put("id", "10000001");
		map.put("name", "上海移动");
		map.put("city", "上海市");
		datas.add(map);// 第一条数据
		map = new HashMap<String, Object>();
		map.put("id", "10000002");
		map.put("name", "北京移动");
		map.put("city", "北京市");
		datas.add(map);// 第二条数据
		map = new HashMap<String, Object>();
		map.put("id", "10000003");
		map.put("name", "重庆移动");
		map.put("city", "重庆市");
		datas.add(map);// 第三条数据
		
		List<PropSetter> props2 = new ArrayList<PropSetter>();
		props2.add(new PropSetter("集团编号", "id", 4000));
		props2.add(new PropSetter("服务等级", "level", 3500));
		props2.add(new PropSetter("属地", "dependency", 3500));
		List<Map<String, Object>> datas2 = new ArrayList<Map<String, Object>>();
		Map<String, Object> map2 = null;
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000001");
		map2.put("level", "标准");
		map2.put("dependency", "浦东");
		datas2.add(map2);// 第一条数据
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000002");
		map2.put("level", "金牌");
		map2.put("dependency", "北区");
		datas2.add(map2);// 第二条数据
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000003");
		map2.put("level", "银牌");
		map2.put("dependency", "南区");
		datas2.add(map2);// 第三条数据

		List<ManySheetExcelUtils> excelUtils = new ArrayList<ManySheetExcelUtils>();
		excelUtils.add(new ManySheetExcelUtils("sheet0", props, datas));
		excelUtils.add(new ManySheetExcelUtils("集团信息", props2, datas2));
		excelUtils.add(new ManySheetExcelUtils("sheet2", props2, datas2));

		OutputStream outputStream = new FileOutputStream("Z:/多sheet.xlsx");
		SXSSFWorkbook workbook = ManySheetExportUtils.createExcel(excelUtils);
		workbook.write(outputStream);
		outputStream.flush();
		outputStream.close();
	}

}

    1.4附上结果

 

 

猜你喜欢

转载自tablemiao.iteye.com/blog/2208619