gpt4 book ai didi

具有背景颜色的粗体文本样式的 excel 行的 Java 代码

转载 作者:搜寻专家 更新时间:2023-11-01 01:41:13 24 4
gpt4 key购买 nike

我用谷歌搜索了一些代码并找到了一些答案,但无法以粗体显示我的 excel 文件输出并设置背景颜色。我尝试使用以下代码。你能告诉我我哪里错了吗?请看一下。谢谢。

FYI: I am going to make 1st Row in BOLD with blue or any Light color background. If you know please help with the code.

// Excel file generation code
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(true);
font.setColor(HSSFColor.DARK_BLUE.index);

style.setFont(font);
// Freeze 1st Row
sheet.createFreezePane(0, 1);

HSSFRow row = sheet.createRow(1);
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.setRowStyle(style);

rowhead.createCell(0).setCellValue("RUN");
rowhead.createCell(1).setCellValue("NUMBER");

最佳答案

您在以下方面做错了:

1- 您没有设置任何背景颜色;

2- 当您创建新单元格时,它们会覆盖行样式,因此您需要设置您创建的每个新单元格的样式;

下面是工作代码:

FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(true);
font.setColor(HSSFColor.DARK_BLUE.index);

style.setFont(font);
//Add these lines
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

sheet.createFreezePane(0, 1); // Freeze 1st Row sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)


HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.setRowStyle(style);
//Set the cell0 Style
HSSFCell cell0 = rowhead.createCell(0);
cell0.setCellStyle(style);
cell0.setCellValue("ROW");
//Set the cell1 Style
HSSFCell cell1 = rowhead.createCell(1);
cell1.setCellStyle(style);
cell1.setCellValue("NUMBER");

workbook.write(fileOut);

文件输出:

result

关于具有背景颜色的粗体文本样式的 excel 行的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37188540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com