如何使用apache poi在同一个excel表单元中更改特定的文本颜色?

时间:2021-01-09 20:20:27

Does anyone know how to change the color of the particular text of a cell in excel. I am using apache poi and I could find out to change the text color of entire cell. But I want only a particular text.

有没有人知道如何在excel中更改单元格特定文本的颜色?我正在使用apache poi,我可以找到更改整个单元格的文本颜色的方法。但我只想要一个特定的文本。

Eg: Cell A1 has Hello World I want "Hello" to be in blue and "World" to be in green. How do I do this?

单元A1有Hello World,我想用“Hello”来表示蓝色,“World”表示绿色。我该怎么做呢?

2 个解决方案

#1


11  

The key is using the HSSFRichTextString object to set the value of the cell. This object has an applyFont method which accepts a startingIndex, endingIndex and a Font. Thus, you can create fonts having the colors you want, then apply them to parts of the cell value using applyFont().

关键是使用HSSFRichTextString对象来设置单元格的值。这个对象有一个applyFont方法,它接受一个startingIndex、endingIndex和一个字体。因此,您可以创建具有所需颜色的字体,然后使用applyFont()将它们应用到单元格值的部分。

Here is some example code I cobbled together (completely untested):

下面是我拼凑的一些示例代码(完全未经测试):

// Set up a rudimentary worksheet with a cell in it
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“sheet1”);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);

// Set up fonts
HSSFFont blueFont = workbook.createFont();
blueFont.setColor(HSSFColor.BLUE.index);

HSSFFont greenFont = workbook.createFont();
greenFont.setColor(HSSFColor.GREEN.index);

// create a cell style and assign the first font to it
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(blueFont);

// assign the style to the cell
cell.setCellStyle(style);

// override the parts of the text that you want to
// color differently by applying a different font.
HSSFRichTextString richString = new HSSFRichTextString("Hello, World!");
richString.applyFont(6, 13, greenFont);
cell.setCellValue(richString);

#2


-1  

At first create a style

首先创建一个样式

//////////////////////Excel Header Style/////////////////////////   
        HSSFCellStyle headerlabelcs = wb.createCellStyle();
        headerlabelcs.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        headerlabelcs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerlabelcs.setBorderLeft((short)1);
        headerlabelcs.setBorderRight((short)1);

        HSSFFont headerlabelfont = wb.createFont();
        headerlabelfont.setFontHeightInPoints((short)12);
        headerlabelfont.setFontName("Calibri");
        headerlabelfont.setColor(HSSFColor.BLACK.index);
        headerlabelfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerlabelcs.setFont(headerlabelfont); 
                //////////////////////Excel Header Style/////////////////////////   

add then this line will be added in your code

然后在代码中添加这一行

sheet.getRow(rowIndex).getCell(0).setCellStyle(headerlabelcs);

#1


11  

The key is using the HSSFRichTextString object to set the value of the cell. This object has an applyFont method which accepts a startingIndex, endingIndex and a Font. Thus, you can create fonts having the colors you want, then apply them to parts of the cell value using applyFont().

关键是使用HSSFRichTextString对象来设置单元格的值。这个对象有一个applyFont方法,它接受一个startingIndex、endingIndex和一个字体。因此,您可以创建具有所需颜色的字体,然后使用applyFont()将它们应用到单元格值的部分。

Here is some example code I cobbled together (completely untested):

下面是我拼凑的一些示例代码(完全未经测试):

// Set up a rudimentary worksheet with a cell in it
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“sheet1”);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);

// Set up fonts
HSSFFont blueFont = workbook.createFont();
blueFont.setColor(HSSFColor.BLUE.index);

HSSFFont greenFont = workbook.createFont();
greenFont.setColor(HSSFColor.GREEN.index);

// create a cell style and assign the first font to it
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(blueFont);

// assign the style to the cell
cell.setCellStyle(style);

// override the parts of the text that you want to
// color differently by applying a different font.
HSSFRichTextString richString = new HSSFRichTextString("Hello, World!");
richString.applyFont(6, 13, greenFont);
cell.setCellValue(richString);

#2


-1  

At first create a style

首先创建一个样式

//////////////////////Excel Header Style/////////////////////////   
        HSSFCellStyle headerlabelcs = wb.createCellStyle();
        headerlabelcs.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        headerlabelcs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerlabelcs.setBorderLeft((short)1);
        headerlabelcs.setBorderRight((short)1);

        HSSFFont headerlabelfont = wb.createFont();
        headerlabelfont.setFontHeightInPoints((short)12);
        headerlabelfont.setFontName("Calibri");
        headerlabelfont.setColor(HSSFColor.BLACK.index);
        headerlabelfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerlabelcs.setFont(headerlabelfont); 
                //////////////////////Excel Header Style/////////////////////////   

add then this line will be added in your code

然后在代码中添加这一行

sheet.getRow(rowIndex).getCell(0).setCellStyle(headerlabelcs);