【问题标题】:How to change font on XSSFTextBox如何更改 XSSFTextBox 上的字体
【发布时间】: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


【解决方案1】:

这很粗略,但是在 poi 3.9 中,似乎只有字体系列而不是字体名称是从 XSSFSimpleShape.applyAttributes 中的 XSSFFont 对象复制而来的。 此外,我只能通过XSSFColor.setColor(HSSFColor.&lt;color&gt;.index) 设置索引颜色 - 也许HSSF custom color palette 有 XSSF 挂件,但我认为下面的方法更直接......

(使用 Libre Office 4.0、MS Excel Viewer、MS Excel 2003 与兼容包测试...)

import java.awt.Color;
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;

public class XlsColors {

    static String fileName = "TestWorkbook.xlsx";

    public static void main(String[] args) throws Exception {
        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.setLineStyleColor(0, 0, 0);
        tb1.setLineWidth(2);
        Color col = Color.orange;
        tb1.setFillColor(col.getRed(), col.getGreen(), col.getBlue());

        XSSFRichTextString address = new XSSFRichTextString("TextBox string 1\nHas three\nLines to it");
        tb1.setText(address);        
        CTTextCharacterProperties rpr = tb1.getCTShape().getTxBody().getPArray(0).getRArray(0).getRPr();
        rpr.addNewLatin().setTypeface("Trebuchet MS");
        rpr.setSz(900); // 9 pt
        col = Color.pink;
        rpr.addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)col.getRed(),(byte)col.getGreen(),(byte)col.getBlue()});

        FileOutputStream fout = new FileOutputStream(file);
        wb.write(fout);
        fout.close();
    }
}

【讨论】:

  • 我不知道您是否在 libre office/Excel 查看器中收到相同的消息,但在 Microsoft Excel 2010 中,在 excel Excel 中打开文件时调用 rpr.addNewSolidFill()... 调用再次进行 xml 修复;它似乎也没有在文本框的背景中添加填充。测试了 Color.Pink 和 Color.White 都没有工作,所以我又在 XSSFTextBox 对象上使用 setFillColor;虽然仍然可以修复 xml。
  • 颜色用于文本本身,而不是背景。希望我明天有时间在工作中使用 Excel 2003 进行检查...
  • MS Excel 2003 兼容包下测试正常,即没有警告。此外,我还添加了背景颜色...不适合眼睛,但它可以工作;)
猜你喜欢
  • 2014-01-11
  • 2011-02-22
  • 1970-01-01
  • 2011-11-08
  • 2020-01-21
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多