编程问答
poi如何自定义单元格背景颜色 -凯发ag旗舰厅登录网址下载
提出问题
poi如何自定义单元格背景颜色???
解决问题
例一:具体的看注释
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.hssfcolor;
import java.io.fileoutputstream;
/** * created by ay on 2016/4/29. */ public class poibackgroundcolortest {
public static void main(string[] args) throws exception{ //创建一份 hssfworkbook excel = new hssfworkbook(); //创建第一个sheet hssfsheet sheet = excel.createsheet("我的poi之旅"); //创建第一行 hssfrow row = sheet.createrow((short) 0); //创建第一个单元格 hssfcell cell = row.createcell((short) 0); //设置单元格的值 cell.setcellvalue("ay"); //生成单元格样式 hssfcellstyle style = excel.createcellstyle(); //设置背景颜色 style.setfillforegroundcolor(hssfcolor.lime.index); //solid 填充 foreground 前景色 style.setfillpattern(hssfcellstyle.solid_foreground); cell.setcellstyle(style); //通过流写到硬盘 fileoutputstream out = new fileoutputstream("d:/old_color.xls"); excel.write(out); out.close();
//====== 这里是重点,马上要自定义单元格的样式了 ============= cell.setcellvalue("al"); //拿到palette颜色板 hssfpalette palette = excel.getcustompalette(); //这个是重点,具体的就是把之前的颜色 hssfcolor.lime.index //替换为 rgb(51,204,204) 宝石蓝这种颜色 //你可以改为 rgb(0,255,127) palette.setcoloratindex(hssfcolor.lime.index, (byte) 0, (byte) 255, (byte) 127); //====== 这里是重点,马上要自定义单元格的样式了 ============= out = new fileoutputstream("d:/new_color.xls"); excel.write(out); out.close(); } } |
d:/old_color.xls 结果
d:/new_color.xls 结果
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的poi如何自定义单元格背景颜色的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: