在Java Apache POI中读取Excel复选框的值

3

我已经花费了无数个小时尝试解决这个问题。我尝试过Apache POI、JExcel和JXLS,但是没有在任何地方找到成功读取复选框(表单控件)值的代码。

如果有人找到了有效的解决方法,那么如果您能在这里分享它,那就太好了。谢谢!

更新

我编写了可以读取复选框的代码,但无法确定它是否被选中。

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;
        }
    }
}

如果您粘贴您的代码并告知问题,我们可以帮助您解决问题。我个人更喜欢Apache POI。 - Eranda
我正在尝试在Java中读取Excel表单控件。不幸的是,我在这方面没有任何进展。 - AngryPanda
从标题中删除标签。 - Brian
1个回答

0

抱歉回复晚了,但我也遇到了同样的问题。我找到了一个技巧来确定复选框的状态。

在您的示例中,您正在循环处理SubRecords并检查CommonObjectDataSubRecord。但是,复选框的值可以在SubRecord.UnknownSubRecord之一中找到。这是不幸的私有类,因此您无法调用任何方法,但toString()会显示数据,并且通过一些正则表达式可以找到值。因此,使用下面的代码,我成功地检索了复选框的状态:

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文件中检索复选框的值...


1
这种方法非常不专业,而且很可能在未来的POI版本中出现问题。你最好修补UnknownSubRecord结构,然后在POI的bug跟踪器上提出此补丁,以便其他人也可以从你的发现中受益。 - morido
1
感谢您的评论。我会修补这个问题。 - Ivar Kanters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接