英文:
How to set font color for Excel header text using JAVA
问题
我正在尝试使用Apache POI设置Excel工作表标题文本的字体颜色,如附加的屏幕截图所示。我的代码是:
Footer footer = sheet.getFooter();
footer.setLeft(HSSFHeader.font(footerFontName, "Regular") +
HSSFHeader.fontSize((short) footerFontSize) +
footerInfo);
请问如何使用JAVA代码将标题文本设置为红色?
英文:
I am trying to set a font color of header text of Excel sheet using Apache POI. As attached screenshot. My code is,
Footer footer = sheet.getFooter();
footer.setLeft(HSSFHeader.font(footerFontName, "Regular") +
HSSFHeader.fontSize((short) footerFontSize) +
footerInfo);
How to set header text in red color using JAVA Code.
答案1
得分: 0
Excel
提供了特殊代码来格式化页眉和页脚。详见页眉和页脚的格式设置和 VBA 代码。提到的文本 &...
在页眉和页脚中有特殊含义。它们不会被打印出来,而是用于确定格式。因此,最简单的方法是使用这些代码。注意 &color
需要变成 &Kcolor
,其中 color
是一个十六进制值。
示例:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class CreateExcelHeaderText {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
String filePath = "./CreateExcelHeaderText.xlsx";
//Workbook workbook = new HSSFWorkbook();
//String filePath = "./CreateExcelHeaderText.xls";
Sheet sheet = workbook.createSheet();
Header header = sheet.getHeader();
header.setCenter("&C&KFF0000&24CENTER HEADER"); // &C = 居中文本; &KFF0000 = 红色字体; &24 = 字体大小24pt
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
这会导致一个居中的页眉,红色字体颜色,字体大小为24pt。
其中一些代码也可以使用 HeaderFooter 进行设置。但是 &K...
直到目前为止在其中不受支持。
英文:
Excel
provides special codes to format headers and footers. See Formatting and VBA codes for headers and footers. The mentioned texts &...
have special meaning in headers and footers. They are not printed but used to determine the formats. So simplest way is using those codes. Note &color
needs to be &Kcolor
where color
is a hexadecimal value.
Example:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class CreateExcelHeaderText {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook(); String filePath = "./CreateExcelHeaderText.xlsx";
//Workbook workbook = new HSSFWorkbook(); String filePath = "./CreateExcelHeaderText.xls";
Sheet sheet = workbook.createSheet();
Header header = sheet.getHeader();
header.setCenter("&C&KFF0000&24CENTER HEADER"); // &C = text centered; &KFF0000 = font color red; &24 = font size 24pt
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
This leads to a centered header in red font color and font size 24pt.
Some of those codes also can be set using HeaderFooter. But &K...
is not supported there until now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论