标签:++ 标题 app cto color str while mat you
@RequestMapping(value = "/exportXls") public void exportXls(@RequestParam String zcq, HttpServletRequest request, HttpServletResponse response) { List<Map<String,Object>> list = new ArrayList<>();//导出数据 String filename = "注册报考初步统计 .xls"; //String path="exportTitle/mntWorker.xml"; String[] head0 = new String[] { "日期", "注册", "", "", "", "报考","", "", "", "", ""};//在excel中的第2行每列的参数 /* 在excel中的第3行每列(合并列)的参数 这里需要注意的是 从第几列合并的时候 需要把前面不需要合并行的列 用"" 补上 (下表是从第7列 开始合并的行的 所以前面补了 6个"", 中间 兼职和总数之间 有一列是不需要合并行的 也补了一个"") */ String[] head1 = new String[] { "个人", "集体", "今日注册人数", "注册总人数", "个人", "集体","今日笔试报考科次", "今日非笔试报考科次", "笔试报考总科次","非笔试报考总科次"}; String[] headnum0 = new String[] { "1,2,0,0", "1,1,1,4","1,1,5,10"};//对应excel中的行和列,下表从0开始{"开始行,结束行,开始列,结束列"} String[] headnum1 = new String[] { "2,2,1,1", "2,2,2,2", "2,2,3,3","2,2,4,4","2,2,5,5", "2,2,6,6","2,2,7,7","2,2,8,8","2,2,9,9","2,2,10,10"}; String[] colName = new String[] { "MODIFYDATE","CNTLS","CNTJT", "CNTJT", "CNTJT", "COUNTBKGR", "COUNTBKJT" ,"COUNTBS", "COUNTFBS" , "COUNTFBS", "COUNTFBS"};//需要显示在excel中的参数对应的值,因为是用map存的,放的都是对应的key try { ExcelUtil.reportMergeXls(request, response, list, filename, head0, headnum0, head1, headnum1, colName,true);//utils类需要用到的参数 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
工具类:
package org.jeecg.common.util; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Map.Entry; import java.util.regex.Pattern; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; 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.CellReference; import org.apache.poi.ss.util.CellRangeAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /*** * excel 导出工具类 * @author Administrator * */ public class ExcelUtil { private static DecimalFormat df = new DecimalFormat("0"); // 默认单元格格式化日期字符串 private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 格式化数字 private static DecimalFormat nf = new DecimalFormat("0.00"); private final static Logger logger = LoggerFactory.getLogger(ExcelUtil.class); public static void export(List<Map<String, Object>> list, String filename, HttpServletResponse response, String titles) { try { response.setContentType("application/x-execl"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "ISO-8859-1")); ServletOutputStream outputStream = response.getOutputStream(); exportExcel(list, filename, outputStream, titles); if (outputStream != null) { outputStream.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 数据导出公用方法 */ public static void exportExcel(List<Map<String, Object>> list, String filename, ServletOutputStream outputStream, String titles) { try { Map<String, Object> map1 = list.get(0); StringBuilder sb = new StringBuilder(); String[] fields = titles.split(","); /*sb.append(title); String[] fields = sb.toString().split(",");*/ // 1、创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 1.1、创建合并单元格对象 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, fields.length - 1);// 起始行号,结束行号,起始列号,结束列号 CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 0, fields.length - 1);// 起始行号,结束行号,起始列号,结束列号 CellRangeAddress cellRangeAddress2 = new CellRangeAddress(1, 1, 7, 12);// 起始行号,结束行号,起始列号,结束列号 // 1.2、头标题样式 HSSFCellStyle style1 = createCellStyle(workbook, (short) 18); // 1.3、列标题样式 HSSFCellStyle style2 = createCellStyle(workbook, (short) 13); String headRow = filename; // 2、创建工作表 HSSFSheet sheet = workbook.createSheet(headRow); // 2.1、加载合并单元格对象 sheet.addMergedRegion(cellRangeAddress); sheet.addMergedRegion(cellRangeAddress1); sheet.addMergedRegion(cellRangeAddress2); // sheet.addMergedRegion(cellRangeAddress4); // 设置默认列宽 sheet.setDefaultColumnWidth(22); sheet.setDefaultRowHeightInPoints(22); sheet.autoSizeColumn(1, true); // // 3、创建行 // 3.1、创建头标题行;并且设置头标题 HSSFRow row1 = sheet.createRow(0); row1.setHeightInPoints(50); HSSFCell cell1 = row1.createCell(0); // 加载单元格样式 cell1.setCellStyle(style1); cell1.setCellValue(headRow); // 3.1、创建副标题行;并且设置 HSSFRow row2 = sheet.createRow(1); row2.setHeightInPoints(25); HSSFCell cell2 = row2.createCell(0); // 3.2、创建列标题行;并且设置列标题 HSSFRow row3 = sheet.createRow(2); /*String[] titles = new String[fields.length]; for (int i = 0; i < fields.length; i++) { titles[i] = getTitles(type, fields[i]); }*/ for (int i = 0; i < fields.length; i++) { HSSFCell cell5 = row3.createCell(i); // 加载单元格样式 cell5.setCellStyle(style2); cell5.setCellValue(fields[i]); } // 备注详情 HSSFRow row36 = sheet.createRow(1); row36.setHeightInPoints(18); HSSFCell cell36 = row36.createCell(0); // 加载单元格样式 HSSFCellStyle style36 = workbook.createCellStyle(); style36.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平居左 style36.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中 cell36.setCellStyle(style36); HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗字体 font.setFontHeightInPoints((short) 16); style36.setFont(font); //cell36.setCellValue(data_batch); // 自适应中文 // 4、操作单元格;写入excel if (list != null && list.size() > 0) { for (int j = 0; j < list.size(); j++) { Map<String, Object> map = list.get(j); //Object bean = getBean(type, map); HSSFRow row = sheet.createRow(j + 3); int i = 0; for (Map.Entry<String, Object> entry : map.entrySet()) { String val = ""; if ((entry.getValue()) != null && (entry.getValue()) != "null") { val = getValue(entry.getKey(), entry.getValue().toString()); } else { val = " "; } //String val=() ? entry.getValue().toString() : "null"; HSSFCell cell = row.createCell(i); i++; cell.setCellValue(val); logger.info("第" + (j + 3) + "行,第" + val + "--赋值成功"); } /*for (int i = 0; i < fields.length; i++) { //String value = CommonUtil.getValue(bean, StringUtils.toCamelCase(fields[i])); //value = getValue(fields[i], value); //cell.setCellValue(value); cell.setCellValue(list.get(j).get(i).toString()); //cell.setCellValue(list.get(j).get(i).toString()); }*/ } } // 5、输出 workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 创建单元格样式 * * @param workbook 工作簿 * @param fontSize 字体大小 * @return 单元格样式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) { HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 创建字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗字体 font.setFontHeightInPoints(fontSize); // 加载字体 style.setFont(font); return style; } public static String getValue(String field, String value) { if (field.equals("credLocation")) { if (value.equals("1")) { value = "库内人员 "; } if (value.equals("2")) { value = "库外人员"; } } if (field.equals("chongfu")) { if (value.equals("1")) { value = "否"; } else { value = "是"; } } if (field.equals("days")) { if (value.contains("-")) { String value1 = "逾期" + value + "天"; value = value1.replace("-", ""); } else { value = value + "天后保养"; } } if (field.equals("recordStatus")) { if (value.equals("1")) { value = "故障报修"; } else if (value.equals("2")) { value = "已派工"; } else if (value.equals("3")) { value = "已出发"; } else if (value.equals("4")) { value = "到达现场"; } else if (value.equals("5")) { value = "维修完成"; } else if (value.equals("6")) { value = "服务评定"; } } return value; } public static DecimalFormat getDf() { return df; } public static void setDf(DecimalFormat df) { ExcelUtil.df = df; } public static SimpleDateFormat getSdf() { return sdf; } public static void setSdf(SimpleDateFormat sdf) { ExcelUtil.sdf = sdf; } public static DecimalFormat getNf() { return nf; } public static void setNf(DecimalFormat nf) { ExcelUtil.nf = nf; } /** * 多行表头 * dataList:导出的数据;sheetName:表头名称; head0:表头第一行列名;headnum0:第一行合并单元格的参数 * head1:表头第二行列名;headnum1:第二行合并单元格的参数;detail:导出的表体字段 */ public static void reportMergeXls(HttpServletRequest request, HttpServletResponse response, List<Map<String, Object>> dataList, String sheetName, String[] head0, String[] headnum0, String[] head1, String[] headnum1, String[] detail,boolean isHeJi) throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); // 1、创建标题 String headRow = sheetName; // 1.1、头标题样式 HSSFCellStyle headstyle = createCellStyle(workbook, (short) 18); // 1.2、列标题样式 HSSFCellStyle style1 = createCellStyle(workbook, (short) 13); // 2、创建工作表 HSSFSheet sheet = workbook.createSheet(headRow); // 设置默认列宽 sheet.setDefaultColumnWidth(22); sheet.setDefaultRowHeightInPoints(22); sheet.autoSizeColumn(1, true); // 3、创建行 // 3.1、创建头标题行;并且设置头标题 HSSFRow row1 = sheet.createRow(0); row1.setHeightInPoints(50); HSSFCell cell1 = row1.createCell(0); // 加载单元格样式 cell1.setCellStyle(headstyle); cell1.setCellValue(headRow); // 3.1、创建副标题行;并且设置 HSSFRow row2 = sheet.createRow(1); row2.setHeightInPoints(25); HSSFCell cell2 = row2.createCell(0); // 3.2、创建列标题行;并且设置列标题 HSSFRow row3 = sheet.createRow(2); // 第一行表头标题 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, head0.length - 1)); HSSFRow row = sheet.createRow(0); row.setHeight((short) 0x349); HSSFCell cell = row.createCell(0); cell.setCellStyle(headstyle); CellUtil.setCellValue(cell, sheetName); // 第二行表头列名 row = sheet.createRow(1); for (int i = 0; i < head0.length; i++) { cell = row.createCell(i); cell.setCellValue(head0[i]); cell.setCellStyle(style1); } // 1.3、普通单元格样式(中文)样式 HSSFCellStyle style2 = workbook.createCellStyle(); //HSSFCellStyle style36 = workbook.createCellStyle(); style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平居左 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗字体 font.setFontHeightInPoints((short) 10); style2.setFont(font); //动态合并单元格 for (int i = 0; i < headnum0.length; i++) { String[] temp = headnum0[i].split(","); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol)); } //设置合并单元格的参数并初始化带边框的表头(这样做可以避免因为合并单元格后有的单元格的边框显示不出来) row = sheet.createRow(2);//因为下标从0开始,所以这里表示的是excel中的第三行 for (int i = 0; i < head0.length; i++) { cell = row.createCell(i); cell.setCellStyle(style1);//设置excel中第四行的1、2、7、8列的边框 if (i > 1) { for (int j = 0; j < head1.length; j++) { cell = row.createCell(j + 1); cell.setCellValue(head1[j]);//给excel中第三行的3、4、5、6列赋值("温度℃", "湿度%", "温度℃", "湿度%") cell.setCellStyle(style1);//设置excel中第三行的3、4、5、6列的边框 } } } //动态合并单元格 for (int i = 0; i < headnum1.length; i++) { String[] temp = headnum1[i].split(","); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol)); } // 设置列值-内容 for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow(i + 3);//标题、时间、表头字段共占了3行,所以在填充数据的时候要加3,也就是数据要从第4行开始填充 for (int j = 0; j < detail.length; j++) { Map tempmap = (HashMap) dataList.get(i); Object data = tempmap.get(detail[j]); cell = row.createCell(j); cell.setCellStyle(style2); cell.setCellType(HSSFCell.CELL_TYPE_STRING); CellUtil.setCellValue(cell, data); } } if (isHeJi){ row = sheet.createRow(dataList.size() + 3); cell = row.createCell(0); cell.setCellStyle(style2); cell.setCellType(HSSFCell.CELL_TYPE_STRING); CellUtil.setCellValue(cell, "合计:"); if (detail.length>1){ for (int i = 1; i < detail.length; i++) { cell = row.createCell(i); cell.setCellStyle(style2); cell.setCellType(HSSFCell.CELL_TYPE_STRING); String colString = CellReference.convertNumToColString(i); //长度转成ABC列 String sumstring = "SUM(" + colString + "1:" + colString + dataList.size() + ")";//求和公式 sheet.getRow(dataList.size()+3).getCell(i).setCellFormula(sumstring); } } } sheet.setForceFormulaRecalculation(true); String fileName = new String(sheetName.getBytes("gb2312"), "ISO8859-1"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); response.setContentType("application/x-download;charset=utf-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); OutputStream os = response.getOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); byte[] b = new byte[1024]; while ((bais.read(b)) > 0) { os.write(b); } bais.close(); os.flush(); os.close(); } }
package org.jeecg.common.util; import java.math.BigDecimal; import java.sql.Date; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.regex.Pattern; import org.apache.poi.hssf.usermodel.HSSFCell; public class CellUtil { public static String returnCellValue(HSSFCell cell){ String cellvalue = ""; if (null != cell) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // 数字 return String.valueOf(cell.getNumericCellValue()).trim(); case HSSFCell.CELL_TYPE_STRING: // 字符串 return String.valueOf(cell.getStringCellValue()).trim(); case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean return String.valueOf(cell.getBooleanCellValue()).trim(); case HSSFCell.CELL_TYPE_FORMULA: // 公式 return String.valueOf(cell.getCellFormula()).trim(); case HSSFCell.CELL_TYPE_BLANK: // 空值 return ""; case HSSFCell.CELL_TYPE_ERROR: // 故障 return ""; default: return ""; } } else { } return cellvalue; } //避免cell.setCellValue(checkOrderQmSave.getSellOrderNo())中参数为空就会报错 public static void setCellValue(HSSFCell cell, Object object){ if(object == null){ cell.setCellValue(""); }else{ if (object instanceof String) { cell.setCellValue(String.valueOf(object)); }else if(object instanceof Long){ Long temp = (Long)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Double){ Double temp = (Double)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Float){ Float temp = (Float)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Integer){ Integer temp = (Integer)object; cell.setCellValue(temp.intValue()); }else if(object instanceof BigDecimal){ BigDecimal temp = (BigDecimal)object; String cellvalue = new DecimalFormat("#").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else{ cell.setCellValue(""); } } } public static void setCellValue(HSSFCell cell, Object object, String model){ if(object == null){ cell.setCellValue(""); }else{ if (object instanceof String) { cell.setCellValue(String.valueOf(object)); }else if(object instanceof Long){ Long temp = (Long)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Double){ Double temp = (Double)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Float){ Float temp = (Float)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof Integer){ Integer temp = (Integer)object; cell.setCellValue(temp.intValue()); }else if(object instanceof BigDecimal){ BigDecimal temp = (BigDecimal)object; String cellvalue = new DecimalFormat("#0.00").format(temp.doubleValue()); cell.setCellValue(cellvalue); }else if(object instanceof java.sql.Date){ cell.setCellValue(new SimpleDateFormat(model).format(object)); }else if(object instanceof java.util.Date){ cell.setCellValue(new SimpleDateFormat(model).format(object)); }else{ cell.setCellValue(""); } } } public static void setCellValue(HSSFCell cell, String object){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(object); } } public static void setCellValue(HSSFCell cell, Long object){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(object.doubleValue()); } } public static void setCellValue(HSSFCell cell, Double object){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(object.doubleValue()); } } public static void setCellValue(HSSFCell cell, double object){ cell.setCellValue(object); } public static void setCellValue(HSSFCell cell, Date object, String model){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(new SimpleDateFormat(model).format(object)); } } public static void setCellValue(HSSFCell cell, java.util.Date object, String model){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(new SimpleDateFormat(model).format(object)); } } public static void setCellValue(HSSFCell cell, BigDecimal object){ if(object == null){ cell.setCellValue(""); }else{ cell.setCellValue(object.toString()); } } //判断EXCEL表格高度用 row.setHeight((short) CellUtil.getExcelCellAutoHeight(TAR_VAL_ALL_STRING, 280, 30)); public static float getExcelCellAutoHeight(String str,float defaultRowHeight, int fontCountInline) { int defaultCount = 0; for (int i = 0; i < str.length(); i++) { int ff = getregex(str.substring(i, i + 1)); defaultCount = defaultCount + ff; } if (defaultCount > fontCountInline){ return ((int) (defaultCount / fontCountInline) + 1) * defaultRowHeight;//计算 } else { return defaultRowHeight; } } public static int getregex(String charStr) { if("".equals(charStr) || charStr == null){ return 1; } // 判断是否为字母或字符 if (Pattern.compile("^[A-Za-z0-9]+$").matcher(charStr).matches()) { return 1; } // 判断是否为全角 if (Pattern.compile("[\u4e00-\u9fa5]+$").matcher(charStr).matches()) { return 2; } //全角符号 及中文 if (Pattern.compile("[^x00-xff]").matcher(charStr).matches()) { return 2; } return 1; } }
标签:++ 标题 app cto color str while mat you
原文地址:https://www.cnblogs.com/xueyicanfei/p/12868960.html