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

时间:2022-10-25 07:34:39

I have googled some code and found some answers but not able to get my excel file output in Bold and set background color. I have tried with following code. can you please tell me where am I going wrong? Please take a look. Thanks.

我用Google搜索了一些代码并找到了一些答案,但无法以粗体显示我的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.

仅供参考:我打算用BOLD蓝色或任何浅色背景制作第一排。如果您知道请帮助代码。

// 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");

2 个解决方案

#1


4  

You are doing wrong in the following:

您在以下方面做错了:

1- You are not setting any Background color;

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

2- When you create the new cells they override the row style, so you need to set the style for each new cell you create;

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

Below is the working code:

以下是工作代码:

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

file output:

文件输出:

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

#2


3  

First remove font.setBoldweight(HSSFFont.COLOR_NORMAL);. COLOR_NORMAL is not really a Boldweight, but a color. font.setBold(true); should be sufficient to bold your text.

首先删除font.setBoldweight(HSSFFont.COLOR_NORMAL);. COLOR_NORMAL不是真正的Boldweight,而是一种颜色。 font.setBold(真);应该足以加粗你的文字。

You also need to add some properties to your style to get the background color. This is the confusing part. The approach is to use a Fill. Fills have Foreground and Background colors distinct from the rest of the cell. To get a solid fill, you need to use:

您还需要在样式中添加一些属性以获取背景颜色。这是令人困惑的部分。方法是使用填充。填充具有与单元格其余部分不同的前景色和背景色。要获得实体填充,您需要使用:

style.setForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

Notice that this sets the foreground color of the fill to light blue. Since the fill has a solid foreground, that is all you need. If you chose one of the pattern fills, you would need to set the background color for the fill as well. The fill itself is the cell background.

请注意,这会将填充的前景色设置为浅蓝色。由于填充具有坚实的前景,这就是您所需要的。如果您选择了其中一个图案填充,则还需要设置填充的背景颜色。填充本身就是单元格背景。

#1


4  

You are doing wrong in the following:

您在以下方面做错了:

1- You are not setting any Background color;

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

2- When you create the new cells they override the row style, so you need to set the style for each new cell you create;

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

Below is the working code:

以下是工作代码:

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

file output:

文件输出:

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

#2


3  

First remove font.setBoldweight(HSSFFont.COLOR_NORMAL);. COLOR_NORMAL is not really a Boldweight, but a color. font.setBold(true); should be sufficient to bold your text.

首先删除font.setBoldweight(HSSFFont.COLOR_NORMAL);. COLOR_NORMAL不是真正的Boldweight,而是一种颜色。 font.setBold(真);应该足以加粗你的文字。

You also need to add some properties to your style to get the background color. This is the confusing part. The approach is to use a Fill. Fills have Foreground and Background colors distinct from the rest of the cell. To get a solid fill, you need to use:

您还需要在样式中添加一些属性以获取背景颜色。这是令人困惑的部分。方法是使用填充。填充具有与单元格其余部分不同的前景色和背景色。要获得实体填充,您需要使用:

style.setForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

Notice that this sets the foreground color of the fill to light blue. Since the fill has a solid foreground, that is all you need. If you chose one of the pattern fills, you would need to set the background color for the fill as well. The fill itself is the cell background.

请注意,这会将填充的前景色设置为浅蓝色。由于填充具有坚实的前景,这就是您所需要的。如果您选择了其中一个图案填充,则还需要设置填充的背景颜色。填充本身就是单元格背景。