2011-12-08 34 views
1

我甚至不確定我是否使用了正確的術語;我會根據需要更新問題和標題。使用OpenXmlSDK,我如何選擇/更新範圍控制單元格中的值?

我正在使用OpenXmlSDK填充預先存在的Excel 2010 .xlsm文件 - 一個啓用宏的工作表。

我可以很好地訪問工作表和單元格。

但是,我不知道如何訪問或更新單元格中的數據,該單元格是下拉控件,它從另一個工作表上的範圍中獲取其值。

它標記爲「H13」的工作表上,並用鼠標右鍵點擊>>格式控制顯示

Input range: 'anotherWorksheet'!$N$3:$N$54 
Cell link: 'anotherWorksheet'!$M$3 

每當我試圖讓這個單元格的引用,我無法找到它 - 我獲得null

我已經試過兩種接入方式:

我甚至不知道,如果我使用的是正確的術語;我會根據需要更新問題和標題。

我正在使用OpenXmlSDK填充預先存在的Excel 2010 .xlsm文件 - 一個啓用宏的工作表。

我可以很好地訪問工作表和單元格。

但是,我不知道如何訪問或更新單元格中的數據,該單元格是下拉控件,它從另一個工作表上的範圍中獲取其值。

它標記爲「H13」的工作表上,並用鼠標右鍵點擊>>格式控制顯示

Input range: 'anotherWorksheet'!$N$3:$N$54 
Cell link: 'anotherWorksheet'!$M$3 

每當我試圖讓這個單元格的引用,我無法找到它 - 我獲得null

我已經試過兩種接入方式:

// 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(); 
    } 

和:

// 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(); 
    } 

兩者得到null用於所述目標小區H13,但提供到周圍的細胞(即,`H12,H14,G13'

實際上,I13也產生null,但細胞不與任何填充的引用。但是,如果我無法獲得參考,我怎麼才能使用SDK進行填充?不是我的主要觀點,在這裏。

我將收到與下拉菜單中的某個條目相匹配的數據;我只需要實際填充/選擇目標電子表格中的特定條目。

我該如何使用OpenXmlSDK做到這一點?我試過使用各種開源庫,但似乎都不支持.xslm文件(客戶端提供的文件,不能以其他格式使用;宏必須在啓動時執行等)。

儘管我使用C#,因爲我的問題是關於OpenXmlSDK,我會接受使用該框架的其他語言的答案。

回答

1

簡答:該單元不存在,因此null參考。

我創建了一個小工作表,其中有一個列表(下拉式)DataValidation指向另一個工作表中的單元格範圍。

反映使用Open XML SDK 2.0 Productivity Tool文件我看到,而不是一個單元格被創建並附加到工作表,DataValidation(具有CellReference等於目標)被創建,而不是。

 
    using X14 = DocumentFormat.OpenXml.Office2010.Excel; 

    [....] 

    X14.DataValidations dataValidations1 = new X14.DataValidations() { Count = (UInt32Value)1U }; 
    dataValidations1.AddNamespaceDeclaration("xm", "http://schemas.microsoft.com/office/excel/2006/main"); 

    X14.DataValidation dataValidation1 = new X14.DataValidation() { Type = DataValidationValues.List, AllowBlank = true, ShowInputMessage = true, ShowErrorMessage = true }; 

    X14.DataValidationForumla1 dataValidationForumla11 = new X14.DataValidationForumla1(); 
    Excel.Formula formula1 = new Excel.Formula(); 
    formula1.Text = "Lists!$A$1:$A$51"; 

    dataValidationForumla11.Append(formula1); 
    Excel.ReferenceSequence referenceSequence1 = new Excel.ReferenceSequence(); 
    referenceSequence1.Text = "A1"; 

    dataValidation1.Append(dataValidationForumla11); 
    dataValidation1.Append(referenceSequence1); 

    dataValidations1.Append(dataValidation1); 

除非工作表中的位置有一個預設值時,它實際上不會當在運行時訪問「是」一個「細胞」。

回想起來,這是有道理的。但在視覺上,它看起來像一個細胞,所以它不是很明顯....

非單元: ceci n'est pas un cell

注意:如果選擇從資料驗證製成,並保存,該單元現在有一個值,所以它存在:

是細胞: c'est bizarre, non?

這可以通過創建和附加到目標行的新小區則返回一個空引用時,周圍的工作。

現在的困難在於需要對共享字符串表的引用驗證,並且不會接受原始文本的單元格值....

更新:我能找到的資料驗證找到目標工作表和範圍,在該範圍內找到目標值,並獲取與該值關聯的SharedStringsTable引用。但是,無論我插入目標單元格的值是什麼,Excel在打開時都被認爲是不好的數據。

最後,我放棄了,回到Excel Interop,並找到a method to select a dropdown field from within (ugh) Interop.

相關問題