npoi
npoi copied to clipboard
Dropdown support
NPOI Version Used
2.6.2
File Type
- [ ] XLSX
- [ ] XLS
- [X] DOCX
- [ ] XLSM
- [ ] OTHER
Use Case
My .docx file has a drop-down field that other users fill in, then send back to me for parsing. The dropdown is in a table. I would like to be able to extract the drop-down field.
Currently, I can iterate to the table tag and get the paragraph, but it's empty.
Description
In the underlying docx XML, the list is a ddList
and it has a result
value, which is an index into the list. There's a good example here of the specification and how to check the value.
Sample Code
Here's how much work it takes to get this value right now:
var table = ...
var answersRow = table.getRow(1); // or whichever row
var cellWithDropDown = answersRow.GetCell(3); // or whichever cell has the dropdown
// Parse the value!
var tc = cellWithDropDown.getCTTc90;
var p = tc.Items.toArray().Single(t => t is CT_P) as CT_P;
var r = p.Items.ToArray().Where(t => t is CT_R).Select(t => t as CT_R);
r = r.Where(r2 => r2.items.ToArray().Any(t => t is CT_FldChar));
List<CT_FldChar> fldChars = new();
foreach (var r3 in r) {
fldChars.AddRange(r.Items.ToArray().Where(t => t is CT_FldChar).Select(t => t as CT_FldChar));
}
var ffData = fldChars.Single(t => t.ffData != null).ffData;
var ddl = ffData.Items.Single(t => t is CT_FFDDList) as CT_FFDDList;
var index = int.Parse(ddl.result.val);
var value = ddl.listEntry[index].val;
// value now has the string value from the dropdown