有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Apache Poi,如何同时设置背景颜色和边框

一开始,我想说我是一个全新的开发者

我试图生成一个excel表格,其中包含带边框的复制表,并设置背景色,但只针对第一列和第二行

下面是一个正确的例子:correct example

我写了类似的东西,但在结果文件中,彩色单元格并没有边框:(

请告诉我如何同时设置背景色和边框。


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();
    }
}

共 (4) 个答案

  1. # 1 楼答案

    你真正的问题是你有两种风格,一种叫背景风格,另一种叫边界风格。然后将两种样式应用于同一单元格,但一个单元格只能有一种样式,因此不是添加第二种样式,而是用第二种样式覆盖第一种样式

    而不是:

        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());
    

    只需创建一个这样的样式:

        CellStyle backgroundStyle = workbook.createCellStyle();
    
        backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        backgroundStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    
        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());
    

    然后将其应用于您的手机:

        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellStyle(backgroundStyle);
    

    注意:如其他答案所述,FillPattern=SOLID\u前景的背景色被忽略,您必须为该图案设置前景色。这可能会令人困惑,因为您正试图将单元格背景设置为纯色。但是cell backgroundbackground color不同Cell backgroundFill Pattern相同,它有两种颜色a Foreground Color和a Background Color。这两种颜色基于所选的特定Fill Pattern显示。SOLID_FOREGROUND填充只使用Foreground Color

  2. # 2 楼答案

    我找到了解决这个问题的一种方法,但我几乎可以肯定还有其他较短的方法

    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.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");
    
            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);
    
                    if (cell.getRowIndex() == 0 || cell.getColumnIndex() == 0) {
                        CellStyle Style = workbook.createCellStyle();
    
                        Style.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
                        Style.setFillPattern(CellStyle.BIG_SPOTS);
                        Style.setBorderBottom(CellStyle.BORDER_THIN);
                        Style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderLeft(CellStyle.BORDER_THIN);
                        Style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderRight(CellStyle.BORDER_THIN);
                        Style.setRightBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderTop(CellStyle.BORDER_THIN);
                        Style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    
                        cell.setCellStyle(Style);
                    } else {
                        CellStyle Style = workbook.createCellStyle();
    
                        Style.setBorderBottom(CellStyle.BORDER_THIN);
                        Style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderLeft(CellStyle.BORDER_THIN);
                        Style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderRight(CellStyle.BORDER_THIN);
                        Style.setRightBorderColor(IndexedColors.BLACK.getIndex());
                        Style.setBorderTop(CellStyle.BORDER_THIN);
                        Style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    
                        cell.setCellStyle(Style);
                 }
                }
            }
    
            FileOutputStream out = new FileOutputStream(fileName);
            workbook.write(out);
            out.close();
      }
    }
    
  3. # 3 楼答案

    backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());更改为

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

    您可以按如下方式设置边框:

            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());
    

    这将根据需要为您提供黄色和边框

  4. # 4 楼答案

    从POI 3开始。x、 单元格填充颜色设置如下:

    CellStyle cs = wb.createCellStyle();
    cs.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);