简体   繁体   English

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

[英]How to change the specific text color in a same cell of excel sheet using apache poi?

Does anyone know how to change the color of the particular text of a cell in excel. 有谁知道如何更改excel中单元格的特定文本的颜色。 I am using apache poi and I could find out to change the text color of entire cell. 我正在使用apache poi,我可以找到改变整个单元格的文本颜色。 But I want only a particular text. 但我只想要一个特定的文字。

Eg: Cell A1 has Hello World I want "Hello" to be in blue and "World" to be in green. 例如:Cell A1有Hello World我希望“Hello”为蓝色,“World”为绿色。 How do I do this? 我该怎么做呢?

The key is using the HSSFRichTextString object to set the value of the cell. 关键是使用HSSFRichTextString对象来设置单元格的值。 This object has an applyFont method which accepts a startingIndex, endingIndex and a Font. 该对象有一个applyFont方法,它接受startingIndex,endingIndex和Font。 Thus, you can create fonts having the colors you want, then apply them to parts of the cell value using applyFont(). 因此,您可以创建具有所需颜色的字体,然后使用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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Apache POI与另一个Excel比较后如何更改源Excel工作表的单元格颜色 - How to change cell color of source excel sheet after comparison with another excel using Apache POI 如何使用 Apache POI 从 excel 工作表中搜索和打印特定单元格? - How do i search and print a specific cell from an excel sheet using Apache POI? 如何使用 Apache POI 为 Excel 工作表中的行应用背景颜色? - How to apply background color for the rows in excel sheet using Apache POI? 如何使用Apache POI在Excel工作表中搜索特定日期? - How to search an Excel sheet for a specific date using Apache POI? 如何使用Apache POI获得Excel单元格字体颜色RGB? - How to get Excel cell font color RGB using Apache POI? 如何在不使用段落和运行的情况下更改特定表单元上的Apache POI Word文档中的字体颜色? - How to change font color in Apache POI word document on a specific table cell without using paragraph and run? 在Excel中用相同的颜色填充整个工作表-Apache POI - Fill whole sheet with the same color in Excel - Apache POI 在Java中使用Apache Poi的Excel工作表合并单元格读取 - Excel sheet merged cell reading using apache poi in java 如何在 Apache POI 中制作带有“逗号样式”单元格的 excel 工作表? - How to make excel sheet with "comma style" cell in Apache POI? 如何使用Apache POI从excel工作表的单元格中读取外来字符,并使用SQLITE将它们存储在数据库中? - How to read foreign characters from a cell in excel sheet using Apache POI and store them in database using SQLITE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM