我使用POI API生成excel报表,我有一个字体文件
我需要的是在生成报告时使用这种字体,而在API函数中搜索时,我没有找到任何以字体路径作为参数的函数。
只支持以下功能:
createFont()
getFontAt(short idx)
findFont(short boldWeight,
short color,
short fontHeight,
java.lang.String name,
boolean italic,
boolean strikeout,
short typeOffset,
byte underline)
你能告诉我怎么做吗?
你应该使用HSSFFont。看一下这个API。
例如: public class ChangeCellFontName {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
/* Get access to HSSFCellStyle */
HSSFCellStyle my_style = my_workbook.createCellStyle();
/* Create HSSFFont object from the workbook */
HSSFFont my_font=my_workbook.createFont();
/* Set the font name to Verdana */
my_font.setFontName("Verdana");
/* Also make the font color to RED */
my_font.setColor(HSSFFont.COLOR_RED);
/* attach the font to the style created earlier */
my_style.setFont(my_font);
/* Attach the new font to a cell */
/* Create a row in the sheet */
Row row = my_sheet.createRow(0);
/* Create a cell */
Cell cell = row.createCell(0);
cell.setCellValue("The font for this text would be Verdana");
/* Attach the style to the cell */
cell.setCellStyle(my_style);
}
}
最后一个例子是从这里取的。还有一个例子。