博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POI简易帮助文档--给Excel设置样式
阅读量:6967 次
发布时间:2019-06-27

本文共 6353 字,大约阅读时间需要 21 分钟。

 正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据。下面还是从简单的例子出发,学习和了解POI的样式设计。

  一、我的位置。

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class ExcelStyle {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页        Row row=sheet.createRow(2); // 创建一个行        row.setHeightInPoints(30);                createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);        createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);        createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);        createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);                FileOutputStream fileOut=new FileOutputStream("D:\\工作簿.xls");        wb.write(fileOut);        fileOut.close();    }        /**     * 创建一个单元格并为其设定指定的对齐方式     * @param wb 工作簿     * @param row 行     * @param column  列     * @param halign  水平方向对其方式     * @param valign  垂直方向对其方式     */    private static void createCell(Workbook wb,Row row,short column,short halign,short valign){        Cell cell=row.createCell(column);  // 创建单元格        cell.setCellValue(new HSSFRichTextString("我在这"));  // 设置值        CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式        cellStyle.setAlignment(halign);  // 设置单元格水平方向对其方式        cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式        cell.setCellStyle(cellStyle); // 设置单元格样式    }    }

  

效果显示:

  

  二、我的边框

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Border {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页        Row row=sheet.createRow(1); // 创建一个行                Cell cell=row.createCell(1); // 创建一个单元格        cell.setCellValue(4);                CellStyle cellStyle=wb.createCellStyle();         cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色                cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左边边框        cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色                cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框        cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右边边框颜色                cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上边边框颜色                cell.setCellStyle(cellStyle);        FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");        wb.write(fileOut);        fileOut.close();    }}

  

效果显示:

  

  三、我的背景

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Bg {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页        Row row=sheet.createRow(1); // 创建一个行                Cell cell=row.createCell(1);        cell.setCellValue("看不清我");        CellStyle cellStyle=wb.createCellStyle();        cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);          cell.setCellStyle(cellStyle);                        Cell cell2=row.createCell(2);        cell2.setCellValue("我的前景色与众不同");        CellStyle cellStyle2=wb.createCellStyle();        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);          cell2.setCellStyle(cellStyle2);                FileOutputStream fileOut=new FileOutputStream("D:\\bg.xls");        wb.write(fileOut);        fileOut.close();    }}

  

效果显示:

  

  四、合并单元格

package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;public class GetTogether {    public static void main(String[] args) throws Exception{        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页        Row row=sheet.createRow(1); // 创建一个行                Cell cell=row.createCell(1);        cell.setCellValue("我们被合并单元格啦!");                sheet.addMergedRegion(new CellRangeAddress(                1, // 起始行                2, // 结束行                1, // 其实列                2  // 结束列        ));                        FileOutputStream fileOut=new FileOutputStream("D:\\Together.xls");        wb.write(fileOut);        fileOut.close();    }}

  

效果显示:

  

 

转载于:https://www.cnblogs.com/kangyanxiang/p/4583175.html

你可能感兴趣的文章
三、Flask_会话控制与请求钩子
查看>>
WS Security 认证方式详解
查看>>
Spring Webflux: Kotlin DSL [片断]
查看>>
搜索引擎选择: Elasticsearch与Solr
查看>>
mysql联合索引
查看>>
监听服务管理(转)
查看>>
java中Hashtable中的t为什么是小写(转)
查看>>
linux C 内存管理方式之半动态
查看>>
图文并茂的生产者消费者应用实例demo
查看>>
asp.net core上使用redis探索(1)
查看>>
程序员的职业素养(读书笔记)-- 第一章
查看>>
Java实现线性表-顺序表示和链式表示
查看>>
HDU Simple Addition Expression
查看>>
mysql启动和关闭外键约束的方法
查看>>
idea如何打war包?(部署tomcat后具有class文件)
查看>>
安装 Docker <一>
查看>>
20165206 2017-2018-2 《Java程序设计》第三周学习总结
查看>>
C#中的Dictionary字典类介绍
查看>>
canvas使用2
查看>>
PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
查看>>