如何在XSSFTextBox上更改字体

时间:2013-05-13 19:36:20

标签: java apache-poi xssf

XSSFTextBox和XSSFRichTextField的

编辑看评论; Apache POI似乎是种族主义者,不支持黑色?红色是?

编辑2:而不是使用XSSFColor在Font中使用常量,它们有红色和黑色;查看默认字体(现在只是为了清除代码正确显示颜色和大小,但字体名称/实际字体仍然是错误的。所以Font.COLOR_NORMAL适用于dat黑色)

出于某种原因,我不能让我的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");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

(使用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();
    }
}