XSSFTextBox 和 XSSFRichTextField 的
编辑见评论;Apache POI 似乎是种族主义者,不支持黑人?红色是?
编辑 2:在字体中使用常量而不是使用 XSSFColor,它们有红色和黑色;查看默认字体(只是为了清楚代码现在正确显示颜色和大小,但字体名称/实际字体仍然是错误的。所以 Font.COLOR_NORMAL 适用于 dat black)
出于某种原因,我无法让我的 XSSFTextBoxs 文本的字体和字体颜色从它们(我假设)默认的 Calibri 白色(为什么默认为白色?!?)。我改变了它们的大小,但它们的字体和字体颜色保持默认值。我发现这是以前的错误,应该在这里修复。
我一直在查看此参考资料,以了解我如何更改字体,看起来它就是这样做的,但它似乎不起作用;我希望对此有另一种看法,还有其他一些小问题我仍在摆弄,但字体实现是目前最重要的事情;欢迎和通缉任何批评!
真正让我恼火的是,我之前使用过 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");
}
}
}