package com.chinaztt.mes.technology.util;
|
|
import com.chinaztt.mes.technology.dto.DocumentMaterialCostDTO;
|
import com.chinaztt.mes.technology.entity.RoutingOperationParam;
|
import lombok.SneakyThrows;
|
import org.apache.poi.xwpf.usermodel.*;
|
import org.apache.xmlbeans.XmlCursor;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.InputStream;
|
import java.math.BigInteger;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 工艺文件导出工具类
|
* @author zhangxy
|
*/
|
public class DocumentWordExportUtils {
|
|
private static final int BYTE_LEN = 100;
|
|
|
/**
|
* @param document 被写入的文件
|
* @param style word 模板的文字样式 从左往右 对应 从0开始
|
* @param context 要写入的文字
|
* @param bold 是否加粗
|
* @param font 字体
|
*/
|
public static void createText(XWPFDocument document, String style, String context, boolean bold, String font) {
|
XWPFParagraph firstParagraph = document.createParagraph();
|
firstParagraph.setAlignment(ParagraphAlignment.LEFT);
|
if (style != null) {
|
firstParagraph.setStyle(style);
|
}
|
XWPFRun run = firstParagraph.createRun();
|
|
run.setText(context);
|
run.setBold(bold);
|
run.setFontFamily(font);
|
// run.addBreak();
|
}
|
|
|
|
/**
|
* @param document
|
* @param stringListMap map <规格 , 当前工序的参数的list>
|
*/
|
public static void createParaTable(XWPFDocument document, Map<String, List<RoutingOperationParam>> stringListMap) {
|
XWPFParagraph paragraph1 = document.createParagraph();
|
XWPFRun paragraphRun1 = paragraph1.createRun();
|
paragraphRun1.setText("\r");
|
|
XWPFTable comTable = document.createTable();
|
CTTblWidth comTableWidth = comTable.getCTTbl().addNewTblPr().addNewTblW();
|
comTableWidth.setType(STTblWidth.DXA);
|
comTableWidth.setW(BigInteger.valueOf(9072));
|
|
|
//获取总列的数量
|
int colTotal = stringListMap.keySet().size() * 2;
|
XWPFTableRow infoTableRowOne = comTable.getRow(0);
|
//获取总行数
|
int rowTotal = 0;
|
for (String key : stringListMap.keySet()) {
|
if (stringListMap.get(key).size() > rowTotal) {
|
rowTotal = stringListMap.get(key).size();
|
}
|
}
|
rowTotal += 1;
|
//画表格 先画第一行
|
infoTableRowOne.getCell(0).setText("");
|
for (int i = 1; i < colTotal; i++) {
|
infoTableRowOne.addNewTableCell().setText("");
|
}
|
//再画第一列
|
for (int i = 1; i < rowTotal; i++) {
|
XWPFTableRow infoTableRow = comTable.createRow();
|
infoTableRow.getCell(0).setText("");
|
}
|
//别的列和行
|
for (int i = 1; i < colTotal; i++) {
|
for (int j = 1; j < rowTotal; j++) {
|
XWPFTableRow infoTableRow = comTable.getRow(j);
|
infoTableRow.getCell(i).setText("");
|
}
|
}
|
//当前行
|
int col = 0;
|
for (String key : stringListMap.keySet()) {
|
infoTableRowOne.getCell(col).setText(key);
|
infoTableRowOne.getCell(col + 1).setText("");
|
List<RoutingOperationParam> processDocMaterialPageList = stringListMap.get(key);
|
for (int j = 0; j < processDocMaterialPageList.size(); j++) {
|
XWPFTableRow infoTableRow = comTable.getRow(j + 1);
|
infoTableRow.getCell(col).setText(processDocMaterialPageList.get(j).getParameterItem());
|
infoTableRow.getCell(col + 1).setText(processDocMaterialPageList.get(j).getParamValue());
|
}
|
col += 2;
|
}
|
}
|
|
|
/**
|
* @param document 被写入数据的文件
|
* @param map 需要写入的数据 MAP形式
|
* @param rows 每一行多少个
|
*/
|
public static void createMeterialTable(XWPFDocument document, Map<String, List<DocumentMaterialCostDTO>> map, int rows) {
|
//这边为固定格式 不要动
|
XWPFParagraph paragraph1 = document.createParagraph();
|
XWPFRun paragraphRun1 = paragraph1.createRun();
|
paragraphRun1.setText("\r");
|
|
XWPFTable comTable = document.createTable();
|
CTTblWidth comTableWidth = comTable.getCTTbl().addNewTblPr().addNewTblW();
|
comTableWidth.setType(STTblWidth.DXA);
|
comTableWidth.setW(BigInteger.valueOf(9072));
|
//只有一张表的时候
|
if (map.keySet().size() == 1) {
|
for (String key : map.keySet()) {
|
//这边开始处理数据
|
List<DocumentMaterialCostDTO> list = map.get(key);
|
//总数量 除以 行数 去除余数 先把整行数的 数据加进去 ,
|
int total = (list.size() / rows);
|
//获取第一行
|
XWPFTableRow infoTableRowOne = comTable.getRow(0);
|
//第一行第一格里面写入物料的规格
|
infoTableRowOne.getCell(0).setText(key);
|
//for 循环 卡 空格位置 相当与设置列
|
for (int x = 1; x < rows; x++) {
|
infoTableRowOne.addNewTableCell().setText("");
|
}
|
//第二行往后
|
for (int i = 0; i < total; i++) {
|
//处理物料名称行
|
XWPFTableRow infoTableRowTwo = comTable.createRow();
|
for (int y = 0; y < rows; y++) {
|
infoTableRowTwo.getCell(y).setText(list.get(i * rows + y).getPartName());
|
}
|
//处理物料数量行
|
XWPFTableRow infoTableRowThree = comTable.createRow();
|
for (int z = 0; z < rows; z++) {
|
infoTableRowThree.getCell(z).setText(list.get(i * rows + z).getQuantity() + "");
|
}
|
}
|
//这边开始处理取整后余下来的几条数据,最后会增加两行
|
if (list.size() - total * rows != 0) {
|
XWPFTableRow infoTableRowTwo = comTable.createRow();
|
XWPFTableRow infoTableRowThree = comTable.createRow();
|
for (int i = 0; i < list.size() - total * rows; i++) {
|
infoTableRowTwo.getCell(i).setText(list.get(total * rows + i).getPartName());
|
infoTableRowThree.getCell(i).setText(list.get(total * rows + i).getQuantity() + "");
|
}
|
}
|
}
|
} else {
|
//获取总列的数量
|
int colTotal = map.keySet().size() * 2;
|
XWPFTableRow infoTableRowOne = comTable.getRow(0);
|
//获取总行数
|
int rowTotal = 0;
|
for (String key : map.keySet()) {
|
if (map.get(key).size() > rowTotal) {
|
rowTotal = map.get(key).size();
|
}
|
}
|
rowTotal += 1;
|
//画表格 先画第一行
|
infoTableRowOne.getCell(0).setText("");
|
for (int i = 1; i < colTotal; i++) {
|
infoTableRowOne.addNewTableCell().setText("");
|
}
|
//再画第一列
|
for (int i = 1; i < rowTotal; i++) {
|
XWPFTableRow infoTableRow = comTable.createRow();
|
infoTableRow.getCell(0).setText("");
|
}
|
//别的列和行
|
for (int i = 1; i < colTotal; i++) {
|
for (int j = 1; j < rowTotal; j++) {
|
XWPFTableRow infoTableRow = comTable.getRow(j);
|
infoTableRow.getCell(i).setText("");
|
}
|
}
|
//当前行
|
int col = 0;
|
for (String key : map.keySet()) {
|
infoTableRowOne.getCell(col).setText(key);
|
infoTableRowOne.getCell(col + 1).setText("");
|
List<DocumentMaterialCostDTO> processDocMaterialPageList = map.get(key);
|
for (int j = 0; j < processDocMaterialPageList.size(); j++) {
|
XWPFTableRow infoTableRow = comTable.getRow(j + 1);
|
infoTableRow.getCell(col).setText(processDocMaterialPageList.get(j).getPartName());
|
infoTableRow.getCell(col + 1).setText(processDocMaterialPageList.get(j).getQuantity() + "");
|
}
|
col += 2;
|
}
|
}
|
}
|
|
|
public static XWPFParagraph newParagraph(XWPFParagraph paragraph) {
|
XmlCursor xmlCursor = paragraph.getCTP().newCursor();
|
xmlCursor.toNextSibling();
|
paragraph = paragraph.getDocument().insertNewParagraph(xmlCursor);
|
return paragraph;
|
}
|
|
/**
|
* @param str 被处理的字符串
|
* @param firstChar 识别的第一个字符
|
* @param secondChar 识别的第二个字符
|
* @param times 出现的次数
|
* @return
|
*/
|
public static String returnStation(String str, String firstChar, String secondChar, int times) {
|
String result = "";
|
try {
|
Pattern pattern1 = Pattern.compile(firstChar);
|
Matcher findMatcher1 = pattern1.matcher(str);
|
Pattern pattern2 = Pattern.compile(secondChar);
|
Matcher findMatcher2 = pattern2.matcher(str);
|
int number = 0;
|
int station1 = 0;
|
int station2 = 0;
|
while (findMatcher1.find()) {
|
number++;
|
if (number == times) {
|
break;
|
}
|
}
|
station1 = findMatcher1.start();
|
number = 0;
|
while (findMatcher2.find()) {
|
number++;
|
if (number == times) {
|
break;
|
}
|
}
|
station2 = findMatcher2.start();
|
result = str.substring(station1 + 1, station2);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
}
|
return result;
|
}
|
|
|
public static String[] returnStation(String str, String firstChar) {
|
String[] sourceStrArray = str.split(firstChar);
|
return sourceStrArray;
|
}
|
|
@SneakyThrows
|
public static final byte[] input2byte(InputStream inStream) {
|
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
byte[] buff = new byte[BYTE_LEN];
|
int rc = 0;
|
while ((rc = inStream.read(buff, 0, BYTE_LEN)) > 0) {
|
swapStream.write(buff, 0, rc);
|
}
|
byte[] in2b = swapStream.toByteArray();
|
return in2b;
|
}
|
|
|
}
|