设置JXL自定义字体

5
如何在JXL中添加自定义字体?除了默认提供的字体之外?
public static final FontName ARIAL = new FontName("Arial");
public static final FontName TIMES = new FontName("Times New Roman");
public static final FontName COURIER = new FontName("Courier New");
public static final FontName TAHOMA = new FontName("Tahoma");
< p > FontName 类似乎是在 WritableFont 类内部的一个 private static 内部类。除了那里提到的字体,我如何添加其他字体?

敬礼, A Y。

2个回答

1
由于FontName构造函数是私有的,我们无法直接实例化一个新的FontName。相反,我们必须使用WritableFont.createFont。这是一个工厂方法,用于创建由提供的字体名称指定的字体。
请注意以下内容:
引用:

此方法应谨慎使用,因为用于创建字体名称的字符串必须被Excel内部处理识别。


要创建 Trebuchet MS 字体,您只需调用静态工厂方法。
public static final FontName TREBUCHET_MS = WritableFont.createFont("Trebuchet MS");

我已经创建了一个简单的字体工厂API,用于创建下面的WritableFont对象:

FontCreator.java

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.excel.parse.PathResolver;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.RGB;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableFont.FontName;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class FontCreator {
    // ========================================================================
    // Private Utilities
    // ========================================================================
    private static final Map<Integer, Colour> colorValueMap;
    private static final Map<String, Colour> colorNameMap;

    static {
        colorValueMap = new HashMap<Integer, Colour>();
        colorNameMap = new HashMap<String, Colour>();

        for (Colour color : Colour.getAllColours()) {
            RGB rgb = color.getDefaultRGB();
            int valueKey = (rgb.getRed() << 16) + (rgb.getGreen() << 8) + rgb.getBlue();
            String nameKey = color.getDescription();

            colorValueMap.put(valueKey, color);
            colorNameMap.put(nameKey, color);
        }
    }

    // ========================================================================
    // Global Values
    // ========================================================================
    public static final WritableFont TREBUCHET_MS = create("Trebuchet MS");
    public static final WritableFont CONSOLAS = create("Consolas", 9, "ocean blue", true, false, 0);

    public static final int NO_UNDERLINE = 0x0;
    public static final int SINGLE = 0x1;
    public static final int DOUBLE = 0x2;
    public static final int SINGLE_ACCOUNTING = 0x21;
    public static final int DOUBLE_ACCOUNTING = 0x22;

    public static void main(String[] args) {
        try {
            File exlFile = new File(PathResolver.resolve("${userprofile}/documents/excel-font-test.xls"));
            WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile);
            WritableSheet writableSheet = writableWorkbook.createSheet("Sheet1", 0);
            WritableCellFormat cellFormat = new WritableCellFormat(FontCreator.CONSOLAS);

            WritableCell label = new Label(0, 0, "This is a test.", cellFormat);

            writableSheet.addCell(label);

            writableWorkbook.write();
            writableWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    public static WritableFont create(String name, int size, Colour color, boolean bold, boolean italic,
            int underline) {
        UnderlineStyle underlineStyle = UnderlineStyle.getStyle(underline);
        FontName font = WritableFont.createFont(name);

        if (bold) {
            return new WritableFont(font, size, WritableFont.BOLD, italic, underlineStyle, color);
        } else {
            return new WritableFont(font, size, WritableFont.NO_BOLD, italic, underlineStyle, color);
        }
    }

    public static WritableFont create(String name, int size, int color, boolean bold, boolean italic, int underline) {
        return create(name, size, lookupColor(color), bold, italic, underline);
    }

    public static WritableFont create(String name, int size, String color, boolean bold, boolean italic,
            int underline) {
        return create(name, size, lookupColor(color.toLowerCase()), bold, italic, underline);
    }

    public static WritableFont create(String fontName, int size, int color) {
        return create(fontName, size, color, false, false, NO_UNDERLINE);
    }

    public static WritableFont create(String fontName, int size, String color) {
        return create(fontName, size, color, false, false, NO_UNDERLINE);
    }

    public static WritableFont create(String fontName, int size) {
        return create(fontName, size, 0x000000);
    }

    public static WritableFont create(String fontName) {
        return create(fontName, WritableFont.DEFAULT_POINT_SIZE);
    }

    public static Colour lookupColor(int value) {
        return colorValueMap.containsKey(value) ? colorValueMap.get(value) : Colour.AUTOMATIC;
    }

    public static Colour lookupColor(String value) {
        return colorNameMap.containsKey(value) ? colorNameMap.get(value) : Colour.AUTOMATIC;
    }
}

PathResolver.java

你可以忽略这个文件,它只用于解决上面示例中的路径("${userprofile}/documents/excel-font-test.xls")。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PathResolver {
    private static final Pattern envVarRegex;

    static {
        String envVar = "[\\w\\(\\)]+";
        String expression = "\\$\\{(" + envVar + "+)\\}|\\$(" + envVar + ")";

        envVarRegex = Pattern.compile(expression);
    }

    public static String resolve(String path) {
        if (path == null) {
            return null;
        }

        Matcher m = envVarRegex.matcher(path);
        StringBuffer sb = new StringBuffer();

        while (m.find()) {
            String envVar = m.group(0);
            String envVarName = null == m.group(1) ? m.group(2) : m.group(1);

            m.appendReplacement(sb, resolveEnvVar(envVar, envVarName));
        }

        return m.appendTail(sb).toString();
    }

    private static String resolveEnvVar(String envVar, String name) {
        try {
            return Matcher.quoteReplacement(System.getenv(name));
        } catch (NullPointerException e) {
            System.err.println("Warning: Environment variable does no exist: " + name);
        }
        return Matcher.quoteReplacement(envVar);
    }
}

0
 WritableFont(WritableFont.FontName fn, int ps)
      Constructs of font of the specified face and of size given by the specified point size

请点击这里


谢谢你的回复,Rachel。我想自定义WritableFont,它提供了5-6种字体选项,但我想要其他不在其中的字体。 - varunrao321
你不能用FontName指定一个字体吗? - Rachel Gallen
看看这个 Pastebin,它将其自定义为 Trebuchet,我相信你可以学到一些技巧。http://ja.pastebin.ca/raw/2304382 - Rachel Gallen
@RachelGallen:你的pastebin链接已经过期了,为什么不把文本添加到这篇帖子中呢?:( - Mr. Polywhirl
@Mr.Polywhirl 当然过期了,它是3年前创建的!我当时刚加入stackoverflow,要么是太长无法粘贴,要么是我不知道更好的方法。我相信你知道如何使用谷歌。搜索一下,你会找到答案的!!! - Rachel Gallen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接