apache common-poi之设置字体

  1. import java.io.FileNotFoundException;   
  2. import java.io.FileOutputStream;   
  3. import java.io.IOException;   
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  5. import org.apache.poi.ss.usermodel.Cell;   
  6. import org.apache.poi.ss.usermodel.CellStyle;   
  7. import org.apache.poi.ss.usermodel.Font;   
  8. import org.apache.poi.ss.usermodel.Row;   
  9. import org.apache.poi.ss.usermodel.Sheet;   
  10. import org.apache.poi.ss.usermodel.Workbook;   
  11.   
  12. /**  
  13.  * @author 刘毅  
  14.  * @date 2010-1-12  
  15.  * @param  
  16.  */  
  17. public class CreateExcelForWorkingFontsSize {   
  18.  /***  
  19.   * 创建空的xls格式的文档,设置字体大小  
  20.   */  
  21.  public void createXlsForFontsSize(){   
  22.   Workbook wb = new HSSFWorkbook();   
  23.      Sheet sheet = wb.createSheet("new sheet");   
  24.      Row row = sheet.createRow(1);   
  25.      // 创建字体并改变它   
  26.      Font font = wb.createFont();   
  27.      //设置长度   
  28.      font.setFontHeightInPoints((short)24);   
  29.      font.setFontName("Courier New");   
  30.      //斜体   
  31.      font.setItalic(true);   
  32.      //设置字体杠杠   
  33.      font.setStrikeout(true);   
  34.      CellStyle style = wb.createCellStyle();   
  35.      style.setFont(font);   
  36.      Cell cell = row.createCell(1);   
  37.      cell.setCellValue("This is a test of fonts");   
  38.      cell.setCellStyle(style);   
  39.      FileOutputStream fileOut = null;   
  40.   try {   
  41.    fileOut = new FileOutputStream("src/workbook.xls");   
  42.    wb.write(fileOut);   
  43.   } catch (FileNotFoundException e) {   
  44.    e.printStackTrace();   
  45.   } catch (IOException e) {   
  46.    e.printStackTrace();   
  47.   }finally{   
  48.    try {   
  49.    if(null !=fileOut){   
  50.      fileOut.close();   
  51.     }   
  52.     } catch (IOException e) {   
  53.      e.printStackTrace();   
  54.     }   
  55.   }   
  56.  }   
  57.  public static void main(String[] args) {   
  58.   CreateExcelForWorkingFontsSize create = new CreateExcelForWorkingFontsSize();   
  59.   create.createXlsForFontsSize();   
  60.  }   
  61. }   

 

转帖:http://ajava.org/course/open/17874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设置字体加粗,你可以使用 Apache POI 库中的 Font 类和 CellStyle 类。下面是一个示例代码,演示如何使用 Apache POI 设置字体加粗: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class BoldFontExample { public static void main(String[] args) { // 创建工作簿和工作表 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建字体对象并设置加粗 Font font = workbook.createFont(); font.setBold(true); // 创建单元格样式对象并应用字体 CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFont(font); // 创建单元格,并将样式应用于单元格 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello, World!"); cell.setCellStyle(cellStyle); // 保存工作簿到文件 try { FileOutputStream fileOutputStream = new FileOutputStream("example.xlsx"); workbook.write(fileOutputStream); fileOutputStream.close(); System.out.println("文件保存成功!"); } catch (IOException e) { e.printStackTrace(); } // 关闭工作簿 try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码将创建一个名为 "Sheet1" 的工作表,并在第一个单元格中设置文本为 "Hello, World!",并将字体加粗应用于该单元格。 注意:上述示例使用的是 Apache POI 4.x 版本的 XSSFWorkbook 类和相关类。如果你使用的是较旧的版本,可能需要相应地更改代码以适应你的版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值