OpenXMLInnerText始终是整数而不是字符串

时间:2018-12-26 04:27:43

标签: c# excel openxml

当我从OpenXML读取单元格值时,我得到的InnerText单元格始终是Integer

using (SpreadsheetDocument document = SpreadsheetDocument.Open(INPUT_DIRECTORY, false))
            {
                WorkbookPart wbPart = document.WorkbookPart;

                foreach (Sheet sheet in wbPart.Workbook.Sheets)
                {
                    switch (sheet.Name.ToString())
                    {
                        case "ABC":

                            WorksheetPart wsPart = (WorksheetPart)wbPart.GetPartById(sheet.Id);
                            SheetData sheetdata = wsPart.Worksheet.Elements<SheetData>().FirstOrDefault();                            

                            foreach (Row r in sheetdata.Elements<Row>())
                            {
                                string xxx = r.Elements<Cell>().ElementAt(0).InnerText;
                                string yyy = r.Elements<Cell>().ElementAt(6).InnerText;
                                string zzz = r.Elements<Cell>().ElementAt(12).InnerText;
                            }                            

                            break;
                    }
                }
            }

xxx,yyy,zzz始终是整数,即使它是带有值的字符串。

2 个答案:

答案 0 :(得分:0)

尝试一下:

using (SpreadsheetDocument document = SpreadsheetDocument.Open(INPUT_DIRECTORY, false))
        {
            WorkbookPart wbPart = document.WorkbookPart;

            foreach (Sheet sheet in wbPart.Workbook.Sheets)
            {
                switch (sheet.Name.ToString())
                {
                    case "ABC":

                        WorksheetPart wsPart = (WorksheetPart)wbPart.GetPartById(sheet.Id);
                        SheetData sheetdata = wsPart.Worksheet.Elements<SheetData>().FirstOrDefault();                            

                        foreach (Row r in sheetdata.Elements<Row>())
                        {
                            string xxx = r.Elements<Cell>().ElementAt(0).InlineString.Text.Text;
                            string yyy = r.Elements<Cell>().ElementAt(6).InlineString.Text.Text;
                            string zzz = r.Elements<Cell>().ElementAt(12).InlineString.Text.Text;
                        }                            

                        break;
                }
            }
        }

答案 1 :(得分:0)

这可能是因为您需要访问SharedStringTablethis post显示了一种处理它的方法。

基本上,您需要从工作簿中获取字符串表:

DefaultMutableTreeNode

然后检查您要查看的单元格的类型,并在必要时查找字符串表ID以获取文本的值。像这样:

private DefaultTreeModel treeModel;
private DefaultMutableTreeNode rootNode;
CheckBoxTree chkbTree;
...
...
public void decode(){
...
...
treeModel = createUnitTypesJTree(unitTypes_);
}
/**
* Return a JTree with all unit types in it
* @param childNodes_ child nodes to all to JTree
* @return
*/
public DefaultTreeModel createUnitTypesJTree (ArrayList<String> childNodes_){

    DefaultTreeModel treeModel_;
    treeModel_ = getTreeModel();
    rootNode = (DefaultMutableTreeNode) treeModel_.getRoot();

    for(int i=0; i<childNodes_.size(); i++){
        rootNode.add(new DefaultMutableTreeNode(childNodes_.get(i)));
    }
    treeModel_.reload(rootNode);
    return treeModel_;
}
private DefaultTreeModel getTreeModel() {
        return new DefaultTreeModel(new DefaultMutableTreeNode("Units"));
}
public CheckBoxTree getUnitsJTree(){
        chkbTree = new CheckBoxTree(treeModel);
        chkbTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                doMouseCLicked(e);
            }
        });
        return chkbTree;
    }