关键词:Java
Apose.Cells
Active X控件
Checkbox
Apache POI
今天简单快速分享一个Excel读取的问题,突然间有个客人给我来了一个带有Checkbox控件的Excel文件,让我做自动化解析,结果as always,我用Apache POI读取Excel,结果无法获取Checkbox cell,一开始没有发现,后来钻研了很久,原来下面的warning就是无法获取的关键
Warning: unknown object type code 0
Warning: Unknown on sheet "sheet1" not supported - omitting
Warning: client anchor not found
Warning: client anchor not found
Warning: unknown object type code 0
Warning: Unknown on sheet "sheet1" not supported - omitting
Warning: client anchor not found
Warning: client anchor not found
Warning: Text on sheet "sheet1" not supported - omitting
Warning: unknown object type code 11
Warning: Unknown on sheet "sheet1" not supported - omitting
仔细研究才发现Apache POI不支持某种类型,查回Apache POI官方文档和论坛,发现目前不具备操作Active X控件的能力,于是开始漫长的third party lib的寻找,最后发现了我们题目中提到的一个付费的product(Aspose.Cells),于是下了试用license试了一下,发现成功把Checkbox的text和value都拿出来了,唯一需要提一下的是,这个third party lib是付费的,需要购买license(各位土豪看官可以忽略),下面用一个例子简单写了一个demo:
-
假设有一个excel如下
CkbA未选中,CkbB选中,我们需要知道CkbA和CkbB是否被选中
- 下面是sample code(https://github.com/easonlau02/asposeJavaDemo)
import com.aspose.cells.CheckBox;
import com.aspose.cells.CheckBoxCollection;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import java.util.Iterator;
public class ExcelManipulate {
public String inputPath;
public ExcelManipulate(String inputPath){
this.inputPath = inputPath;
}
public static void main(String[] args) {
ExcelManipulate excelManipulate = new ExcelManipulate("./samples/activex.xls");
try{
// initial woorkbook
Workbook asposeWb = new Workbook(excelManipulate.inputPath);
// get worksheet 0
Worksheet ws = asposeWb.getWorksheets().get(0);
// get all checkbox from worksheet 0
CheckBoxCollection cbc = ws.getCheckBoxes();
Iterator<CheckBox> iterator = cbc.iterator();
while (iterator.hasNext()){
CheckBox cb = iterator.next();
// getText == checkbox name, getValue == checked/unchecked
System.out.println(cb.getText() + " = "+cb.getValue());
}
}catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
-
最后简单运行一些结果
可以看到获取的结果恰好是我们sample file里边的结果
- 写在最后,Aspose.Cells是付费的product,功能特别强大,license值得购买,除了Aspose.Cells,还有其他的product family
——END——
作者 :Eason
,专注各种技术、平台、集成,不满现状,喜欢改改改
文章、技术合作,大胆的扫一扫,害羞的请邮件
Email : eason.lau02@hotmail.com
网友评论