2013-02-04 5 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"); 

FontNameWritableFont 클래스는 클래스의 내부 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 

+0

답장을 보내 주셔서 감사합니다. 레이첼, WritableFont를 사용자 정의하고 싶습니다. WritableFont는 5-6 글꼴 옵션을 제공하지만 거기에없는 다른 글꼴을 갖고 싶습니다. – ayachama

+0

FontName에 글꼴을 지정할 수 없습니까? –

+0

이 붙여 넣기를 보시고 그것을 trebuchet에 알리십시오. 몇 가지 팁을 골라 드릴 수 있습니다. http://ja.pastebin.ca/raw/2304382 –

0

글꼴 이름 생성자는 개인이기 때문에 here, 우리는 직접 새 FontName을 인스턴스화 할 수 없습니다를 참조하십시오. 대신 WritableFont.createFont을 사용해야합니다. 이것은 제공된 글꼴 이름으로 지정된 글꼴을 만드는 팩토리 메서드입니다.

는 다음 사항에 유의하시기 바랍니다 :

글꼴 이름을 만드는 데 사용되는 문자열이 Excel의 내부 처리에 의해 인식해야하기 때문에이 방법은주의해서 사용해야합니다


Trebuchet MS 글꼴을 만들려면 단순히 정적 팩터 리 메서드를 호출하면됩니다.

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

나는 아래 WritableFont 객체를 생성하기위한 간단한 글꼴 공장 API를 만들 수 있습니다 :이 파일을 무시할 수 PathResolver.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; 
    } 
} 

FontCreator.java , 경로를 확인하는 데만 사용됩니다 ("$ {userpr ofile} /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); 
    } 
}