2013-02-04 91 views
3

如何在JXL中添加自定義字體?除了默認情況下可用?設置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"); 

FontName類似乎是WritableFont類的內部的privatestatic內部類。我怎樣才能在這裏提到的字體之外添加字體?

問候, 一個Y.

回答

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

看到here

+0

感謝您的回覆Rachel,我想自定義WritableFont,WritableFont提供5-6字體選項,但我想要其他字體不在那裏。 – ayachama

+0

你不能指定一個與FontName? –

+0

看看這個pastebin它定製它trebuchet我敢肯定,你可以拿起一些提示http://ja.pastebin.ca/raw/2304382 –

0

由於FONTNAME構造函數是私有的,我們不能直接實例化一個新FontName。相反,我們必須使用WritableFont.createFont。這是一種工廠方法,用於創建由提供的字體名稱指定的字體。

請注意以下事項:

這個方法應該小心使用,因爲用於創建的字體名稱的字符串必須用Excel的內部處理來識別


要創建Trebuchet MS字體,您只需調用靜態工廠方法即可。

public static final FontName TREBUCHET_MS = WritableFont.createFont("Trebuchet MS"); 

我有下面創建WritableFont對象來創建一個簡單的字體工廠API:

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

可以忽略這個文件,它僅用於解析路徑(「$ {userpr在上面的例子中,文檔/文檔/外部文件測試.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); 
    } 
}