郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.gk.hotwork.specialWork.util;
 
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.List;
 
public class WorkUtil {
    public static void write2Docx()throws Exception{
        XWPFDocument document = new XWPFDocument();
 
 
        wordHeader(document,"动火作业");
        //基本信息表格
        XWPFTable infoTable = document.createTable(1,12);
        //infoTable.setTableAlignment(TableRowAlign.CENTER);
        //列宽自动分割
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
 
        //表格第一行
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("申请单位");
        infoTableRowOne.addNewTableCell().setText("人事部门");
        infoTableRowOne.addNewTableCell().setText("申请人");
        infoTableRowOne.addNewTableCell().setText("张三");
        infoTableRowOne.addNewTableCell().setText("作业证编号");
        infoTableRowOne.addNewTableCell().setText("XHGT-00001");
 
        //表格第二行
        XWPFTableRow infoTableRowTwo = infoTable.createRow();
        infoTableRowTwo.getCell(0).setText("动火作业级别");
        infoTableRowTwo.getCell(1).setText("特级动火作业");
        infoTableRowTwo.getCell(2).setText("动火方式");
        infoTableRowTwo.getCell(3).setText("电焊、气割、手动论切割");
 
        infoTableRowTwo.addNewTableCell().setText("");
        //表格第三行
        XWPFTableRow infoTableRowThree = infoTable.createRow();
        infoTableRowThree.getCell(0).setText("动火地点及内容");
        infoTableRowThree.getCell(1).setText("·········");
        infoTableRowThree.getCell(2).setText("");
        infoTableRowThree.getCell(3).setText("");
        infoTableRowThree.getCell(4).setText("");
        infoTableRowThree.getCell(5).setText("");
        //表格第四行
        XWPFTableRow infoTableRowFour = infoTable.createRow();
        infoTableRowFour.getCell(0).setText("动火时间");
        infoTableRowFour.getCell(1).setText("XXXX-XX-XX");
 
        mergeCellsHorizontal(infoTable,3,1,5);
 
        //分析数据
        //表格第五行
        XWPFTableRow infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("分析点名称");
        infoTableRowFive.getCell(1).setText("分析项");
        infoTableRowFive.getCell(2).setText("分析数据");
        infoTableRowFive.getCell(3).setText("分析结果");
        infoTableRowFive.getCell(4).setText("分析人");
        infoTableRowFive.getCell(5).setText("分析时间");
 
        XWPFTableRow infoTableRowSix = infoTable.createRow();
        infoTableRowSix.getCell(0).setText("XX");
        infoTableRowSix.getCell(1).setText("XX");
        infoTableRowSix.getCell(2).setText("XX");
        infoTableRowSix.getCell(3).setText("XX");
        infoTableRowSix.getCell(4).setText("XX");
        infoTableRowSix.getCell(5).setText("XX");
 
        XWPFTableRow infoTableRowSeven = infoTable.createRow();
        infoTableRowSeven.getCell(0).setText("XX");
        infoTableRowSeven.getCell(1).setText("XX");
        infoTableRowSeven.getCell(2).setText("XX");
        infoTableRowSeven.getCell(3).setText("XX");
        infoTableRowSeven.getCell(4).setText("XX");
        infoTableRowSeven.getCell(5).setText("XX");
 
        XWPFTableRow infoTableRowEight = infoTable.createRow();
        infoTableRowEight.getCell(0).setText("涉及的其他特殊作业");
        infoTableRowEight.getCell(1).setText("XX");
        infoTableRowEight.getCell(2).setText("XX");
        infoTableRowEight.getCell(3).setText("XX");
        infoTableRowEight.getCell(4).setText("XX");
        infoTableRowEight.getCell(5).setText("XX");
 
        XWPFTableRow infoTableRowNine = infoTable.createRow();
        infoTableRowNine.getCell(0).setText("涉及的其他特殊作业");
        infoTableRowNine.getCell(1).setText("危害辨识");
        infoTableRowNine.getCell(2).setText("XX");
        infoTableRowNine.getCell(3).setText("XX");
        infoTableRowNine.getCell(4).setText("XX");
        infoTableRowNine.getCell(5).setText("XX");
 
        XWPFTableRow infoTableRowTen = infoTable.createRow();
        infoTableRowTen.getCell(0).setText("安全措施");
        infoTableRowTen.getCell(1).setText("XX");
        infoTableRowTen.getCell(2).setText("XX");
        infoTableRowTen.getCell(3).setText("XX");
        infoTableRowTen.getCell(4).setText("XX");
        infoTableRowTen.getCell(5).setText("XX");
        int numberOfRows = infoTable.getNumberOfRows();
        System.out.println(numberOfRows);
 
        wordFooter(document);
 
 
        //创建文档
        File file = new File("F:\\Offer\\create_table.docx");
        if(!file.exists()){
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }
            if (!file.exists()){
                file.createNewFile();
            }
        }
        FileOutputStream out = new FileOutputStream(file);
        document.write(out);
        out.close();
    }
 
    /**
     * 作业票证头部
     * @param document
     */
    public static void wordHeader(XWPFDocument document,String workTypeName){
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
 
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("新疆国泰新华化工有限责任公司"+workTypeName+"证");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(14);
//        //换行
//        titleParagraphRun.addBreak();
 
    }
 
    /**
     * 作业票证尾部
     * @param document
     */
    public static void wordFooter(XWPFDocument document){
        //添加标题
        XWPFParagraph footerParagraph = document.createParagraph();
        //设置段落居中
        footerParagraph.setAlignment(ParagraphAlignment.CENTER);
 
        XWPFRun titleParagraphRun = footerParagraph.createRun();
        titleParagraphRun.setText("此票一式三联,一联生产车间留存,二联作业单位留存,三联生产技术科留存");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(12);
        //换行
        titleParagraphRun.addBreak();
    }
 
    /**
     * word单元格列合并列
     * @param table 表格
     * @param row 合并列所在行
     * @param startCell 开始列
     * @param endCell 结束列
     */
    public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
        for (int i = startCell; i <= endCell; i++) {
            XWPFTableCell cell = table.getRow(row).getCell(i);
            if (i == startCell) {
                // 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);
            }
        }
    }
 
    /**
     * word单元格列合并行
     * @param table 表格
     * @param col 合并行所在列
     * @param startRow 开始行
     * @param endRow 结束行
     */
    public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {
        for (int i = startRow; i <= endRow; i++) {
            XWPFTableCell cell = table.getRow(i).getCell(col);
            if (i == startRow) {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
 
    public static void expotWord(HttpServletResponse response, String fileName, XWPFDocument document) {
        try {
            fileName = URLEncoder.encode(fileName,"UTF-8");
            response.setContentType("application/x-msdownload;charset=UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".docx");
            document.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    /**
     * 清空单元格数据
     * @param row 行
     * @throws Exception
     */
    public static void clearCell(XWPFTableRow row){
        List<XWPFTableCell> tableCells = row.getTableCells();
        for (int i = 0; i < tableCells.size(); i++) {
            tableCells.get(i).getParagraphs();
            if(tableCells.get(i).getParagraphs().size()>0){
                tableCells.get(i).removeParagraph(0);
            }
 
        }
    }
    public static void clearCell1(XWPFTableRow row,int col,String text){
        XWPFTableCell cell = row.getTableCells().get(col);
        for (XWPFParagraph p : cell.getParagraphs())
        {
            for (XWPFRun r : p.getRuns())
            {
 
                r.setText(text);//要深入到原cell中的run替换内容才能保证样式一致
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
       write2Docx();
        System.out.println(System.getProperty("user.dir"));
    }
 
}