如何使用JAVA为Excel标题文本设置字体颜色

huangapple go评论80阅读模式
英文:

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代码将标题文本设置为红色?

如何使用JAVA为Excel标题文本设置字体颜色

英文:

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.

如何使用JAVA为Excel标题文本设置字体颜色

答案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 coloris 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.

huangapple
  • 本文由 发表于 2020年7月23日 21:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定