package com.whyc.service;
|
|
import com.whyc.dto.paramter.POIWord;
|
import com.whyc.dto.paramter.TestDataInfDoc;
|
import com.whyc.mapper.BattInfMapper;
|
import com.whyc.pojo.BatttestdataId;
|
import com.whyc.util.ActionUtil;
|
import org.apache.poi.xwpf.usermodel.*;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.OutputStream;
|
import java.math.BigInteger;
|
import java.net.URLEncoder;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
public class PoIWordExportService {
|
@Autowired(required = false)
|
private BattInfMapper binfMapper;
|
@Autowired
|
private BatttestdataService bdataService;
|
|
//word导出
|
public void export(HttpServletRequest req, HttpServletResponse res) {
|
String poiJson = req.getParameter("json");
|
POIWord poiWord = ActionUtil.getGson().fromJson(poiJson, POIWord.class);
|
List<TestDataInfDoc> list = bdataService.searchWordExport(poiWord);
|
String date = ActionUtil.sdfwithtime_yyyyMMdd_HH_mm_ss.format(new Date());
|
String fileName = "doc_" + date + "文件";
|
XWPFDocument doc = new XWPFDocument();
|
//标题
|
createText(doc, "蓄电池放电数据报表", ParagraphAlignment.CENTER, 20);
|
//使用单位
|
createText(doc, "使用单位:" + poiWord.getUserName(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "产品名称:" + poiWord.getProdcutName(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "测试单位:" + poiWord.getTestDepart(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "测试人员:" + poiWord.getTestPeople(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "测试日期:" + poiWord.getTestDate(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "打印日期:" + poiWord.getPrintDate(), ParagraphAlignment.LEFT, 15);
|
createText(doc, "测试站点:" + poiWord.getTestStationName(), ParagraphAlignment.LEFT, 15);
|
//电池参数
|
createText(doc, "电池参数:", ParagraphAlignment.LEFT, 15);
|
createParam(doc, poiWord);
|
//全部数据
|
createText(doc, "全部数据:", ParagraphAlignment.LEFT, 15);
|
createData(doc, list, poiWord.getMonCount());
|
//结论表
|
createText(doc, "结论表:", ParagraphAlignment.LEFT, 15);
|
createResult(doc, poiWord);
|
try {
|
res.setCharacterEncoding("utf-8");
|
res.setContentType("application/msword");
|
res.setHeader("Access-Control-Expose-Headers", "Content-disposition");
|
res.setHeader("Content-disposition", "attachment; filename=" +
|
URLEncoder.encode(fileName.concat(".doc"), "UTF-8"));
|
OutputStream out = res.getOutputStream();
|
doc.write(out);
|
out.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
/* try {
|
String path = "D:\\test.doc";
|
OutputStream os = new FileOutputStream(path);
|
doc.write(os);
|
if(os!=null){
|
try{
|
os.close();
|
System.out.println("文件已输出!");
|
}
|
catch(IOException e){
|
e.printStackTrace();
|
}
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}*/
|
|
}
|
|
//结论表
|
private void createResult(XWPFDocument doc, POIWord poiWord) {
|
//添加表格
|
XWPFTable table = doc.createTable(3, 4);
|
table.setCellMargins(3, 5, 3, 5);
|
XWPFTableRow row;
|
XWPFTableCell cell;
|
XWPFParagraph para;
|
XWPFRun run;
|
CTTcPr cellPr;
|
Map<String, Map<String, String>> map = new HashMap<>();
|
//第一行数据
|
Map<String, String> rowMap1 = new HashMap<>();
|
rowMap1.put("l1", "蓄电池初充电、放电、容量试验记录是否合格");
|
rowMap1.put("l2", "蓄电池初充电、放电、容量试验记录是否合格");
|
rowMap1.put("l3", "蓄电池初充电、放电、容量试验记录是否合格");
|
rowMap1.put("l4", "" + poiWord.getTestIsGood());
|
map.put("r1", rowMap1);
|
//第二行数据
|
Map<String, String> rowMap2 = new HashMap<>();
|
rowMap2.put("l1", "负责人");
|
rowMap2.put("l2", "" + poiWord.getHeadName());
|
rowMap2.put("l3", "成员");
|
rowMap2.put("l4", "" + poiWord.getGroupPeoples());
|
map.put("r2", rowMap2);
|
//第三行数据
|
Map<String, String> rowMap3 = new HashMap<>();
|
rowMap3.put("l1", "备注");
|
rowMap3.put("l2", "" + poiWord.getNote());
|
rowMap3.put("l3", "");
|
rowMap3.put("l4", "");
|
map.put("r3", rowMap3);
|
for (int i = 0; i < 3; i++) {
|
row = table.getRow(i);
|
row.setHeight(400);
|
for (int j = 0; j < 4; j++) {
|
cell = row.getCell(j);
|
cellPr = cell.getCTTc().addNewTcPr();
|
cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
|
para = cell.getParagraphs().get(0);
|
para.setAlignment(ParagraphAlignment.CENTER);
|
run = para.createRun();
|
run.setFontFamily("仿宋");
|
run.setFontSize(11);
|
String text = map.get("r" + (i + 1)).get("l" + (j + 1));
|
run.setText(text);
|
}
|
}
|
//合并单元格
|
//2.列合并3行5,6,456789行的234
|
mergeCellsHorizontal(table, 0, 0, 2);
|
mergeCellsHorizontal(table, 2, 1, 3);
|
}
|
|
//全部数据
|
private void createData(XWPFDocument doc, List<TestDataInfDoc> list, int monCount) {
|
Map<String, Map<String, Object>> map = new HashMap<>();
|
int lineCount = list.size();
|
int rowCount = monCount + 4;
|
//添加表格
|
XWPFTable table = doc.createTable(rowCount, list.size() + 1);
|
table.setCellMargins(3, 5, 3, 5);
|
XWPFTableRow row;
|
XWPFTableCell cell;
|
XWPFParagraph para;
|
XWPFRun run;
|
CTTcPr cellPr;
|
for (int i = 0; i < rowCount; i++) {
|
Map<String, Object> mapRow = new HashMap<>();
|
if (i == 0) {
|
mapRow.put("l1", "放电时间");
|
for (int j = 0; j < lineCount; j++) {
|
TestDataInfDoc infDoc = list.get(j);
|
mapRow.put("l" + (j + 2), ActionUtil.sdfwithtime.format(infDoc.getRecordTime()));
|
}
|
} else if (i == 1) {
|
mapRow.put("l1", "总电压(V)");
|
for (int j = 0; j < lineCount; j++) {
|
TestDataInfDoc infDoc = list.get(j);
|
mapRow.put("l" + (j + 2), infDoc.getGroupVol());
|
}
|
} else if (i == 2) {
|
mapRow.put("l1", "电流(A)");
|
for (int j = 0; j < lineCount; j++) {
|
TestDataInfDoc infDoc = list.get(j);
|
mapRow.put("l" + (j + 2), infDoc.getTestCurr());
|
}
|
} else if (i == 3) {
|
mapRow.put("l1", "容量(AH)");
|
for (int j = 0; j < lineCount; j++) {
|
TestDataInfDoc infDoc = list.get(j);
|
mapRow.put("l" + (j + 2), infDoc.getTestCap());
|
}
|
} else {
|
int monNum = (i - 3);
|
mapRow.put("l1", monNum + "#");
|
for (int j = 0; j < lineCount; j++) {
|
TestDataInfDoc infDoc = list.get(j);
|
List<BatttestdataId> listData = infDoc.getDatalist();
|
for (int m = 0; m < listData.size(); m++) {
|
BatttestdataId bdataId = listData.get(m);
|
if (monNum == bdataId.getMonNum()) {
|
mapRow.put("l" + (j + 2), bdataId.getMonVol());
|
}
|
}
|
}
|
}
|
map.put("r" + (i + 1), mapRow);
|
}
|
for (int i = 0; i < monCount + 4; i++) {
|
row = table.getRow(i);
|
row.setHeight(400);
|
for (int j = 0; j < list.size() + 1; j++) {
|
cell = row.getCell(j);
|
cellPr = cell.getCTTc().addNewTcPr();
|
cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
|
para = cell.getParagraphs().get(0);
|
para.setAlignment(ParagraphAlignment.CENTER);
|
run = para.createRun();
|
run.setFontFamily("仿宋");
|
run.setFontSize(11);
|
String text = map.get("r" + (i + 1)).get("l" + (j + 1)).toString();
|
run.setText(text);
|
}
|
}
|
}
|
|
//电池参数
|
private void createParam(XWPFDocument doc, POIWord poiWord) {
|
//添加表格
|
XWPFTable table = doc.createTable(9, 5);
|
table.setCellMargins(3, 5, 3, 5);
|
XWPFTableRow row;
|
XWPFTableCell cell;
|
XWPFParagraph para;
|
XWPFRun run;
|
CTTcPr cellPr;
|
Map<String, Map<String, String>> map = new HashMap<>();
|
//第一行数据
|
Map<String, String> rowMap1 = new HashMap<>();
|
rowMap1.put("l1", "参数");
|
rowMap1.put("l2", "型号");
|
rowMap1.put("l3", "" + poiWord.getBattModel());
|
rowMap1.put("l4", "制造厂");
|
rowMap1.put("l5", "" + poiWord.getBattProducer());
|
map.put("r1", rowMap1);
|
//第二行数据
|
Map<String, String> rowMap2 = new HashMap<>();
|
rowMap2.put("l1", "参数");
|
rowMap2.put("l2", "安装日期");
|
rowMap2.put("l3", "" + poiWord.getBattInuseDate());
|
rowMap2.put("l4", "检查日期");
|
rowMap2.put("l5", "" + poiWord.getBattCheckDate());
|
map.put("r2", rowMap2);
|
//第三行数据
|
Map<String, String> rowMap3 = new HashMap<>();
|
rowMap3.put("l1", "参数");
|
rowMap3.put("l2", "站名");
|
rowMap3.put("l3", "" + poiWord.getTestStationName());
|
rowMap3.put("l4", "组别");
|
rowMap3.put("l5", "" + poiWord.getGroupName());
|
map.put("r3", rowMap3);
|
//第四行数据
|
Map<String, String> rowmapRow = new HashMap<>();
|
rowmapRow.put("l1", "外观检查");
|
rowmapRow.put("l2", "(1)外壳是否有裂纹、损伤和漏液现象");
|
rowmapRow.put("l3", "");
|
rowmapRow.put("l4", "");
|
rowmapRow.put("l5", "" + poiWord.getParamCheck1());
|
map.put("r4", rowmapRow);
|
//第五行数据
|
Map<String, String> rowMap5 = new HashMap<>();
|
rowMap5.put("l1", "外观检查");
|
rowMap5.put("l2", "(2)正、负极性是否正确");
|
rowMap5.put("l3", "");
|
rowMap5.put("l4", "");
|
rowMap5.put("l5", "" + poiWord.getParamCheck2());
|
map.put("r5", rowMap5);
|
//第六行数据
|
Map<String, String> rowMap6 = new HashMap<>();
|
rowMap6.put("l1", "外观检查");
|
rowMap6.put("l2", "(3)环境温度C");
|
rowMap6.put("l3", "");
|
rowMap6.put("l4", "");
|
rowMap6.put("l5", "" + poiWord.getParamCheck3());
|
map.put("r6", rowMap6);
|
//第七行数据
|
Map<String, String> rowMap7 = new HashMap<>();
|
rowMap7.put("l1", "外观检查");
|
rowMap7.put("l2", "(4)检查连接条、螺丝是否紧固、锈蚀");
|
rowMap7.put("l3", "");
|
rowMap7.put("l4", "");
|
rowMap7.put("l5", "" + poiWord.getParamCheck4());
|
map.put("r7", rowMap7);
|
//第八行数据
|
Map<String, String> rowMap8 = new HashMap<>();
|
rowMap8.put("l1", "记录");
|
rowMap8.put("l2", "装置画面显示蓄电池电压值(V)");
|
rowMap8.put("l3", "");
|
rowMap8.put("l4", "");
|
rowMap8.put("l5", "" + poiWord.getParamRecordVol1());
|
map.put("r8", rowMap8);
|
//第九行数据
|
Map<String, String> rowMap9 = new HashMap<>();
|
rowMap9.put("l1", "记录");
|
rowMap9.put("l2", "实测蓄电池电压值(v)");
|
rowMap9.put("l3", "");
|
rowMap9.put("l4", "");
|
rowMap9.put("l5", "" + poiWord.getParamRecordVol2());
|
map.put("r9", rowMap9);
|
for (int i = 0; i < 9; i++) {
|
row = table.getRow(i);
|
row.setHeight(400);
|
for (int j = 0; j < 5; j++) {
|
cell = row.getCell(j);
|
cellPr = cell.getCTTc().addNewTcPr();
|
cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
|
para = cell.getParagraphs().get(0);
|
para.setAlignment(ParagraphAlignment.CENTER);
|
run = para.createRun();
|
run.setFontFamily("仿宋");
|
run.setFontSize(11);
|
String text = map.get("r" + (i + 1)).get("l" + (j + 1));
|
run.setText(text);
|
}
|
}
|
//合并单元格
|
//1.行合并 1~3合并,4~7,8~9
|
mergeCellsVertically(table, 0, 0, 2);
|
mergeCellsVertically(table, 0, 3, 6);
|
mergeCellsVertically(table, 0, 7, 8);
|
//2.列合并3行5,6,456789行的234
|
mergeCellsHorizontal(table, 3, 1, 3);
|
mergeCellsHorizontal(table, 4, 1, 3);
|
mergeCellsHorizontal(table, 5, 1, 3);
|
mergeCellsHorizontal(table, 6, 1, 3);
|
mergeCellsHorizontal(table, 7, 1, 3);
|
mergeCellsHorizontal(table, 8, 1, 3);
|
}
|
|
//创建文本
|
private void createText(XWPFDocument doc, String msg, ParagraphAlignment align, int fontSize) {
|
XWPFParagraph para = doc.createParagraph();
|
para.setAlignment(align);//设置左对齐
|
XWPFRun run = para.createRun();
|
run.setFontFamily("仿宋");
|
run.setFontSize(fontSize);
|
run.setText(msg);
|
doc.createParagraph();
|
}
|
|
/**
|
* @Description: 跨列合并
|
*/
|
private void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
|
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
|
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
|
if (cellIndex == fromCell) {
|
// The first merged cell is set with RESTART merge value
|
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
|
} else {
|
// Cells which join (merge) the first one, are set with CONTINUE
|
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
|
}
|
}
|
}
|
|
/**
|
* @Description: 跨行合并
|
* @seehttp://stackoverflow.com/questions/24907541/row-span-with-xwpftable
|
*/
|
private void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
|
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
|
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
|
if (rowIndex == fromRow) {
|
// The first merged cell is set with RESTART merge value
|
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
|
} else {
|
// Cells which join (merge) the first one, are set with CONTINUE
|
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
|
}
|
}
|
}
|
}
|