I'm not even sure if I'm using the correct terminology; I will update the question and title as required.
I'm using the OpenXmlSDK to populate a pre-existing Excel 2010 .xlsm
file - a macro-enabled worksheet.
I can access worksheets and cells fairly well.
However, I can't figure out how to either access or update the data in a cell that is a dropdown-control that takes its values from a range on another worksheet.
It's labelled as "H13" on the worksheet, and right-click >> format control shows
Input range: 'anotherWorksheet'!$N$3:$N$54
Cell link: 'anotherWorksheet'!$M$3
Whenever I try to get a reference to this cell, I can't find it -- I get a null
value
I've tried two access methods:
I'm not even sure if I'm using the correct terminology; I will update the question and title as required.
I'm using the OpenXmlSDK to populate a pre-existing Excel 2010 .xlsm
file - a macro-enabled worksheet.
I can access worksheets and cells fairly well.
However, I can't figure out how to either access or update the data in a cell that is a dropdown-control that takes its values from a range on another worksheet.
It's labelled as "H13" on the worksheet, and right-click >> format control shows
Input range: 'anotherWorksheet'!$N$3:$N$54
Cell link: 'anotherWorksheet'!$M$3
Whenever I try to get a reference to this cell, I can't find it -- I get a null
value
I've tried two access methods:
// http://msdn.microsoft.com/en-us/library/ff921204.aspx
private static Cell GetCell(Worksheet worksheet, string addressName)
{
return worksheet.Descendants<Cell>().Where(
c => c.CellReference == addressName).FirstOrDefault();
}
and:
// Given a worksheet, a column name, and a row index,
// gets the cell at the specified column and
// http://stackoverflow.com/questions/527028/open-xml-sdk-2-0-how-to-update-a-cell-in-a-spreadsheet
private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
{
Row row = GetRow(worksheet, rowIndex);
if (row == null)
return null;
return row.Elements<Cell>().Where(c => string.Compare
(c.CellReference.Value, columnName +
rowIndex, true) == 0).FirstOrDefault();
}
// Given a worksheet and a row index, return the row.
private static Row GetRow(Worksheet worksheet, uint rowIndex)
{
return worksheet.GetFirstChild<SheetData>().
Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
}
Both yield null
for the target cell H13
, but provide references to surrounding cells (ie, `H12, H14, G13'
Actually, I13
also yields null
, but that cell is not populated with anything. However, if I can't get a reference, how could I populate it with the SDK? Not my main point, here.
I will be receiving data that will match one of the entries in the dropdown; I just need to actually populate/select that particular entry in the target spreadsheet.
How can I do this with the OpenXmlSDK? I've tried using various open-source libraries, but none seem to support .xslm
files (file provided by the client, and cannot be used in another format; macros must execute on launch, etc.).
Although I'm using C#, since my question is about the OpenXmlSDK, I would accept answers in other languages using that framework.