POI实战-java开发excel详解之附录

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();

                        }

                  }

            }

      }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值