导出为Excel工具类

package com.cattsoft.common.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.Font;

public class ExportExcelUtils {
	/**
	 * 
	 * @param exportDataList 需导出数据
	 * @param headMap 文件头部
	 * @param name 文件名
	 * @param response 响应对象
	 * @param sheetName sheet名
	 * @throws Exception
	 */
	public void exportExcel(
			List<List<LinkedHashMap<String, Object>>> exportDataList,
			LinkedHashMap<String, String> headMap, 
			String name, 
			HttpServletResponse response,
			String sheetName) throws Exception {
		// 批量导出workBook
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 循环批量塞入数据
		for (List<LinkedHashMap<String, Object>> dataMap : exportDataList) {
			int dataMapSize = dataMap.size() - 1;
			String opRoadGroupBatchNo = MapUtils.getString(dataMap.get(dataMap.size() - 1), sheetName);
			HSSFSheet sheet = workbook.createSheet(opRoadGroupBatchNo);
			dataMap.remove(dataMap.get(dataMapSize));
			sheet.setDefaultColumnWidth(20);
			sheet.setColumnWidth(0, 5120);
			sheet.setColumnWidth(1, 10240);
			sheet.setColumnWidth(2, 5120);
			sheet.setColumnWidth(3, 10240);
			sheet.setColumnWidth(4, 10240);
			sheet.setColumnWidth(5, 5120);
			sheet.setColumnWidth(6, 10240);
			sheet.setColumnWidth(7, 5120);
			sheet.setColumnWidth(8, 10240);
			sheet.setColumnWidth(9, 10240);
			sheet.setColumnWidth(10, 10240);
			HSSFRow row = sheet.createRow(0);
			row.setHeightInPoints(50);
			Iterator<Entry<String, String>> headiterator = headMap.entrySet().iterator();
			int headCellSize = 0;
			int dataRowSize = 1;
			while (headiterator.hasNext()) {
				HSSFCell cell = row.createCell(headCellSize++);
				cell.setCellStyle(createHeadStype(workbook));//设置样式
				cell.setCellValue(headiterator.next().getValue());//给列赋值
			}
			for (LinkedHashMap<String, Object> data : dataMap) {
				HSSFRow row2 = sheet.createRow(dataRowSize++);
				int cell2Size = 0;
				Set<String> dataKey = data.keySet();
				for (String keyValue : dataKey) {
					HSSFCell cell = row2.createCell(cell2Size++);
					String dataValue = (String) data.get(keyValue);
					cell.setCellValue(dataValue);
				}
			}
		}
		//输出流
		OutputStream output = null;
		try {
			output = response.getOutputStream();
			response.reset();
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setHeader("Content-Disposition","attachment;filename=" + new String((name + ".xls").getBytes(), "iso-8859-1"));
			workbook.write(output);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 支持导出表结构不一样的sheet页excel表格
	 * @param exportDataList 
	 * @param headMap 文件头部
	 * @param name 文件名
	 * @param response 响应对象
	 * @param sheetName sheet名
	 * @throws Exception
	 * 
	 * 		roadExportMap.put("SCHEET_NAME", "本地侧光路隐患分析");
		roadExportMap.put("EXPORT_DATA", roadExportmap);
		roadExportMap.put("EXP_HEADS", exp_roadHeadsStr);
		roadExportMap.put("EXP_COLS", exp_roadColsStr);
	 */
	@SuppressWarnings("unchecked")
	public void exportExcelTwo(
			List<Map<String,Object>> exportDataList,
			String fileName, 
			HttpServletResponse response) throws Exception {
		// 批量导出workBook
		HSSFWorkbook workbook = new HSSFWorkbook();
		for(Map<String,Object> exportDataMap : exportDataList) {
			String sheetName = MapUtils.getString(exportDataMap, "SCHEET_NAME");
			List<String> headList = (List<String>) exportDataMap.get("EXP_HEADS");
			List<String> colsList = (List<String>) exportDataMap.get("EXP_COLS");
			List<LinkedHashMap<String, Object>>  dataMapList = (List<LinkedHashMap<String, Object>>) exportDataMap.get("EXPORT_DATA");
			HSSFSheet sheet = workbook.createSheet(sheetName);
			HSSFRow headRow = sheet.createRow(0);
			CellStyle headStyle = createHeadStype(workbook);
			//设置头
			for(int i=0; i<headList.size(); i++) {
				Cell cell = headRow.createCell(i);
				cell.setCellStyle(headStyle);
				cell.setCellValue(headList.get(i));
				sheet.setColumnWidth(i, 10240);
			}
			int index = 1;
			for(LinkedHashMap<String, Object> dataMap : dataMapList) {
				HSSFRow bodyRow = sheet.createRow(index ++);
				for(int i=0; i<colsList.size(); i++) {
					String cols = colsList.get(i);
					bodyRow.createCell(i).setCellValue(MapUtils.getString(dataMap, cols));
				}
			}
		}
		//输出流
		OutputStream output = null;
		try {
			output = response.getOutputStream();
			response.reset();
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setHeader("Content-Disposition","attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
			workbook.write(output);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 文件样式
	 * @param workbook
	 * @return
	 */
	public CellStyle createHeadStype(HSSFWorkbook workbook) {
		Font warnfont = workbook.createFont();
		warnfont.setFontHeightInPoints((short) 10);
		warnfont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		warnfont.setColor(HSSFColor.BLACK.index);
		CellStyle headStyle = workbook.createCellStyle();
		headStyle.setWrapText(true);
		headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		headStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
		headStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		headStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		headStyle.setAlignment(CellStyle.ALIGN_CENTER);
		headStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		headStyle.setBorderBottom(CellStyle.BORDER_THIN);
		headStyle.setBorderRight(CellStyle.BORDER_THIN);
		headStyle.setBorderLeft(CellStyle.BORDER_THIN);
		headStyle.setBorderTop(CellStyle.BORDER_THIN);
		headStyle.setFont(warnfont);
		return headStyle;
	}

}

工具类的使用代码:

ExportExcelUtils exportExcelUtil = new ExportExcelUtils();
		try {
			exportExcelUtil.exportExcel(exportData, headMap, date, response, sheetName);
		} catch (Exception e) {
			e.printStackTrace();
		}

猜你喜欢

转载自blog.csdn.net/weixin_40486739/article/details/89703196