POI实战-java开发excel详解(第六章 附录)

来源:互联网 发布:标准韩国语第二册淘宝 编辑:程序博客网 时间:2024/05/21 07:51

6 附录

HSSFColor类:


生成代码:

public class TestHSSFColor {

      @SuppressWarnings({ "rawtypes", "unused" })

      public static List<String> SysoColor() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{

            List<String> list = new ArrayList<String>();

            Class clazz = Class.forName(HSSFColor.class.getName());

            Class[] Classes = clazz.getDeclaredClasses();

            Method[] methods = clazz.getDeclaredMethods();

            for(int i = 0; i < Classes.length; i++){

                  Class colorClazz = Classes[i];

                  Field field = colorClazz.getDeclaredField("index");

                  Object property = field.get(colorClazz);

                  list.add(colorClazz.getSimpleName()+":"+property);

            }

            return list;

      }

     

      public static HSSFWorkbook write(){

            HSSFWorkbook workbook = null;

            try {

                  int count = 0;

                  List<String> list = SysoColor();

                  workbook = new HSSFWorkbook();

                  HSSFSheet sheet = workbook.createSheet("HSSFColor");

                  double size = list.size();

                  double rowlength = size/5.0;

                  for(int rownum = 0; rownum < Math.ceil(rowlength); rownum++){

                        HSSFRow row = sheet.createRow(rownum);

                        for(int cellnum = 0; cellnum < 5; cellnum++){

                              HSSFCell cell = row.createCell(cellnum);

                              if(count >= list.size()){

                                    break;

                              }

                              String str = list.get(count);

                              count++;

                              String indexStr = str.substring(str.indexOf(":")+1, str.length());

                              cell.setCellValue(new HSSFRichTextString(str));

                              HSSFCellStyle cellStyle = workbook.createCellStyle();

                              cellStyle.setFillForegroundColor(Short.valueOf(indexStr));

                              cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

                              cell.setCellStyle(cellStyle);

                        }

                  }

            } catch (ClassNotFoundException e) {

                  e.printStackTrace();

            } catch (SecurityException e) {

                  e.printStackTrace();

            } catch (IllegalArgumentException e) {

                  e.printStackTrace();

            } catch (NoSuchFieldException e) {

                  e.printStackTrace();

            } catch (IllegalAccessException e) {

                  e.printStackTrace();

            }

            return workbook;

      }

     

     

      public static void main(String[] args) {

            OutputStream outputStream = null;

            try {

                  HSSFWorkbook workbook = TestHSSFColor.write();

                  outputStream = new FileOutputStream(new File("E:\\helloPOI1.xls"));

                  workbook.write(outputStream);

            } catch (FileNotFoundException e) {

                  e.printStackTrace();

            } catch (IOException e) {

                  e.printStackTrace();

            } finally{

                  if(outputStream !=null){

                        try {

                              outputStream.close();

                        } catch (IOException e) {

                              e.printStackTrace();

                        }

                  }

            }

      }

 

}

0 0
原创粉丝点击