zoukankan      html  css  js  c++  java
  • POI按照源单元格设置目标单元格格式

    POI按照源单元格设置目标单元格格式

    poi按照一个源单元格设置目标单元格格式,如果两个单元格不在同一个workbook,
    要用
    HSSFCellStyle下的cloneStyleFrom()方法,
    而不能用
    HSSFCell下的setCellStyle()方法。
     
    public void copyHssfRow(HSSFRow destRow, HSSFRow sourceRow){
    int currentColumnNum = 0;
    HSSFCell tempCell = null;
    HSSFCellStyle tempCellStyle = null;
    System.out.println("ExcelHandler.java copy specific excel row starts------");
    for(Iterator i = sourceRow.cellIterator(); i.hasNext();){
    tempCell = (HSSFCell)i.next();
    //确保这个destRow的待赋值cell有值
    if(destRow.getCell(currentColumnNum) == null){
    destRow.createCell(currentColumnNum);
    }
    destRow.getCell(currentColumnNum).setCellValue(tempCell.getStringCellValue());
    //红色字体的目的是把目的单元格的格式设置为源单元格的格式,这是正确的方式
    tempCellStyle = tempCell.getCellStyle();
    destRow.getCell(currentColumnNum).getCellStyle().cloneStyleFrom(tempCellStyle);
    System.out.println("DestCell name: " + destRow.getCell(currentColumnNum).getStringCellValue()
    + " SourceCell name: " + tempCell.getStringCellValue());
    currentColumnNum++;
    }
    System.out.println("ExcelHandler.java copy specific excel row ends------");
    }
     
    最初红色的代码是这样的:
    tempCellStyle = tempCell.getCellStyle();
    destRow.getCell(currentColumnNum).setCellStyle(tempCellStyle);
    然后出现了报错:
    java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook.
     
    再去google搜索这个报错,发现poi手册中HSSFCellStyle有一个方法会抛出这个异常。

    verifyBelongsToWorkbook

    public void verifyBelongsToWorkbook(HSSFWorkbook wb)
    Verifies that this style belongs to the supplied Workbook. Will throw an exception if it belongs to a different one. This is normally called when trying to assign a style to a cell, to ensure the cell and the style are from the same workbook (if they're not, it won't work)
    Throws:
    java.lang.IllegalArgumentException- if there's a workbook mis-match
    再往下一看,就柳暗花明了。poi要求一个单元格的格式和单元格本身都在一个worksheet,不同的worksheet间不能共用HSSFCellStyle。想了下,这样是对的,这样就可以保证同一个style不会同时属于两个不同sheet的不同单元格,如果对一个单元格做了修改,另一个也会受到牵连。单元格之间的格式相当一个格式集,格式集中的参数数值我们可以一样,但是我们的格式集不能是同一个。

    cloneStyleFrom

    public void cloneStyleFrom(CellStyle source)Clones all the style information from another HSSFCellStyle, onto this one. This HSSFCellStyle will then have all the same properties as the source, but the two may be edited independently. Any stylings on this HSSFCellStyle will be lost! The source HSSFCellStyle could be from another HSSFWorkbook if you like. This allows you to copy styles from one HSSFWorkbook to another.
    Specified by:
    cloneStyleFromin interfaceCellStyle
     
  • 相关阅读:
    今天开始用 VSU 2010
    Visual Studio 2010 模型设计工具 基本应用
    Asp.Net访问Oracle 数据库 执行SQL语句和调用存储过程
    Enterprise Library 4.1 Security Block 快速使用图文笔记
    解决“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本。”(图)
    一个Oracle存储过程示例
    Enterprise Library 4.1 Application Settings 快速使用图文笔记
    Oracle 10g for Windows 简体中文版的安装过程
    Oracle 11g for Windows 简体中文版的安装过程
    Oracle 9i 数据库 创建数据库 Net 配置 创建表 SQL查询 创建存储过程 (图)
  • 原文地址:https://www.cnblogs.com/zouhao/p/3672953.html
Copyright © 2011-2022 走看看