zoukankan      html  css  js  c++  java
  • Apache POI 合并单元格

    合并单元格所使用的方法:
    sheet.addMergedRegion( CellRangeAddress  cellRangeAddress  );
     
    CellRangeAddress  对象的构造方法需要传入合并单元格的首行、最后一行、首列、最后一列。
    CellRangeAddress cra=new CellRangeAddress(0, 3, 3, 9);
     
    怎样把数据写入合并后的单元格中
    1. 首先要查看你 CellRangeAddress 构造方法的firstcol index
    2. 创建firstcol cell对象
    3. cell 的set 方法写数据
    在合并单元格的后一个位置写数据
    1. 查看  CellRangeAddress 构造方法的lastcol index     
    2. 创建lastcol+1  cell
    3. cell 的set方法写数据
     

    以下是demo:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. FileOutputStream fos=new FileOutputStream("D:\13.xls");  
    2.           
    3.         Workbook wb=new HSSFWorkbook();  
    4.           
    5.         Sheet sheet=wb.createSheet();  
    6.         /* 
    7.          * 设定合并单元格区域范围 
    8.          *  firstRow  0-based 
    9.          *  lastRow   0-based 
    10.          *  firstCol  0-based 
    11.          *  lastCol   0-based 
    12.          */  
    13.         CellRangeAddress cra=new CellRangeAddress(0, 3, 3, 9);        
    14.           
    15.         //在sheet里增加合并单元格  
    16.         sheet.addMergedRegion(cra);  
    17.           
    18.         Row row = sheet.createRow(0);  
    19.           
    20.         Cell cell_1 = row.createCell(3);  
    21.           
    22.         cell_1.setCellValue("When you're right , no one remembers, when you're wrong ,no one forgets .");  
    23.           
    24.         //cell 位置3-9被合并成一个单元格,不管你怎样创建第4个cell还是第5个cell…然后在写数据。都是无法写入的。  
    25.         Cell cell_2 = row.createCell(10);  
    26.           
    27.         cell_2.setCellValue("what's up ! ");  
    28.           
    29.         wb.write(fos);  
    30.           
    31.         fos.close();  


     
  • 相关阅读:
    junit 4快速入门
    eclipse中的快速修复的另类用法(转,例子)
    eclipse 快速修复.(转)
    在Eclipse中导入dtd和xsd文件,使XML自动提示 (转)
    No grammar constraints (DTD or XML Schema)(转)
    web.xml报错Referenced file contains errors(转)
    Eclipse中编辑xml文件不自动提示怎么办(转)
    myeclipse 自定义xml模板,供新建xml文件时自动导入 (转)
    Junit 测试基础介绍!
    redis集群源码阅读 之 集群设置主从
  • 原文地址:https://www.cnblogs.com/exmyth/p/5378092.html
Copyright © 2011-2022 走看看