程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Java Apache Poi,如何同时设置背景颜色和边框大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到Java Apache Poi,如何同时设置背景颜色和边框的问题如何解决?下面主要结合日常开发的经验,给出你关于Java Apache Poi,如何同时设置背景颜色和边框的解决方法建议,希望对你解决Java Apache Poi,如何同时设置背景颜色和边框有所启发或帮助;

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

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

解决方法

一开始,我想说我是开发人员领域的新手。

我试图生成一个Excel工作表,其中包含带边框的Mutiplication表并设置背景颜色,但仅用于第一列和第一行。

这是一个正确的例子:正确的例子

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

请向我解释如何同时设置背景颜色和边框。

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

大佬总结

以上是大佬教程为你收集整理的Java Apache Poi,如何同时设置背景颜色和边框全部内容,希望文章能够帮你解决Java Apache Poi,如何同时设置背景颜色和边框所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: