zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.shlanbao.tzsc.utils.excel;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
 
import com.shlanbao.tzsc.utils.tools.MathUtil;
 
 
public class ExcelWriter{
 
    // 设置cell编码解决中文高位字节截断
    private static short XLS_ENCODING = HSSFCell.ENCODING_UTF_16;
    // 定制浮点数格式
    private static String NUMBER_FORMAT = "#,##0.00";
    // 定制日期格式
    private static String DATE_FORMAT = "m/d/yy"; // "m/d/yy h:mm"
    private OutputStream out = null;
    private HSSFWorkbook workbook = new HSSFWorkbook();
    private HSSFSheet sheet = workbook.createSheet();
    private HSSFRow row = null;
    private HSSFCellStyle style=null;
    
 
    public ExcelWriter() {
    }
 
    //初始化excel
    public ExcelWriter(OutputStream out) {
        this.out = out;
        //this.workbook = new HSSFWorkbook();
        //this.sheet = workbook.createSheet();
        
        //表格样式
        this.style=workbook.createCellStyle();
        
        //边框
        style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);   
        style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); 
        style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 指定单元格居中对齐   
        
        //设置字体
        HSSFFont f = workbook.createFont();  
        f.setFontHeightInPoints((short)12);  
        f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        style.setFont(f); 
        
    
        //设置列宽  
        sheet.setColumnWidth((short)0, (short)4000);  
        sheet.setColumnWidth((short)1, (short)3000);  
        sheet.setColumnWidth((short)2, (short)6000);  
        sheet.setColumnWidth((short)3, (short)6000);
        sheet.setColumnWidth((short)4, (short)6000);  
        sheet.setColumnWidth((short)5, (short)6000);  
        //sheet.setColumnWidth((short)5, (short)6000); 
        
        // 表头表格样式
        HSSFCellStyle style1=workbook.createCellStyle();  
        style1.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);   
        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 指定单元格居中对齐
        HSSFFont f1 = workbook.createFont();  
        f1.setFontHeightInPoints((short)6);  
        f1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        style1.setFont(f1);// 设置字体    
    }
 
    
    //导出Excel文件
    public void export() throws FileNotFoundException, IOException {
        try {
            workbook.write(out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            throw new IOException(" 生成导出Excel文件出错! ", e);
        } catch (IOException e) {
            throw new IOException(" 写入Excel文件出错! ", e);
        }
    }
 
    //增加一行index表示行号
    public void createRow(int index) {
        this.row = this.sheet.createRow(index);
    }
 
    
    //获取单元格的值index列好
    public String getCell(int index) {
        HSSFCell cell = this.row.getCell((short) index);
        String strExcelCell = "";
        if (cell != null) { // add this condition
            // judge
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_FORMULA:
                strExcelCell = "FORMULA ";
                break;
            case HSSFCell.CELL_TYPE_NUMERIC: {
                strExcelCell = String.valueOf(cell.getNumericCellValue());
            }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                strExcelCell = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                strExcelCell = "";
                break;
            default:
                strExcelCell = "";
                break;
            }
        }
        return strExcelCell;
    }
    /**
     * 设置单元格 index列号   value单元格填充值
     * 日期类型
     * 
     * */
    public void setDate(int index,Date value){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平   
        
        HSSFCell cell = this.row.createCell((short) index);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);     
        if(value != null){
            cell.setCellValue(simpleDateFormat.format(value));
        }else{
            cell.setCellValue("");
        }
        cell.setCellStyle(cellStyle); 
    }
    /**
     * 设置单元格 index列号   value单元格填充值
     * int类型
     * */
    public void setCell(int index, int value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        cell.setCellType(HSSFCellStyle.ALIGN_LEFT);
        cell.setCellValue(value);
    }
    /**
     * 设置单元格 index列号   value单元格填充值
     * double类型
     * 
     * */
    public void setCell(int index, double value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        cell.setCellValue(value);
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        HSSFDataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
        cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
    }
    /**
     * 设置单元格 index列号   value单元格填充值
     * String类型
     * 
     * */
    public void setCell(int index, String value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);         
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(XLS_ENCODING);
        cell.setCellValue(value);
    }
 
    public void setCell(int index, Calendar value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellValue(XLS_ENCODING);
        cell.setCellValue(value.getTime());
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
        cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
    }
 
    /**
     * 第一列新样式标题
     * 
     * */
    //设置单元格 index列号  value单元格填充值
    public void setCellTitle(int index, String value) {
        sheet.addMergedRegion(new Region(0,(short)0,0,(short)5));
        HSSFCell cell = this.row.createCell((short) index);
         //生成一个字体
        HSSFFont font1=workbook.createFont();
        font1.setColor(HSSFColor.BLACK.index);
        font1.setFontHeightInPoints((short)12);
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平   
        //把字体应用到当前的样式
        cell.setEncoding(HSSFCell.ENCODING_UTF_16); 
        cellStyle.setFont(font1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }
 
    /**
     * 第一列新值
     * 不能合并单元格
     * 
     * */
    //设置单元格 index列号  value单元格填充值
    public void setCellFirst(int index, String value) {
        //sheet.addMergedRegion(new Region(rowcoo,(short)cumcoo,rowspan,(short)cumspan));
        HSSFCell cell = this.row.createCell((short) index);
         //生成一个字体
        HSSFFont font1=workbook.createFont();
        font1.setColor(HSSFColor.BLACK.index);
        font1.setFontHeightInPoints((short)10);
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平   
      //把字体应用到当前的样式
        cell.setEncoding(HSSFCell.ENCODING_UTF_16); 
        cellStyle.setFont(font1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }
    
    //循环遍历封装值
    public void ExcelWriter(List<Object[]> dg,String np){
        File f = new File(np);
        ExcelWriter e = new ExcelWriter();
        try {
            e = new ExcelWriter(new FileOutputStream(f));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        int y=0;
        e.createRow(0);
        e.setCellTitle(0,"设备有效作业率" );
        e.createRow(1);
        e.setCellFirst(0,"设备名称" );
        e.setCellFirst(1, "班次");
        e.setCellFirst(2, "总时间");
        e.setCellFirst(3,"实际产量");
        e.setCellFirst(4, "车间实际产量");
        e.setCellFirst(5,"有效作业率" );
        //e.setCellFirst(6, "实际有效作业率");
        
        for(int i=0;i<dg.size();i++){
            Object[] w=(Object[])dg.get(i);
            e.createRow(i+2);
            y++;
            if(y>0){
                y--;
                try {
                    e.setCell(y,   w[0].toString());
                    e.setCell(1,   w[2].toString());
                    e.setCell(y+2, w[4].toString());
                    e.setCell(y+3, w[5].toString());
                    e.setCell(y+4, w[3].toString());
                    e.setCell(y+5, this.getEfic(w[3].toString(), w[4].toString()));
                    //e.setCell(y+6, "");
                } catch (Exception e1) {
                    // TODO: handle exception
                }
            }
        }
 
        try {
            e.export();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
 
    
    private String getEfic(String qty,String r_qty){
        if(Float.parseFloat(r_qty)==0){
            return  "0";
        }else{
            BigDecimal a1=new BigDecimal(Double.parseDouble(qty));
            BigDecimal b1=new BigDecimal(Double.parseDouble(r_qty));
            Double st=MathUtil.roundHalfUp((a1.divide(b1,4, BigDecimal.ROUND_HALF_EVEN).doubleValue()*100), 2);
            if(st>120){
                st=96.1; //有效作业率大于100,给出默认值90.1,防止数据错误,导致误差太大(测试数据,误差大正常);wch 2015年10月13日
            }
            if(st==0){
                return "0";
            }
            return st.toString();
        }
    }
    
    
    
    /**
     * 张璐-2015.10.14
     * 设置列,带合并单元格的
     * @param index
     * @param value
     * @param rowcoo:行坐标
     * @param cumcoo:列坐标
     * @param rowspan:跨行
     * @param cumspan:跨列
     */
    //设置单元格 index列号  value单元格填充值
    public void setCellFirstSpan(int index, String value,int rowcoo,int cumcoo,int rowspan,int cumspan) {
        sheet.addMergedRegion(new Region(rowcoo,(short)cumcoo,rowspan,(short)cumspan));
        HSSFCell cell = this.row.createCell((short) index);
         //生成一个字体
        HSSFFont font1=workbook.createFont();
        font1.setColor(HSSFColor.BLACK.index);
        font1.setFontHeightInPoints((short)10);
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平   
      //把字体应用到当前的样式
        cell.setEncoding(HSSFCell.ENCODING_UTF_16); 
        cellStyle.setFont(font1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }
 
}