精华内容
下载资源
问答
  • poi设置背景色

    2017-01-20 10:19:00
    headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headStyle.setFillForegroundColor(HSSFColor.RED.index);
    headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);   
    headStyle.setFillForegroundColor(HSSFColor.RED.index);
    展开全文
  • Please explain me how to set background color and borders at the same time.import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss....

    at start i want to say that i'm totally new in developers world.

    I tried to generate an excel sheet that contains Mutiplication Table with borders and set background color but only for 1st column and row.

    Here is a correct Example: correct example

    I wrote something like that, but in result file colored cells has not have borders :(.

    Please explain me how to set background color and borders at the same time.import org.apache.poi.ss.usermodel.Workbook;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import org.apache.poi.ss.usermodel.Sheet;

    import org.apache.poi.ss.usermodel.Row;

    import org.apache.poi.ss.usermodel.Cell;

    import org.apache.poi.ss.usermodel.CellStyle;

    import org.apache.poi.ss.usermodel.IndexedColors;

    import java.awt.image.IndexColorModel;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.util.Scanner;

    public class Excel {

    public static void main(String[] args) throws IOException {

    Scanner in = new Scanner(System.in);

    System.out.println("enter number of rows: ");

    int x = in.nextInt();

    System.out.println("enter number of columns: ");

    int y = in.nextInt();

    System.out.println("enter name of file: ");

    String fileName = in.next() + ".xls";

    System.out.println("Multiplication table will be created in file: " + fileName);

    createExcelMultiplicationTable(fileName, x, y);

    System.out.println("Process successful executed");

    }

    private static void createExcelMultiplicationTable(String fileName, int x, int y) throws IOException {

    Workbook workbook = new HSSFWorkbook();

    Sheet sheet = workbook.createSheet("multiplicationTable");

    CellStyle backgroundStyle = workbook.createCellStyle();

    backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());

    backgroundStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

    CellStyle borderStyle = workbook.createCellStyle();

    borderStyle.setBorderBottom(CellStyle.BORDER_THIN);

    borderStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

    borderStyle.setBorderLeft(CellStyle.BORDER_THIN);

    borderStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());

    borderStyle.setBorderRight(CellStyle.BORDER_THIN);

    borderStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

    borderStyle.setBorderTop(CellStyle.BORDER_THIN);

    borderStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());

    for (int i = 1; i <= x; i++) {

    Row row = sheet.createRow(i - 1);

    for (int j = 1; j <= y; j++) {

    Cell cell = row.createCell(j - 1);

    cell.setCellValue(i * j);

    cell.setCellStyle(borderStyle);

    if (cell.getRowIndex() == 0 || cell.getColumnIndex() == 0) {

    cell.setCellStyle(backgroundStyle);

    }

    }

    }

    FileOutputStream out = new FileOutputStream(fileName);

    workbook.write(out);

    out.close();

    }

    }

    解决方案

    change backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); to

    backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

    And you can set border as like below :

    backgroundStyle.setBorderBottom(CellStyle.BORDER_THIN);

    backgroundStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

    backgroundStyle.setBorderLeft(CellStyle.BORDER_THIN);

    backgroundStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());

    backgroundStyle.setBorderRight(CellStyle.BORDER_THIN);

    backgroundStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

    backgroundStyle.setBorderTop(CellStyle.BORDER_THIN);

    backgroundStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());

    This will give you yellow color and border as required

    展开全文
  • poi设置背景颜色 字体 合并单元格

    千次阅读 2018-10-17 15:24:24
    生成excel poi设置背景颜色 字体 合并单元格 。。。。 包 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; ...

    生成excel poi设置背景颜色 字体 合并单元格 。。。。

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    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;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    

    代码

     public static void writer(String path, String fileName,String fileType,List<object> list,String titleRow[] ,String count[]) throws Exception {  
            Workbook wbk = null;
            String excelPath = path+File.separator+fileName+"."+fileType;
            File file = new File(excelPath);
            Sheet sheet =null;
            //1.创建工作文档对象
            if (!file.exists()) {
                if (fileType.equals("xls")) {
                    wbk = new HSSFWorkbook();
                } else if(fileType.equals("xlsx")) {
                        wbk = new XSSFWorkbook();
                } else {
                    throw new Exception("文件格式不正确");
                }
                //2.创建sheet对象
                sheet = (Sheet) wbk.createSheet("sheet1");
                OutputStream outputStream = new FileOutputStream(excelPath);
                wbk.write(outputStream);
                outputStream.flush();
                outputStream.close();
                
            } else {
                if (fileType.equals("xls")) {
                    wbk = new HSSFWorkbook();
                } else if(fileType.equals("xlsx")) {
                    wbk = new XSSFWorkbook();
                } else {
                    throw new Exception("文件格式不正确");
                }
            }
             //创建sheet对象   
            if (sheet==null) {
                sheet = (Sheet) wbk.createSheet("sheet1");
            }
            //3.创建样式对象
            CellStyle style = wbk.createCellStyle(); // 样式对象(表头)
            CellStyle style1 = wbk.createCellStyle(); // 样式对象(正文)
            
            sheet.setDisplayGridlines(false);//去除网格线
            //表头样式
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  //填充单元格
            style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);//设置单元格的背景颜色为淡蓝色
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直
            style.setAlignment(CellStyle.ALIGN_CENTER);// 水平
            style.setWrapText(true);// 指定当单元格内容显示不下时自动换行
            Font font = wbk.createFont();
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            font.setFontName("宋体");
            font.setFontHeight((short) 280);
            style.setFont(font);
    
            //正文样式
            style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直
            style1.setAlignment(CellStyle.ALIGN_CENTER);// 水平
            style1.setWrapText(true);// 指定当单元格内容显示不下时自动换行
            Font font1 = wbk.createFont();
            font1.setFontName("新宋体");
            font1.setFontHeight((short) 280);
            style1.setFont(font1);
            
    
            //4.添加表头
            Row row = sheet.createRow(1);
            Cell cell = row.createCell(1);  //创建详情 表头
            Cell cell1 = row.createCell(8); //创建统计 表头
            cell.setCellValue("网点签到数据详情");
            cell1.setCellValue("统计报表");
            row.setHeight((short) 600);
           
            //5.单元格合并  (四个参数分别是:起始行号,终止行号, 起始列号,终止列号)
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));//详情
            cell.setCellStyle(style); // 添加样式
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 8, 9));//统计
            cell1.setCellStyle(style); // 添加样式
            sheet.autoSizeColumn(5200);
            
            //6.添加附表头
            row = sheet.createRow(2);    //创建第二行
            for(int i = 0;i < titleRow.length;i++){//详情
                cell = row.createCell(i+1);
                cell.setCellValue(titleRow[i]);
                cell.setCellStyle(style);
                sheet.setColumnWidth(i+1, 20 * 256);
            }
            for(int i = 0;i < count.length;i++){//统计
                cell = row.createCell(i+8);
                cell.setCellValue(count[i]);
                cell.setCellStyle(style);
                sheet.setColumnWidth(i+8, 20 * 256);
            }
            row.setHeight((short) 540);
            
            
            //7.循环写入行数据  (数据详情)
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i+3);
                row.createCell(1).setCellValue(( list.get(i)).getTags());
                row.getCell(1).setCellStyle(style1);
                row.createCell(2).setCellValue(( list.get(i)).getTitle());
                row.getCell(2).setCellStyle(style1);
                row.createCell(3).setCellValue(( list.get(i)).getAddress());
                row.getCell(3).setCellStyle(style1);
                row.createCell(4).setCellValue(( list.get(i)).getEmpName());
                row.getCell(4).setCellStyle(style1);
                row.createCell(5).setCellValue(( list.get(i)).getCreateTime());
                row.getCell(5).setCellStyle(style1);
                if(i < 5) {//循环写入行数据  (统计报表)
                	 row.createCell(8).setCellValue(( list.get(i)).getTags());
                     row.getCell(8).setCellStyle(style1);
                     row.createCell(9).setCellValue(( list.get(i)).getTitle());
                     row.getCell(9).setCellStyle(style1);
                }
                row.setHeight((short) 500);
    
            }
            
            //8.创建文件流
            OutputStream stream = new FileOutputStream(excelPath);
            //9.写入数据
            wbk.write(stream);
            //10.关闭文件流
            stream.close();
        }
    

    最后附上 效果图
    效果图

    展开全文
  • POI设置背景色采坑记录

    千次阅读 2018-07-03 16:35:53
    需求:POI做Excel导出的时候为单元格设置背景色 我先使用了xField2FontYellow.setFillBackgroundColor(HSSFColor.YELLOW.index); 但是无论如何都不起作用,后来发现POI是需要设置前景色(目前还不清楚POI什么时候使用...

    需求:POI做Excel导出的时候为单元格设置背景色

    我先使用了xField2FontYellow.setFillBackgroundColor(HSSFColor.YELLOW.index);
    但是无论如何都不起作用,后来发现POI是需要设置前景色(目前还不清楚POI什么时候使用背景色和前景色)

    xField2FontYellow.setFillForegroundColor(HSSFColor.YELLOW.index);

    但是也不起作用,后来又发现需要设置填充模式,这里我们设置为全填充

     xField2FontYellow.setFillForegroundColor(HSSFColor.YELLOW.index);
     xField2FontYellow.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    展开全文
  • poi设置背景颜色不起作用setFillForegroundColor

    千次阅读 多人点赞 2019-11-12 16:52:59
    项目中遇到一个使用POI导出excel奇怪的问题,设置poi背景颜色不起作用。 解决办法: 设置setFillForegroundColor还需要设置一下setFillPattern,如果只设置背景颜色是不起作用的,太坑了。style.setFillPattern...
  • 关于POI设置背景色的问题

    千次阅读 2013-03-25 15:26:04
    网上查了点资料,POI设置背景色必须与另外一个设置结合起来 headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);   headStyle.setFillForegroundColor(HSSFColor.RED.index); 就可以了!
  • POI设置Cell背景色编码与实际颜色对照表
  • POI 设置单元格背景色

    2017-10-14 11:29:04
    POI 设置单元格背景色背景色编码与实际颜色对照表 转载 2015年06月27日 16:29:58 标签: </ul
  • POI 设置Excel单元格背景色前言一、冲~~~1.代码实现2.颜色对应3.填充对应总结 前言 POI 设置单元格背景颜色 一、冲~~~ 1.代码实现 注: 本人用的是目前最新的依赖 4.1.2 代码如下(示例): HSSFWorkbook ...
  • POI 设置单元格背景颜色

    千次阅读 2014-09-25 16:51:47
    POI 设置单元格背景颜色 博客分类:  apache   This example shows you Excel cell fills and colors using Apache POI. In our example i have used all the possible colors and set ...
  • java使用poi如何给单个单元格设置背景颜色,我已做过判断行和列都不行,都会是整个excel表格都有背景颜色,求好心人解答呀
  • HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setFillForegroundColor(IndexedColors.CORNFLOWER...// 设置背景色 style.setFillPattern
  • This example shows you Excel cell fills and colors using Apache POI. In our example i have used all the possible colors and set it as Fills background colors of cells. Below is the example code.
  • This example shows you Excel cell fills and colors using Apache POI. In our example i have used all the possible colors and set it as Fills background colors of cells. Below is the example code.
  • import java.io.FileOutputStream; import java.io.IOException;...import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; im
  • Java使用poi可以很方便地导出Excel表格文件,同时可以设置各种样式,例如背景颜色。下面给出两个例子设置背景颜色,一个使用自带的颜色设置背景颜色,一个使用自定义颜色设置。 使用内置的颜色设置单元格背景颜色 ...
  • import java.io.FileOutputStream; import java.io.IOException; ...import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; ...
  • poi设置Excel单元格边框 和 背景色,java设置Excel 单元格边框 和 背景色 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel...
  • 1、pom.xml <dependency> <...org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dep...
  • POI设置单元格背景色

    千次阅读 2017-12-05 18:08:12
    该篇文章为转载,原文地址:POI操作Excel:cell的背景颜色类型 POI背景颜色设置方法如下: HSSFCellStyle style = workbook.createCellStyle(); style.setFillBackgroundColor(HSSFColor.AQU

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,837
精华内容 1,934
关键字:

poi设置背景颜色