读取Java Apache POI中的Excelcheckbox值

我花了无数小时试图find解决办法。 我已经尝试了Apache POI,JExcel和JXLS,但没有find代码来成功读取checkbox(表单控件)值。

如果有人find了工作解决scheme,那么如果你能在这里分享,那将是非常好的。 谢谢!

UPDATE

我已经编写了读取checkbox的代码,但是无法确定它是否被选中。

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.record.CommonObjectDataSubRecord; import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SubRecord; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class App { private static final String path = "C:\\test.xls"; private static final String Workbook = "Workbook"; private static void readExcelfile() { FileInputStream file = null; try { file = new FileInputStream(new File(path)); // Get the workbook instance for XLS file HSSFWorkbook workbook = new HSSFWorkbook(file); // Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows from first sheet Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t\t"); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t\t"); break; } } System.out.println(); } // file.close(); // FileOutputStream out = new FileOutputStream( // new File(path)); // workbook.write(out); // out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (file != null) file.close(); } catch (IOException ex) { ex.printStackTrace(); } } } private static void readCheckbox() { FileInputStream file = null; InputStream istream = null; try { file = new FileInputStream(new File(path)); POIFSFileSystem poifs = new POIFSFileSystem(file); istream = poifs.createDocumentInputStream(Workbook); HSSFRequest req = new HSSFRequest(); req.addListenerForAllRecords(new EventExample()); HSSFEventFactory factory = new HSSFEventFactory(); factory.processEvents(req, istream); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (file != null) file.close(); if (istream != null) istream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { System.out.println("ReadExcelFile"); readExcelfile(); System.out.println("ReadCheckbox"); readCheckbox(); } } class EventExample implements HSSFListener { public void processRecord(Record record) { switch (record.getSid()) { case ObjRecord.sid: ObjRecord objRec = (ObjRecord) record; List<SubRecord> subRecords = objRec.getSubRecords(); for (SubRecord subRecord : subRecords) { if (subRecord instanceof CommonObjectDataSubRecord) { CommonObjectDataSubRecord datasubRecord = (CommonObjectDataSubRecord) subRecord; if (datasubRecord.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_CHECKBOX) { System.out.println("ObjId: " + datasubRecord.getObjectId() + "\nDetails: " + datasubRecord.toString()); } } } break; } } } 

对不起,迟到的回复,但我遇到了同样的。 我发现一个技巧来确定checkbox的状态。

在你的例子中,你循环的SubRecords和你检查CommonObjectDataSubRecord 。 但是checkbox的值可以在SubRecord.UnknownSubRecord 。 这不幸是一个私人类,所以你不能调用任何方法,但toString()显示的数据,并用一个正则expression式值可以find。 所以使用下面的代码我设法检索checkbox的状态:

 Pattern p = Pattern.compile("\\[sid=0x000A.+?\\[0(\\d),"); if (!(subRecord instanceof CommonObjectDataSubRecord)) { Matcher m = p.matcher(subRecord.toString()); if (m.find()) { String checkBit = m.group(1); if (checkBit.length() == 1) { boolean checked = "1".equals(checkBit); checkBox.setChecked(checked); } } } 

现在我的挑战是检索xlsx文件中的checkbox值…