| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.minpay.common.util;
- import org.apache.poi.xssf.usermodel.*;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- *
- */
- public class ExportUtils {
- public final static String COLUMNS_SEPARATOR = "#";//分割符
- /**
- * 描述列样式
- * @param wb
- * @return
- */
- public static XSSFCellStyle getDescCellStyle(XSSFWorkbook wb){
- // 生成一个样式
- XSSFCellStyle style = wb.createCellStyle();
- // 设置这些样式
- style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
- style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
- style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
- style.setBorderRight(XSSFCellStyle.BORDER_THIN);
- style.setBorderTop(XSSFCellStyle.BORDER_THIN);
- style.setAlignment(XSSFCellStyle.ALIGN_LEFT);
- // 生成一个字体
- XSSFFont font = wb.createFont();
- font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋体");
- font.setColor(new XSSFColor(java.awt.Color.RED));
- font.setFontHeightInPoints((short) 12);
- // 把字体应用到当前的样式
- style.setFont(font);
- return style;
- }
- /**
- * 标题样式
- * @param wb
- * @return
- */
- public static XSSFCellStyle getTitleCellStyle(XSSFWorkbook wb){
- // 生成一个样式
- XSSFCellStyle style = wb.createCellStyle();
- // 设置这些样式
- style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray));
- style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
- style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
- style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
- style.setBorderRight(XSSFCellStyle.BORDER_THIN);
- style.setBorderTop(XSSFCellStyle.BORDER_THIN);
- style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
- // 生成一个字体
- XSSFFont font = wb.createFont();
- font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋体");
- font.setColor(new XSSFColor(java.awt.Color.BLACK));
- font.setFontHeightInPoints((short) 11);
- // 把字体应用到当前的样式
- style.setFont(font);
- return style;
- }
- /**
- * 正文样式
- * @param wb
- * @return
- */
- public static XSSFCellStyle getTextCellStyle(XSSFWorkbook wb) {
- // 生成并设置另一个样式
- XSSFCellStyle style2 = wb.createCellStyle();
- style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));
- style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
- style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
- style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
- style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
- style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
- style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
- style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
- // 生成另一个字体
- XSSFFont font2 = wb.createFont();
- font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
- // 把字体应用到当前的样式
- style2.setFont(font2);
- return style2;
- }
- public static String[] autocomplete(String[] titleArray, int manyTitle) {
- for(int i = 0; i< titleArray.length; i++) {
- titleArray[i] = completeStr(titleArray[i], manyTitle - titleArray[i].split(COLUMNS_SEPARATOR).length);
- }
- return titleArray;
- }
- private static String completeStr(String col, int count) {
- if (count == 0) {
- return col;
- }
- col = col.split(COLUMNS_SEPARATOR)[0] + COLUMNS_SEPARATOR + col;
- return completeStr(col, --count);
- }
- /**
- * 表头数据合并
- * @param sheet
- * @param coltitleIndex 列数
- * @param colTitles 标题行
- * @param num 行索引
- */
- public static void titleMerge(XSSFSheet sheet, int coltitleIndex, String[] colTitles, int num) {
- for(int i = 0; i < num; i++) {
- String preVal = "";
- int columnIndex = 0;
- for(int j = 0; j < colTitles.length; j++) {
- columnIndex++;
- preVal = colTitles[j].split(COLUMNS_SEPARATOR)[i];
- String columnVal = colTitles[j + 1].split(COLUMNS_SEPARATOR)[i];
- if(!"".equals(columnVal) && !preVal.equals(columnVal)) {
- List<Integer> list = new ArrayList<Integer>();
- for(int m = j - columnIndex + 1; m <= j; m++) {
- int bottom = 0;
- for(int k = i + 1; k <= num; k++) {
- String nextRowVal = "";
- try {
- nextRowVal = colTitles[m].split(COLUMNS_SEPARATOR)[k];
- } catch (Exception e) {
- list.add(bottom);
- }
- if(!"".equals(nextRowVal) && !preVal.equals(nextRowVal)) {
- list.add(bottom);
- break;
- }
- bottom++;
- }
- }
- Collections.sort(list); //从小到大
- columnIndex = 0;
- }
- preVal = columnVal;
- }
- }
- }
- }
|