简体   繁体   English

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

[英]Reading Excel checkbox values in Java Apache POI

I have spent countless hours trying to find a solution to this. 我花了无数个小时试图找到解决方案。 I have tried Apache POI, JExcel and JXLS but no where have I found code to successfully read checkbox (form control) values. 我已经尝试过Apache POI,JExcel和JXLS但是我没有找到成功读取复选框(表单控件)值的代码。

If anyone has found a working solution then it would be great if you could share it here. 如果有人找到了可行的解决方案,那么如果你能在这里分享它会很棒。 Thanks! 谢谢!

UPDATE UPDATE

I have written code that reads the checkbox but it cannot determine whether it is checked or not. 我编写的代码读取了复选框,但无法确定是否已选中。

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

Sorry for the late reply, but I ran into the same. 对不起,迟到的回复,但我遇到了同样的问题。 I found a trick to determine the checkbox state. 我发现了一个确定复选框状态的技巧。

In your example you ar looping over the SubRecords and you examine the CommonObjectDataSubRecord . 在您的示例中,您将遍历SubRecords并检查CommonObjectDataSubRecord But the value for the checkbox can be found in the one of the SubRecord.UnknownSubRecord . 但是复选框的值可以在SubRecord.UnknownSubRecord This is unfortunately a private class so you cannot call any method on it, but the toString() reveals the data, and with a little regex the value can be found. 遗憾的是,这是一个私有类,所以你不能调用它上面的任何方法,但toString()显示数据,并且只需一点​​正则表达式即可找到该值。 So using the code below I managed to retrieve the state of the 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);
        }
    }
}

Now my challenge is to retrieve the checkbox value in a xlsx file... 现在我的挑战是检索xlsx文件中的复选框值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM