【发布时间】:2013-05-07 22:38:42
【问题描述】:
XSSFTextBox 和 XSSFRichTextField 的
编辑见评论; Apache POI 似乎是种族主义者,不支持黑人?红色是?
编辑 2:在字体中使用常量而不是 XSSFColor,它们有红色和黑色;查看默认字体(只是为了清楚代码现在正确显示颜色和大小,但字体名称/实际字体仍然是错误的。所以 Font.COLOR_NORMAL 适用于 dat black)
由于某种原因,我无法让我的 XSSFTextBoxs 文本的字体和字体颜色从它们(我假设)默认的 Calibri 白色(为什么默认是白色?!?)。我改变了它们的大小,但它们的字体和字体颜色保持默认值。我发现这是以前的错误,应该修复here。
我一直在查看this 对我如何更改字体的基础的引用,看起来它就是这样做的,但它似乎不起作用;我希望对此有另一种看法,还有其他一些小问题我仍在摆弄,但字体实现是目前最重要的事情;欢迎提出任何批评意见!
真正让我感到厌烦的是,我以前非常广泛地将 XSSFFont 与 XSSFCellStlyes 一起使用,并且在更改字体或颜色时从未遇到任何问题,更不用说其他任何问题了,所以我不知道这是不是我的一些奇怪行为'没有看到,或者我在这里做错了什么。
代码
package excelhandling;
import java.awt.Desktop;
import java.io.*;
import javax.swing.JOptionPane;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
/**Unit Tests<br>To test random things to figure them out so I can implement them
* in the real code later; ideal is to test kinks here so whole application
* doesn't have to be loaded over and over to figure small issues out (living the dream)
*
* @author Sean Newell
*/
public class UnitTests
{
static String fileName = "TestWorkbook.xlsx";
public static void main(String[] args)
{
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sht = wb.createSheet();
File file = new File(fileName);
int colStart = 5;
XSSFDrawing draw = sht.createDrawingPatriarch();
XSSFShapeGroup group = draw.createGroup(draw.createAnchor(0, 0, 0, 0, colStart, 11, colStart + 6, 11+7));
group.setCoordinates(colStart, 11, colStart + 6, 11+7);
XSSFTextBox tb1 = group.createTextbox(new XSSFChildAnchor(0, 0, 6, 7));
tb1.setShapeType(ShapeTypes.RECT);
tb1.setNoFill(false);
tb1.setFillColor(255, 255, 255); //This causes excel to repair xml - don't know why;
//following message from Excel:
//Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)
//Log:
// <?xml version="1.0" encoding="UTF-8" standalone="true"?>
// -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
// <logFileName>error094800_07.xml</logFileName>
// <summary>Errors were detected in file 'H:\My Documents\NetBeansProjects\TemplateBuilder\TestWorkbook.xlsx'</summary>
// -<repairedRecords summary="Following is a list of repairs:">
// <repairedRecord>Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)</repairedRecord>
// </repairedRecords>
// </recoveryLog>
XSSFRichTextString address = new XSSFRichTextString("TextBox string 1\nHas three\nLines to it");
XSSFFont arial10 = wb.createFont();
arial10.setFontName("Arial"); // Doesn't seem to work
arial10.setFontHeight(10);
arial10.setColor(new XSSFColor(java.awt.Color.BLACK)); // Doesn't seem to work
address.applyFont(arial10); // Possible problem?
tb1.setText(address);
tb1.setLineStyleColor(0, 0, 0);
tb1.setLineWidth(2);
XSSFTextBox tb2 = group.createTextbox(new XSSFChildAnchor(0, 7, 10, 13));
tb2.setShapeType(ShapeTypes.RECT);
tb2.setNoFill(false);
tb2.setFillColor(254, 254, 254); //This causes excel to repair xml - don't know why
XSSFRichTextString secret = new XSSFRichTextString("A single-line thing that has like, a lot of text, but can wrap and be smaller, like, totally.");
XSSFFont arial8 = wb.createFont();
arial8.setFontName("Arial"); // Doesn't seem to work
arial8.setFontHeight(8);
arial8.setColor(new XSSFColor(java.awt.Color.BLACK)); // Doesn't seem to work
secret.applyFont(arial8); // Possible problem?
tb2.setText(secret);
tb2.setLineStyleColor(0, 0, 0);
tb2.setLineWidth(2);
try {
FileOutputStream fout;
fout = new FileOutputStream(file);
wb.write(fout);
fout.close();
JOptionPane.showConfirmDialog(null, "Excel sheet written");
Desktop.getDesktop().open(file);
} catch (IOException exc) {
JOptionPane.showInputDialog("Please close other instances of excel that have " + fileName + " open");
}
}
}
【问题讨论】:
-
我把颜色改成红色,文字改成红色:|似乎不支持黑色。新种族主义?可能是。去试试看看支持什么字体,支持什么颜色。
-
我已经有一段时间没有使用 poi 了。至少对于旧的 .xls 格式,颜色被限制在文档的调色板中。使用其中一个常量(例如 BLACK)并不总是指代黑色,而是指调色板中默认调色板中为黑色的插槽。如果您更改了调色板,BLACK 指的是该插槽中的任何颜色。
-
我猜是有道理的;你所看到的就是我在这个测试类中使用的所有东西——所以我没有明确地改变它;您认为调色板就像您从 getStylesSource() 获得的样式表吗? Link
标签: java apache-poi xssf