POI excel 设置样式

  1.   
  1. 创建sheet什么的就不多说了,直接进入正题  
  2.   
  3.   
  4. HSSFCellStyle cellStyle = wb.createCellStyle();    
  5.  一、设置背景色:  
  6.   
  7.   
  8. cellStyle.setFillForegroundColor((short) 13);// 设置背景色    
  9. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);    
  10. 二、设置边框:  
  11.   
  12.   
  13. cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
  14. cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
  15. cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
  16. cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框    
  17. 三、设置居中:  
  18.   
  19.   
  20. cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中    
  21. 四、设置字体:  
  22.   
  23.   
  24. HSSFFont font = wb.createFont();    
  25. font.setFontName("黑体");    
  26. font.setFontHeightInPoints((short) 16);//设置字体大小    
  27.     
  28. HSSFFont font2 = wb.createFont();    
  29. font2.setFontName("仿宋_GB2312");    
  30. font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示    
  31. font2.setFontHeightInPoints((short) 12);    
  32.     
  33. cellStyle.setFont(font);//选择需要用到的字体格式    
  34. 五、设置列宽:  
  35.   
  36. sheet.setColumnWidth(0, 3766);   
  37. //第一个参数代表列id(从0开始),第2个参数代表宽度值  参考 :"2012-08-10"的宽度为2500    
  38. 六、设置自动换行:  
  39.   
  40. cellStyle.setWrapText(true);//设置自动换行    
  41. 七、合并单元格:  
  42.   
  43. Region region1 = new Region(0, (short) 0, 0, (short) 6);//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号    
  44.   
  45.   
  46. //此方法在POI3.8中已经被废弃,建议使用下面一个    
  47. 或者用  
  48.   
  49.   
  50. CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);     
  51.   
  52.   
  53. //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列      
  54. 但应注意两个构造方法的参数不是一样的,具体使用哪个取决于POI的不同版本。   
  55.   
  56.   
  57. sheet.addMergedRegion(region1);    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用POI操作Excel时,可以通过设置CellStyle的属性来设置行间距。 具体操作步骤如下: 1. 获取Workbook对象。 2. 创建CellStyle对象。 3. 设置CellStyle的行高和字体属性,其中行高的单位是1/20个点。 ```java // 创建CellStyle对象 CellStyle cellStyle = workbook.createCellStyle(); // 设置行高 cellStyle.setRowHeight((short) (20 * 20)); // 创建字体对象 Font font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 设置字体 font.setFontName("宋体"); // 设置字体样式 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 设置字体颜色 font.setColor(IndexedColors.BLACK.getIndex()); // 设置行间距 font.setLineSpacing((short) 300); // 将字体应用到CellStyle cellStyle.setFont(font); ``` 4. 将CellStyle应用到需要设置行间距的行。 ```java // 获取Sheet对象 Sheet sheet = workbook.getSheetAt(0); // 获取第一行 Row row = sheet.getRow(0); // 将CellStyle应用到第一行 row.setRowStyle(cellStyle); ``` 完整示例代码如下: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class PoiExcelSetRowSpacingDemo { public static void main(String[] args) throws IOException { // 创建Workbook对象 Workbook workbook = new HSSFWorkbook(); // 创建Sheet对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建Row对象 Row row = sheet.createRow(0); // 创建Cell对象 row.createCell(0).setCellValue("Hello World!"); // 创建CellStyle对象 CellStyle cellStyle = workbook.createCellStyle(); // 设置行高 cellStyle.setRowHeight((short) (20 * 20)); // 创建字体对象 Font font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 设置字体 font.setFontName("宋体"); // 设置字体样式 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 设置字体颜色 font.setColor(IndexedColors.BLACK.getIndex()); // 设置行间距 font.setLineSpacing((short) 300); // 将字体应用到CellStyle cellStyle.setFont(font); // 将CellStyle应用到第一行 row.setRowStyle(cellStyle); // 保存Excel文件 FileOutputStream fos = new FileOutputStream("test.xls"); workbook.write(fos); fos.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值