一、工具类如下
package com.enter.net.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @Author: zhangshizhe
*
* @Date: 2020年05月30日 9:00:33
*
* @Description: 使用模板导出工具类
*
* @Version: 1.0
*
*/
public class ExportFreemakerUtil {
//注:如果使用表格宽度的话,每次添加新表格时需要重新new对象
private List<Integer> table_widths = new ArrayList<>();//当前表格宽度数组
private static final int SUM_TABLE_WIDTH = 9326;//表格默认总宽度
/**
* 自动计算列宽
* @param num
*/
public ExportFreemakerUtil(Integer num) {
for (int i = 0; i < num; i++) {
table_widths.add(SUM_TABLE_WIDTH / num);
}
}
/**
* 自动计算剩余列宽
* @param num 共几列
* @param widths 需要固定的宽度(key:列数,value:宽度)
*/
public ExportFreemakerUtil(Integer num, Map<Integer,Integer> widths) {
Integer sum = 0;
for (Integer column : widths.keySet()) {
sum += widths.get(column);
}
Integer average = (SUM_TABLE_WIDTH - sum) / num;
for (int i = 0; i < num; i++) {
Integer integer = widths.get(i);
if (integer == null) {//添加平均数
table_widths.add(average);
}else {//添加手动的
table_widths.add(integer);
}
}
}
/**
* 没有宽度,加载段落使用,不能加载表格
*/
public ExportFreemakerUtil() {
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取表格的开始(需手动传宽度)
* @Fcunction getTableStart
* @return String
*
*/
public String getTableStart(){
//表格固定开始
String str =
" <w:tbl>\n" +
" <w:tblPr>\n" +
" <w:tblW w:w=\"9378\" w:type=\"dxa\"/>\n" +
" <w:jc w:val=\"center\"/>\n" +
" <w:tblInd w:w=\"-145\" w:type=\"dxa\"/>\n" +
" <w:tblBorders>\n" +
" <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:bottom w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:insideH w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:insideV w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" </w:tblBorders>\n" +
" <w:tblLayout w:type=\"Fixed\"/>\n" +
" <w:tblCellMar>\n" +
" <w:left w:w=\"0\" w:type=\"dxa\"/>\n" +
" <w:right w:w=\"0\" w:type=\"dxa\"/>\n" +
" </w:tblCellMar>\n" +
" </w:tblPr>";
//表格宽度设置
str += " <w:tblGrid>";
for (Integer width : table_widths) {
str += " <w:gridCol w:w=\"" + width + "\"/>";
}
str += " </w:tblGrid>";
return str;
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取表格的结束
* @Fcunction getTableEnd
* @return String
*
*/
public String getTableEnd(){
//表格固定结束
String str =" </w:tbl>";
return str;
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取表格的每一行的开始
* @Fcunction getTableRowStart
* @return String
*
*/
public String getTableRowStart(){
return " <w:tr wsp:rsidR=\"0044249F\" wsp:rsidRPr=\"00CC26F0\" wsp:rsidTr=\"00221871\">\n" +
" <w:tblPrEx>\n" +
" <w:tblCellMar>\n" +
" <w:top w:w=\"0\" w:type=\"dxa\"/>\n" +
" <w:bottom w:w=\"0\" w:type=\"dxa\"/>\n" +
" </w:tblCellMar>\n" +
" </w:tblPrEx>\n" +
" <w:trPr>\n" +
" <w:cantSplit/>\n" +
" <w:trHeight w:val=\"421\"/>\n" +
" <w:jc w:val=\"center\"/>\n" +
" </w:trPr>";
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取表格的每一行的结束
* @Fcunction getTableRowEnd
* @return String
*
*/
public String getTableRowEnd(){
return " </w:tr>";
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取每一列
* @Fcunction getColumn
* @param str
* @param count 第几列
* @return String
*
*/
public String getColumn(String str,Integer count){
return " <w:tc>\n" +
" <w:tcPr>\n" +
" <w:tcW w:w=\"" + table_widths.get(count) + "\" w:type=\"dxa\"/>\n" +
" <w:tcBorders>\n" +
" <w:top w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:bottom w:val=\"single\" w:sz=\"5\" wx:bdrwidth=\"15\" w:space=\"0\" w:color=\"000000\"/>\n" +
" <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" </w:tcBorders>\n" +
" <w:vAlign w:val=\"center\"/>\n" +
" </w:tcPr>\n" +
" <w:p wsp:rsidR=\"0044249F\" wsp:rsidRPr=\"00CC26F0\" wsp:rsidRDefault=\"0044249F\" wsp:rsidP=\"00465E49\">\n" +
" <w:pPr>\n" +
" <w:widowControl/>\n" +
" <w:spacing w:line=\"300\" w:line-rule=\"exact\"/>\n" +
" <w:jc w:val=\"center\"/>\n" +
" <w:rPr>\n" +
" <w:kern w:val=\"0\"/>\n" +
" <w:sz-cs w:val=\"21\"/>\n" +
" </w:rPr>\n" +
" </w:pPr>\n" +
" <w:r wsp:rsidRPr=\"00CC26F0\">\n" +
" <w:rPr>\n" +
" <wx:font wx:val=\"宋体\"/>\n" +
" <w:kern w:val=\"0\"/>\n" +
" <w:sz-cs w:val=\"21\"/>\n" +
" </w:rPr>\n" +
" <w:t>" + str + "</w:t>\n" +
" </w:r>\n" +
" </w:p>\n" +
" </w:tc>";
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取列合并的列
* @Fcunction getMergeColumn
* @param str 文字
* @param num 合并几列
* @param count 第几列
* @return String
*
*/
public String getMergeColumn(String str,Integer num,Integer count){
Integer sum = 0;
for (int i = count; i < count + num; i++) {
if (i < table_widths.size()) {
sum += table_widths.get(i);
}
}
return " <w:tc>\n" +
" <w:tcPr>\n" +
" <w:tcW w:w=\"" + sum + "\" w:type=\"dxa\"/>\n" +
" <w:gridSpan w:val=\"" + num + "\"/>\n" +
" <w:tcBorders>\n" +
" <w:top w:val=\"single\" w:sz=\"5\" wx:bdrwidth=\"15\" w:space=\"0\" w:color=\"000000\"/>\n" +
" <w:left w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" <w:bottom w:val=\"single\" w:sz=\"5\" wx:bdrwidth=\"15\" w:space=\"0\" w:color=\"000000\"/>\n" +
" <w:right w:val=\"single\" w:sz=\"4\" wx:bdrwidth=\"10\" w:space=\"0\" w:color=\"auto\"/>\n" +
" </w:tcBorders>\n" +
" <w:vAlign w:val=\"center\"/>\n" +
" </w:tcPr>\n" +
" <w:p wsp:rsidR=\"0044249F\" wsp:rsidRPr=\"00CC26F0\" wsp:rsidRDefault=\"0044249F\" wsp:rsidP=\"00865525\">\n" +
" <w:pPr>\n" +
" <w:jc w:val=\"center\"/>\n" +
" </w:pPr>\n" +
" <w:r wsp:rsidRPr=\"00CC26F0\">\n" +
" <w:rPr>\n" +
" <wx:font wx:val=\"宋体\"/>\n" +
" <w:kern w:val=\"0\"/>\n" +
" <w:sz-cs w:val=\"21\"/>\n" +
" </w:rPr>\n" +
" <w:t>" + str + "</w:t>\n" +
" </w:r>\n" +
" </w:p>\n" +
" </w:tc>";
}
/**
*
* @Author zhangshizhe
* @Date 2020年05月30日 9:06:03
* @Description 获取行合并的列(合并起始列)
* @Fcunction getMergeRowColumnStart
* @param str
* @param count 第几列
* @return String
*
*/
public String getMergeRowColumnStart(String str,Integer count){
return " <w:tc>\n" +
" <w:tcPr>\n" +
" <w:tcW w:w=\"" + table_widths.get(count) + "\" w:type=\"dxa\"/>\n" +
" <w:vmerge w:val=\"restart\"/>\n" +
" <w:tcBorders>\n" +
" <w:top w:val=\"single\" w:sz=\"5\" wx:bdrwidth=\"15\" w:space=\"0\" w:color=\"000000\"/>\n" +
" <w:left w:val=\"single\" w:sz=\"5\" wx:bdrwidth=\"15\" w:space=\"0\" w:color=\"000000\"/>\n