Helius
2020-12-15 2914588a65371a3ce43f678cde0a26cd8da26611
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
 * 
 * 文件名:ExportExcelUtil.java
 * 版权:Copyright HNNE All Rights Reserved.
 * 描述:
 * 修改人:jyy
 * 修改时间:2016年8月1日
 * 修改内容:
 */
 
package com.matrix.system.hive.plugin.util;
 
import com.matrix.core.tools.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author jyy
 * @version 1.0, 2016年8月1日
 */
public class ExcelUtil {
 
    Logger log = Logger.getLogger(this.getClass());
 
    // 2007 版本以上 最大支持1048576行
    public final static String EXCEl_FILE_2007 = "2007";
    // 2003 版本 最大支持65536 行
    public final static String EXCEL_FILE_2003 = "2003";
 
    /**
     * <p>
     * 导出无头部标题行Excel <br>
     * 时间格式默认:yyyy-MM-dd hh:mm:ss <br>
     * </p>
     * 
     * @param title
     *            表格标题
     * @param dataset
     *            数据集合
     * @param out
     *            输出流
     * @param version
     *            2003 或者 2007,不传时默认生成2003版本
     * @param hasNum
     *            是否有序号
     */
    public ByteArrayInputStream exportExcel(String title, Collection<?> dataset, OutputStream out, String version,
            boolean isMerg, List<String[]> mergParm, boolean hasNum) {
        if (StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())) {
            return exportExcel2003(title, null, dataset, "yyyy-MM-dd hh:mm:ss", isMerg, mergParm, hasNum);
        } else {
            return exportExcel2007(title, null, dataset, "yyyy-MM-dd hh:mm:ss", isMerg, mergParm, null);
        }
    }
 
    /**
     * <p>
     * 导出带有头部标题行的Excel <br>
     * 时间格式默认:yyyy-MM-dd hh:mm:ss <br>
     * </p>
     * 
     * @param title
     *            表格标题
     * @param headers
     *            头部标题集合
     * @param dataset
     *            数据集合
     * @param out
     *            输出流
     * @param version
     *            2003 或者 2007,不传时默认生成2003版本
     * @param hasNum
     *            是否有序号
     */
    public ByteArrayInputStream exportExcel(String title, String[] headers, Collection<?> dataset, String version,
            boolean isMerg, List<String[]> mergParm, boolean hasNum) {
        if (StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())) {
            return exportExcel2003(title, headers, dataset, "yyyy-MM-dd hh:mm:ss", isMerg, mergParm, hasNum);
        } else {
            return exportExcel2007(title, headers, dataset, "yyyy-MM-dd hh:mm:ss", isMerg, mergParm, null);
        }
    }
 
    /**
     * <p>
     * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br>
     * 此版本生成2007以上版本的文件 (文件后缀:xlsx)
     * </p>
     * 
     * @param title
     *            表格标题名
     * @param headers
     *            表格头部标题集合
     * @param dataset
     *            需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的
     *            JavaBean属性的数据类型有基本数据类型及String,Date
     * @param out
     *            与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
     * @param pattern
     *            如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss"
     * @param isMerg
     *            是否需要合并单元格
     * @param mergParm
     *            合并单元格参数
     * @param cols
     *            要生成excel列数
     * @param headerRows
     *            要生成的excel表头的行数
     * @param list
     *            表头列字符串参数(Map<String, Integer[]>)String表头列名,Integer[]
     */
    @SuppressWarnings({ "rawtypes" })
    public static ByteArrayInputStream exportExcel2007(String title, String[] headers, Collection<?> dataset,
            String pattern, boolean isMerg, List<String[]> mergParm, List<Map<String, Integer[]>> list) {
        // List<Map<String, Integer[]>> lsit = null;
        // 声明一个工作薄
 
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet(title);
        sheet.setDefaultColumnWidth(20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(new XSSFColor(java.awt.Color.GRAY));
        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setWrapText(true);
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        font.setFontName("宋体");
        font.setColor(new XSSFColor(java.awt.Color.BLACK));
        font.setFontHeightInPoints((short) 11);
        // 把字体应用到当前的样式
        style.setFont(font);
        // 生成并设置另一个样式
        XSSFCellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));
        style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
        style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        // 生成另一个字体
        XSSFFont font2 = workbook.createFont();
        font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style2.setFont(font2);
        if (isMerg && (mergParm != null && mergParm.size() > 0)) {// 如果是需要合并单元格
            // 产生表格标题行
            XSSFRow row = sheet.createRow(0);
            XSSFCell cellHeader;
            for (int i = 0; i < headers.length; i++) {
                cellHeader = row.createCell(i);
                cellHeader.setCellStyle(style);
                cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
            }
        } else {// 不需要合并单元格
 
        }
        // 设置表格默认列宽度为20个字节
 
        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        XSSFCell cellHeader;
        for (int i = 0; i < headers.length; i++) {
            cellHeader = row.createCell(i);
            cellHeader.setCellStyle(style);
            cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
        }
 
        // 遍历集合数据,产生数据行
        Iterator<?> it = dataset.iterator();
        int index = 0;
        Object t;
        Field[] fields;
        Field field;
        XSSFRichTextString richString;
        Pattern p = Pattern.compile("^//d+(//.//d+)?$");
        Matcher matcher;
        String fieldName;
        String getMethodName;
        XSSFCell cell;
        Class tCls;
        Method getMethod;
        Object value;
        String textValue;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        while (it.hasNext()) {
            index++;
            row = sheet.createRow(index);
            t = it.next();
            // 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
            fields = clearUnChekedFields(t.getClass().getDeclaredFields());
            for (int i = 0; i < fields.length; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(style2);
                field = fields[i];
                fieldName = field.getName();
                getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                try {
                    tCls = t.getClass();
                    getMethod = tCls.getMethod(getMethodName, new Class[] {});
                    value = getMethod.invoke(t, new Object[] {});
                    // 判断值的类型后进行强制类型转换
                    textValue = null;
                    if (value instanceof Integer) {
                        cell.setCellValue((Integer) value);
                    } else if (value instanceof Float) {
                        textValue = String.valueOf((Float) value);
                        cell.setCellValue(textValue);
                    } else if (value instanceof Double) {
                        textValue = String.valueOf((Double) value);
                        cell.setCellValue(textValue);
                    } else if (value instanceof Long) {
                        cell.setCellValue((Long) value);
                    } else if (value instanceof Boolean) {
                        textValue = "是";
                        if (!(Boolean) value) {
                            textValue = "否";
                        }
                    } else if (value instanceof Date) {
                        textValue = sdf.format((Date) value);
                    } else {
                        // 其它数据类型都当作字符串简单处理
                        if (value != null) {
                            textValue = value.toString();
                        }
                    }
                    if (textValue != null) {
                        matcher = p.matcher(textValue);
                        if (matcher.matches()) {
                            // 是数字当作double处理
                            cell.setCellValue(Double.parseDouble(textValue));
                        } else {
                            richString = new XSSFRichTextString(textValue);
                            cell.setCellValue(richString);
                        }
                    }
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } finally {
                    // 清理资源
                }
            }
        }
        ByteArrayInputStream bais = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            workbook.write(bos);
            bais = new ByteArrayInputStream(bos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bais;
    }
 
    /**
     * <p>
     * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br>
     * 此方法生成2003版本的excel,文件名后缀:xls <br>
     * </p>
     * 
     * @param title
     *            表格标题名
     * @param headers
     *            表格头部标题集合
     * @param dataset
     *            需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的
     *            JavaBean属性的数据类型有基本数据类型及String,Date
     * @param out
     *            与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
     * @param pattern
     *            如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss"
     * 
     * @param hasNum
     *            第一列是否为序列
     */
    @SuppressWarnings({ "rawtypes" })
    public ByteArrayInputStream exportExcel2003(String title, String[] headers, Collection<?> dataset, String pattern,
            boolean isMerg, List<String[]> mergParm, boolean hasNum) {
 
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet(title);
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth(20);
        // 生成一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(HSSFColor.WHITE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成一个字体
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontName("宋体");
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 11);
        // 把字体应用到当前的样式
        style.setFont(font);
        // 生成并设置另一个样式
        HSSFCellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(HSSFColor.WHITE.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 生成另一个字体
        HSSFFont font2 = workbook.createFont();
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style2.setFont(font2);
 
        // 产生表格标题行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cellHeader;
        int numIndex = 0;
        if (hasNum) {// 如果需要序号,添加序号头
            cellHeader = row.createCell(numIndex);
            cellHeader.setCellStyle(style);
            cellHeader.setCellValue("序号");
            numIndex = 1;
        }
        for (int i = numIndex; i < headers.length + numIndex; i++) {
            cellHeader = row.createCell(i);
            cellHeader.setCellStyle(style);
            cellHeader.setCellValue(new HSSFRichTextString(headers[i - numIndex]));
        }
 
        // 遍历集合数据,产生数据行
        Iterator<?> it = dataset.iterator();
        int index = 0;
        Object t;
        Field[] fields;
        Field field;
        HSSFRichTextString richString;
        Pattern p = Pattern.compile("^//d+(//.//d+)?$");
        Matcher matcher;
        String fieldName;
        String getMethodName;
        HSSFCell cell;
        Class tCls;
        Method getMethod;
        Object value;
        String textValue;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        int rowNum = 1;
        while (it.hasNext()) {
            int cnumIndex = 0;
            index++;
            row = sheet.createRow(index);
            if (hasNum) {// 如果需要序号,添加序号头
                cell = row.createCell(cnumIndex);
                cell.setCellStyle(style2);
                cell.setCellValue(rowNum++);
                cnumIndex = 1;
            }
            t = it.next();
            // 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
            // fields = t.getClass().getDeclaredFields();
            fields = clearUnChekedFields(t.getClass().getDeclaredFields());
            for (int i = cnumIndex; i < fields.length + cnumIndex; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(style2);
                field = fields[i - cnumIndex];
                fieldName = field.getName();
                getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                try {
                    tCls = t.getClass();
                    getMethod = tCls.getMethod(getMethodName, new Class[] {});
                    value = getMethod.invoke(t, new Object[] {});
                    // 判断值的类型后进行强制类型转换
                    textValue = null;
                    if (value instanceof Integer) {
                        textValue = String.valueOf((Integer) value);
                        // cell.setCellValue((Integer) value);
                    } else if (value instanceof Float) {
                        textValue = String.valueOf((Float) value);
                        // cell.setCellValue(textValue);
                    } else if (value instanceof Double) {
                        textValue = String.valueOf((Double) value);
                        // cell.setCellValue(textValue);
                    } else if (value instanceof Long) {
                        textValue = String.valueOf((Long) value);
                        // cell.setCellValue((Long) value);
                    }
                    if (value instanceof Boolean) {
                        textValue = "是";
                        if (!(Boolean) value) {
                            textValue = "否";
                        }
                    } else if (value instanceof Date) {
                        textValue = sdf.format((Date) value);
                    } else {
                        // 其它数据类型都当作字符串简单处理
                        if (value != null) {
                            textValue = value.toString();
                        }
                    }
                    if (textValue != null) {
                        matcher = p.matcher(textValue);
                        if (matcher.matches()) {
                            // 是数字当作double处理
                            cell.setCellValue(Double.parseDouble(textValue));
                        } else {
                            richString = new HSSFRichTextString(StringUtils.getNull(textValue));
                            cell.setCellValue(richString);
                        }
                    }
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } finally {
                    // 清理资源
                }
            }
        }
 
        ByteArrayInputStream bais = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            workbook.write(bos);
            bais = new ByteArrayInputStream(bos.toByteArray());
            bos.close();
        } catch (IOException e) {
            log.debug("excelutil异常", e);
        }
 
        return bais;
    }
 
    /**
     * 只加入包含ExcelAnnotion uncheked = true的变量
     * 
     * @param fields
     * @return
     */
    private static Field[] clearUnChekedFields(Field[] fields) {
        List<Field> list = new ArrayList<Field>();
        for (int i = 0; i < fields.length; i++) {
            // 获取方法上注解
            Annotation annotation = fields[i].getAnnotation(ExcelAnnotation.class);
            if (annotation instanceof ExcelAnnotation) {
                ExcelAnnotation ea = (ExcelAnnotation) annotation;
                if (ea.checked()) {// 该标签为true的时候,加入导出行列
                    list.add(fields[i]);
                } else {
                    // 忽略改变量
                    continue;
                }
            }
        }
        // list to array
        return (Field[]) list.toArray(new Field[list.size()]);
    }
 
    /**
     * 
     * @Title: exportExcel
     * 标题 @param @param headers 表头信息 @param @param dataset
     * 数据集合(需要使用者在调用之前自己将数据拼串好) @param @param pattern
     * 时间格式(如果有时间类型数据) @param @return 设定文件 @return ByteArrayInputStream
     * 返回类型 @throws
     */
    public ByteArrayInputStream exportExcel(String title, String[] headers, List<List<?>> dataset, String pattern) {
        // List<Map<String, Integer[]>> lsit = null;
        // 声明一个工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet(title);
        sheet.setDefaultColumnWidth(20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置excel样式
        style.setFillForegroundColor(new XSSFColor(java.awt.Color.GRAY));
        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setWrapText(true);
        // 生成字体样式
        XSSFFont font = workbook.createFont();
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        font.setFontName("宋体");
        font.setColor(new XSSFColor(java.awt.Color.BLACK));
        font.setFontHeightInPoints((short) 11);
        // 把字体应用到当前的样式
        style.setFont(font);
        // 生成并设置另一个样式
        XSSFCellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));
        style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
        style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        // 生成另一个字体
        XSSFFont font2 = workbook.createFont();
        font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style2.setFont(font2);
        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        XSSFCell cellHeader;
        for (int i = 0; i < headers.length; i++) {
            cellHeader = row.createCell(i);
            cellHeader.setCellStyle(style);
            cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
        }
        // 遍历集合数据,产生数据行
        Iterator<List<?>> it = dataset.iterator();
        int index = 0;
        XSSFRichTextString richString;
        Pattern p = Pattern.compile("^//d+(//.//d+)?$");
        Matcher matcher;
        XSSFCell cell;
        Object value;
        String textValue;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        while (it.hasNext()) {
            List<?> t = new ArrayList<>();
            index++;
            row = sheet.createRow(index);
            t = it.next();
            // 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
            for (int i = 0; i < t.size(); i++) {
                cell = row.createCell(i);
                cell.setCellStyle(style2);
                try {
                    value = t.get(i);
                    // 判断值的类型后进行强制类型转换
                    textValue = null;
                    if (value instanceof Integer) {
                        cell.setCellValue((Integer) value);
                    } else if (value instanceof Float) {
                        textValue = String.valueOf((Float) value);
                        cell.setCellValue(textValue);
                    } else if (value instanceof Double) {
                        textValue = String.valueOf((Double) value);
                        cell.setCellValue(textValue);
                    } else if (value instanceof Long) {
                        cell.setCellValue((Long) value);
                    } else if (value instanceof Boolean) {
                        textValue = "是";
                        if (!(Boolean) value) {
                            textValue = "否";
                        }
                    } else if (value instanceof Date) {
                        textValue = sdf.format((Date) value);
                    } else {
                        // 其它数据类型都当作字符串简单处理
                        if (value != null) {
                            textValue = value.toString();
                        }
                    }
                    if (textValue != null) {
                        matcher = p.matcher(textValue);
                        if (matcher.matches()) {
                            // 是数字当作double处理
                            cell.setCellValue(Double.parseDouble(textValue));
                        } else {
                            richString = new XSSFRichTextString(textValue);
                            cell.setCellValue(richString);
                        }
                    }
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } finally {
                    // 清理资源
                }
            }
        }
        ByteArrayInputStream bais = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            workbook.write(bos);
            bais = new ByteArrayInputStream(bos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bais;
    }
 
    public static HSSFWorkbook generateExcel(String title, String[] headers,List<List<Object>> list) {  
        HSSFWorkbook book = new HSSFWorkbook();  
        try{  
             HSSFSheet sheet = book.createSheet("Sheet1");  
            //sheet.autoSizeColumn(1, true);//自适应列宽度  
            //样式设置  
            HSSFCellStyle style = book.createCellStyle();  
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
              style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
              style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
              style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
              style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
              style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
              style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
              // 生成一个字体  
              HSSFFont font = book.createFont();  
              font.setColor(HSSFColor.VIOLET.index);  
              font.setFontHeightInPoints((short) 12);  
              font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
              // 把字体应用到当前的样式  
              style.setFont(font);  
              
                
              HSSFCellStyle style2 = book.createCellStyle();  
                  //设置上下左右边框  
                  style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
                  style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
                  style2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
                  style2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
                  style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
                   //设置单元格背景色
                style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                  style.setFillPattern(CellStyle.SOLID_FOREGROUND);
              //填充表头标题  
              int colSize = list.get(0).size();  
            // System.out.println("size:" + colSize);  
              //合并单元格供标题使用(表名)  
              sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colSize-1));  
              HSSFRow firstRow = sheet.createRow(0);//第几行(从0开始)  
              HSSFCell firstCell = firstRow.createCell(0);  
              firstCell.setCellValue(title);  
              firstCell.setCellStyle(style);  
                
              //填充表头header  
              HSSFRow row = sheet.createRow(1);  
              for(int i=0; i< headers.length; i++) {  
                  sheet.autoSizeColumn((short)i,true);
                  HSSFCell cell = row.createCell(i);  
                  cell.setCellValue(headers[i]);  
                  cell.setCellStyle(style2);  
              }  
              
              //填充表格内容  
              for(int i=0; i<list.size(); i++) { 
                  HSSFRow row2 = sheet.createRow(i+2);//index:第几行  
                  List<?> rowList = list.get(i);  
                  for(int j=0; j<rowList.size(); j++) {
                      String textValue = "";  
                      sheet.autoSizeColumn((short)j,true);
                      Object value = rowList.get(j);  
                      HSSFCell cell = row2.createCell(j);//第几列:从0开始  
                      
                          if(value!=null){
                              textValue = String.valueOf(value);
                          }else{
                              textValue="";
                          }
                          cell.setCellValue(textValue);
                   
                          cell.setCellStyle(style2);  
                  }  
              }  
        }catch(
 
    Exception ex)
    {
        ex.printStackTrace();
    }return book;
    }
 
    /**
     * 一个excel含有多个表格
     * 
     * @author jiangyouyao
     * @param title
     *            excel标题
     * @param headers
     *            多个标题的二位数组
     * @param list
     *            多个表格的内容集合
     * @return
     */
    public static HSSFWorkbook generateExcelMore(String title, String[][] headers, List<List<List<Object>>> list) {
        HSSFWorkbook book = new HSSFWorkbook();
        try {
            HSSFSheet sheet = book.createSheet("Sheet1");
            // sheet.autoSizeColumn(1, true);//自适应列宽度
            // 样式设置
            HSSFCellStyle style = book.createCellStyle();
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 生成一个字体
            HSSFFont font = book.createFont();
            font.setColor(HSSFColor.VIOLET.index);
            font.setFontHeightInPoints((short) 12);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 把字体应用到当前的样式
            style.setFont(font);
            HSSFCellStyle style2 = book.createCellStyle();
            // 设置上下左右边框
            style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 设置单元格背景色
            style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
            // 填充表头标题 ,找出长度最大的集合的长度,进行合并
            List<List<Object>> max = list.get(0);
            for (int j = 1; j < list.size(); j++) {
                max = (max.size() >= list.get(j).size()) ? max : list.get(j);
            }
            int colSize = max.size();
            // System.out.println("size:" + colSize);
            // 合并单元格供标题使用(表名)
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colSize - 1));
            HSSFRow firstRow = sheet.createRow(0);// 第几行(从0开始)
            HSSFCell firstCell = firstRow.createCell(0);
            firstCell.setCellValue(title);
            firstCell.setCellStyle(style);
 
            // countRow= 表名所占的一行1+标题行
            int countRow = 0;
            for (int k = 0; k < headers.length; k++) {
                String[] header = headers[k];
                countRow += 1;
                // 不是首航则要空一行
                if (k != 0) {
                    countRow++;
                }
                // 填充表头header
                HSSFRow row = sheet.createRow(countRow);
                for (int i = 0; i < header.length; i++) {
                    sheet.autoSizeColumn((short) i, true);
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(header[i]);
                    cell.setCellStyle(style2);
                }
 
                // 填充表格内容
                List<List<Object>> contentList = list.get(k);
 
                for (int i = 0; i < contentList.size(); i++) {
                    countRow++;
                    HSSFRow row2 = sheet.createRow(countRow);// index:第几行
 
                    List<?> rowList = contentList.get(i);
                    for (int j = 0; j < rowList.size(); j++) {
                        String textValue = "";
                        sheet.autoSizeColumn((short) j, true);
                        Object value = rowList.get(j);
                        HSSFCell cell = row2.createCell(j);// 第几列:从0开始
                        if (value instanceof Integer) {
                            cell.setCellValue((Integer) value);
                        } else if (value instanceof Float) {
                            textValue = String.valueOf((Float) value);
                            cell.setCellValue(textValue);
                        } else if (value instanceof Double) {
                            textValue = String.valueOf((Double) value);
                            cell.setCellValue(textValue);
                        } else if (value instanceof Long) {
                            cell.setCellValue((Long) value);
                        } else if (value instanceof Boolean) {
                            textValue = "是";
                            if (!(Boolean) value) {
                                textValue = "否";
                            }
                            cell.setCellValue(textValue);
                        } else {
                            // 其它数据类型都当作字符串简单处理
                            if (value != null) {
                                textValue = value.toString();
                            }
                            cell.setCellValue(textValue);
                        }
                        cell.setCellStyle(style2);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return book;
    }
 
    public static class ViewExcel {
        protected void buildExcelDocument(Map<String, Object> map, HSSFWorkbook book, HttpServletRequest request,
                HttpServletResponse response) throws Exception {
        }
 
        public static void buildExcel(Map<String, Object> map, HSSFWorkbook book, HttpServletRequest request,
                HttpServletResponse response, String fileName) throws Exception {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            OutputStream ouputStream = response.getOutputStream();
            book.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
 
        }
 
        public static void buildExcel2007(Map<String, Object> map, XSSFWorkbook book, HttpServletRequest request,
                HttpServletResponse response, String fileName) throws Exception {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            OutputStream ouputStream = response.getOutputStream();
            book.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
 
        }
 
    }
 
    /**
     * 创建简单的2007excel(.xlsx)
     * 
     * @param sheetName
     *            sheet名字
     * @param columns
     *            列名数组
     * @param dataList
     *            单元格数据集合
     * @author mlg
     * @return XSSFWorkbook对象
     */
    public static XSSFWorkbook createSimple2007(String sheetName, String[] columns, List<List<Object>> dataList) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet(sheetName);
        // 生成标题行
        XSSFCellStyle style = workbook.createCellStyle(); // 样式对象
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平 居中
        XSSFRow row0 = sheet.createRow(0);
        sheet.addMergedRegion(new CellRangeAddress(0, (short) 1, 0, columns.length - 1));
        XSSFCell cell0 = row0.createCell(0);
        cell0.setCellStyle(style);
        cell0.setCellValue(sheetName);
        // 产生表格表头
        XSSFRow row = sheet.createRow(2);
        for (int i = 0; i < columns.length; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(columns[i]);
        }
        for (int i = 0; i < dataList.size(); i++) {
            List<Object> list = dataList.get(i);
            XSSFRow dataRow = sheet.createRow(i + 3);
 
            for (int j = 0; j < list.size(); j++) {
                XSSFCell cell = dataRow.createCell(j);
                cell.setCellValue((list.get(j) == null) ? "" : list.get(j).toString());
            }
        }
        return workbook;
    }
 
}