POI微软雅黑等样式操作

该博客介绍如何使用Apache POI库在Java中操作Excel文件,特别是设置字体(如微软雅黑)和样式,包括自动换行、锁定单元格以及隐藏列。示例代码展示了创建工作簿、工作表,设置单元格内容、样式和字体的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Poi 操作excel样式

微软雅黑  宋体

 

 

[java]   view plain  copy
 print ?
  1. package com.cloud.poi.utils;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStream;  
  6. import org.apache.poi.ss.usermodel.Cell;  
  7. import org.apache.poi.ss.usermodel.CellStyle;  
  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. import org.apache.poi.xssf.usermodel.XSSFCell;  
  12. import org.apache.poi.xssf.usermodel.XSSFFont;  
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  14. /** 
  15.  * Poi 操作excel样式 
  16.  * @author Henry 
  17.  */  
  18. public class PoiExcelStyleAlter {  
  19.     public static void main(String[] args) throws IOException {  
  20.         String filePath = "d:/";  
  21.         String fileName = "test";  
  22.         String fileType = "xlsx";  
  23.         Workbook workbook = new XSSFWorkbook();//创建工作簿  
  24.         Sheet sheet = workbook.createSheet("test");//创建sheet  
  25.         sheet.setColumnHidden(2true);//隐藏第1列--excel就不显示C纵列了  
  26.         Row row = sheet.createRow(1);  
  27.         Cell cell = row.createCell(1);  
  28.         CellStyle style = workbook.createCellStyle();//新建样式对象  
  29.         style.setWrapText(true);//设置自动换行  
  30.         XSSFFont font = (XSSFFont) workbook.createFont();//创建字体对象  
  31.         font.setFontName("微软雅黑");  
  32.         style.setFont(font);  
  33.         cell.setCellStyle(style);  
  34.         style.setLocked(true);//设置cell所引用的样式是否锁住  
  35.         cell.setCellType(XSSFCell.CELL_TYPE_STRING);//设置单元格 格式为 字符串  
  36.         cell.setCellValue("test我现在很累,你知道吗,我真的很累。我现在很累很累很累很累!~I 老虎 YOU!");  
  37.         File file = new File(filePath + fileName + "." + fileType);  
  38.         OutputStream stream = new FileOutputStream(file);  
  39.         workbook.write(stream);  
  40.         stream.close();  
  41.     }  
  42. }  

转:http://blog.youkuaiyun.com/lihaiyun718/article/details/8197691

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值