POI写2003 EXCEL文件

public void writeExcel2003(List<String> list)
    {
        //当集合为空,则认为不需要生成EXCEL文件
        if (null == list || list.isEmpty())
        {
            return;
        }
        FileOutputStream fos = null;
        
        try
        {
            String filePath = System.getProperty("user.dir")
            + File.separator + "config" + File.separator
            + "2003.xls";
            fos = new FileOutputStream(filePath);
            HSSFWorkbook workbook = new HSSFWorkbook();
            
            //文件头部的样式
            HSSFCellStyle headStyle = workbook.createCellStyle();
            headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            headStyle.setFillForegroundColor(HSSFColor.ROYAL_BLUE.index);
            headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            headStyle.setWrapText(true);
            
            HSSFSheet sheet = workbook.createSheet("sheet0");
            HSSFRow headRow = sheet.createRow(0); //创建第一行,第一行为标题部分
            
            HSSFCell headCell = headRow.createCell(0); //创建头部第一列
            headCell.setCellValue("head");  //往第一行输入值
            headCell.setCellStyle(headStyle);  //设置第一行第一列的样式
            sheet.setColumnWidth(0, 4500);  //设置第一列的列宽
            
            
            HSSFRow row = null;
            HSSFCell cell = null;

            //由于需要单数行和双数行的颜色样式设置为不一样,所以下面有两种列的样式
            
            //设置LEMON_CHIFFON颜色
            HSSFCellStyle lemonChiffonStyle = workbook.createCellStyle();  //通过workbook创建样式
            lemonChiffonStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            lemonChiffonStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            lemonChiffonStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            lemonChiffonStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            lemonChiffonStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            lemonChiffonStyle.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
//          functionStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            lemonChiffonStyle.setWrapText(true);  //设置可以自动换行
            
            //设置CORNFLOWER_BLUE颜色
            HSSFCellStyle cornlowerBlueStyle = workbook.createCellStyle();
            cornlowerBlueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cornlowerBlueStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            cornlowerBlueStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            cornlowerBlueStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            cornlowerBlueStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            cornlowerBlueStyle.setFillForegroundColor(HSSFColor.CORNFLOWER_BLUE.index);
            cornlowerBlueStyle.setWrapText(true);
            
            
            HSSFCellStyle cellStyle = null;
            String value = "";
            for (int i = 0; i < list.size(); i++)
            {
                value = list.get(i);
                if (null == value || value.isEmpty())
                {
                    //当value值为空,则进入到下一轮循环
                    continue;
                }
                
                if(i%2==0)
                {
                    //双数列设置lemonChiffonStyle样式
                    cellStyle = lemonChiffonStyle;
                }
                else
                {
                    //单数列设置cornlowerBlueStyle样式
                    cellStyle = cornlowerBlueStyle;
                }
                
                row = sheet.createRow(i + 1); // 因为第一行为标题,所以行数要+1
                
                cell = row.createCell(0); // 创建第一列
                cell.setCellValue(value);
                cell.setCellStyle(cellStyle);  //设置列的样式
            }
            //写出EXCEL文件
            workbook.write(fos);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fos)
            {
                try
                {
                    //关闭FileOutputStream流
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值