@RequestMapping(value = "/exportXls") public void exportXls(@RequestParam String zcq,@RequestParam String ksq,HttpServletRequest request, HttpServletResponse response) throws IOException { Map<String, Object> conditionMap = new HashMap<String, Object>(); List<Map<String, Object>> grjjtdwbktjzjList = kqtjbbService.getgrjjtdwbktjzjList(conditionMap); String filename = "個人及集體單位報考統計總計"; String[] head0 = {"模塊一","","","","模塊二","","",""}; String[] head1 = {"區縣代碼","區縣名稱","集體單位代碼","集體單位名稱","集體報考人數","現場未確認人數","集體報考科次","已繳費科次"}; String[] headnum0 = new String[] { "1,1,0,3", "1,1,4,7"};//對應excel中的行和列,下表從0開始{"開始行,結束行,開始列,結束列"} String[] headnum1 = new String[] { "2,2,0,0", "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"}; String[] colName = {"QXDM","QXMC","JTBKDWDM","JTBKDWMC","JTBKRSCNT","XCWQRCNT","JTBKKCCNT","YJFKCCNT"}; int[] mergeIndex = {0,1}; List<Map<String, Object>> headList = new ArrayList(); Map<String, Object> headMap0 = new HashMap<String, Object>(); headMap0.put("head", head0); headMap0.put("headnum", headnum0); headList.add(headMap0); Map<String, Object> headMap1 = new HashMap<String, Object>(); headMap1.put("head", head1); headMap1.put("headnum", headnum1); headList.add(headMap1); try { ExcelUtil.reportMergeXls(request, response, grjjtdwbktjzjList, filename, headList, colName, mergeIndex, false);//utils類需要用到的參數 } catch (Exception e) { e.printStackTrace(); } }
工具類:
package org.jeecg.common.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; 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 javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.api.client.util.Lists; 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.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; 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) { 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 = entry.getValue().toString(); } else { val = " "; } HSSFCell cell = row.createCell(i); i++; cell.setCellValue(val); logger.info("第" + (j + 3) + "行,第" + val + "--賦值成功"); } } } // 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 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; } /** * 導出多行表頭 合並行 * @param request * @param response * @param dataList 數據集合 * @param sheetName 標題行 * @param headList 副標題集合 (head:副標題內容,headnum:合並位置{"開始行,結束行,開始列,結束列"}) * @param detail 表字段 * @param mergeIndex 合並的列 * @param isHeJi 合計行 * @throws Exception */ public static void reportMergeXls(HttpServletRequest request, HttpServletResponse response, List<Map<String, Object>> dataList, String sheetName, List<Map<String, Object>> headList, String[] detail, int[] mergeIndex, 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); // 第一行表頭標題 String[] head0 = (String[]) headList.get(0).get("head"); 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); int rowIndex = 1;//副標題第一行開始 for (int k = 0; k < headList.size(); k++) { String[] head = (String[]) headList.get(k).get("head"); String[] headnum = (String[]) headList.get(k).get("headnum"); row = sheet.createRow(rowIndex); if (k == 0) { for (int i = 0; i < head.length; i++) { cell = row.createCell(i); cell.setCellValue(head[i]); cell.setCellStyle(style1); } if (headnum != null){ for (int i = 0; i < headnum.length; i++) { String[] temp = headnum[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)); } } } else { String[] head1 = (String[]) headList.get(k - 1).get("head"); for (int i = 0; i < head1.length; i++) { cell = row.createCell(i); cell.setCellStyle(style1); if (i > 1) { for (int j = 0; j < head.length; j++) { cell = row.createCell(j); cell.setCellValue(head[j]); cell.setCellStyle(style1); } } } if (headnum != null){ for (int i = 0; i < headnum.length; i++) { String[] temp = headnum[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)); } } } rowIndex++; } // 1.3、普通單元格樣式(中文)樣式 HSSFCellStyle style2 = 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); if (mergeIndex == null) { for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow(i + rowIndex);//標題、時間、表頭字段共占了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); } } } else { /*遍歷該數據集合*/ int index = rowIndex; List<PoiModel> poiModels = Lists.newArrayList(); if (null != workbook) { Iterator iterator = dataList.iterator(); while (iterator.hasNext()) { row = sheet.createRow(index); /*取得當前這行的map,該map中以key,value的形式存着這一行值*/ Map<String, String> map = (Map<String, String>) iterator.next(); /*循環列數,給當前行塞值*/ for (int i = 0; i < detail.length; i++) { String old = ""; /*old存的是上一行統一位置的單元的值,第一行是最上一行了,所以從第二行開始記*/ if (index > rowIndex) { old = poiModels.get(i) == null ? "" : poiModels.get(i).getContent(); } /*循環需要合並的列*/ for (int j = 0; j < mergeIndex.length; j++) { if (index == rowIndex) { /*記錄第一行的開始行和開始列*/ PoiModel poiModel = new PoiModel(); poiModel.setOldContent(String.valueOf(map.get(detail[i]))); poiModel.setContent(String.valueOf(map.get(detail[i]))); poiModel.setRowIndex(rowIndex); poiModel.setCellIndex(i); poiModels.add(poiModel); break; } else if (i > 0 && mergeIndex[j] == i) {/*這邊i>0也是因為第一列已經是最前一列了,只能從第二列開始*/ /*當前同一列的內容與上一行同一列不同時,把那以上的合並, 或者在當前元素一樣的情況下,前一列的元素並不一樣,這種情況也合並*/ /*如果不需要考慮當前行與上一行內容相同,但是它們的前一列內容不一樣則不合並的情況,把下面條件中||poiModels.get(i).getContent().equals(String.valueOf(map.get(title[i]))) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i-1]))去掉就行*/ if (!poiModels.get(i).getContent().equals(String.valueOf(map.get(detail[i]))) || poiModels.get(i).getContent().equals(String.valueOf(map.get(detail[i]))) && !poiModels.get(i - 1).getOldContent().equals(map.get(detail[i - 1]))) { /*當前行的當前列與上一行的當前列的內容不一致時,則把當前行以上的合並*/ CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/); //在sheet里增加合並單元格 sheet.addMergedRegion(cra); /*重新記錄該列的內容為當前內容,行標記改為當前行標記,列標記則為當前列*/ poiModels.get(i).setContent(String.valueOf(map.get(detail[i]))); poiModels.get(i).setRowIndex(index); poiModels.get(i).setCellIndex(i); } } /*處理第一列的情況*/ if (mergeIndex[j] == i && i == 0 && !poiModels.get(i).getContent().equals(String.valueOf(map.get(detail[i])))) { /*當前行的當前列與上一行的當前列的內容不一致時,則把當前行以上的合並*/ CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index - 1/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/); //在sheet里增加合並單元格 sheet.addMergedRegion(cra); /*重新記錄該列的內容為當前內容,行標記改為當前行標記*/ poiModels.get(i).setContent(String.valueOf(map.get(detail[i]))); poiModels.get(i).setRowIndex(index); poiModels.get(i).setCellIndex(i); } /*最后一行沒有后續的行與之比較,所有當到最后一行時則直接合並對應列的相同內容 ,index初始值不是1 ,需減去等於1*/ if (mergeIndex[j] == i && index == dataList.size()) { CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex()/*從第二行開始*/, index/*到第幾行*/, poiModels.get(i).getCellIndex()/*從某一列開始*/, poiModels.get(i).getCellIndex()/*到第幾列*/); //在sheet里增加合並單元格 sheet.addMergedRegion(cra); } } cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellValue(String.valueOf(map.get(detail[i]))); cell.setCellStyle(style2); cell.setCellType(HSSFCell.CELL_TYPE_STRING); /*在每一個單元格處理完成后,把這個單元格內容設置為old內容*/ poiModels.get(i).setOldContent(old); } index++; } } } 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; public class PoiModel { private String content; private String oldContent; private int rowIndex; private int cellIndex; public String getOldContent() { return oldContent; } public void setOldContent(String oldContent) { this.oldContent = oldContent; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getRowIndex() { return rowIndex; } public void setRowIndex(int rowIndex) { this.rowIndex = rowIndex; } public int getCellIndex() { return cellIndex; } public void setCellIndex(int cellIndex) { this.cellIndex = cellIndex; } }
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; } }