POI生成Excel

来源:互联网 发布:推荐算法的java实现 编辑:程序博客网 时间:2024/05/20 07:33
参考:http://www.cnblogs.com/Areas/archive/2012/06/26/2563139.html 和 http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html
            try{    String now = new SimpleDateFormat("yyyy").format(new Date()); // 声明一个工作薄        HSSFWorkbook workbook = new HSSFWorkbook();        // 生成一个表格        HSSFSheet sheet = workbook.createSheet("aaa");        //默认宽度        sheet.setDefaultColumnWidth(12);                // 生成一个样式        HSSFCellStyle style = workbook.createCellStyle();        // 设置这些样式        style.setFillForegroundColor(HSSFColor.CORNFLOWER_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_JUSTIFY);        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);        style.setWrapText(true);        // 生成一个字体        HSSFFont font = workbook.createFont();        font.setColor(HSSFColor.GREY_80_PERCENT.index);//字体颜色        font.setFontHeightInPoints((short) 11);        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体        // 把字体应用到当前的样式        style.setFont(font);                // 声明一个画图的顶级管理器        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();               // 产生表格标题行        HSSFRow row = sheet.createRow(0);                String[][] comms = {{"省分代码"},{"省分名称"},{"3G净增用户预算值"},{"填报日期"}};                for (short i = 0; i < comms.length; i++) {                 // 定义注释的大小和位置,详见文档        HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,                0, 0, 0, (short) 4, 2, (short) 6, 5));        // 设置注释内容        comment.setString(new HSSFRichTextString("格式要求:"));                    HSSFCell cell = row.createCell(i);            cell.setCellStyle(style);            HSSFRichTextString text = new HSSFRichTextString(comms[i][0]);            cell.setCellValue(text);            cell.setCellComment(comment);        }                OutputStream out = new FileOutputStream("D://a.xls");workbook.write(out); //把相应的Excel工作薄存盘} catch (Exception e){e.printStackTrace();}

原创粉丝点击