使用C#添加一个公式,该公式引用OPEN XML sdk中另一个工作簿中的单元格

时间:2015-01-09 18:23:48

标签: openxml openxml-sdk

我正在使用C#中的Open XML sdk 2.0。

我有两本工作簿Book1和Book2。 让我们说Book1在其单元格D1中的值为10。然后book2应该在其单元格A1中有10个。 Book1已经创建了我需要处理book2。

我正在尝试创建一个工作簿Book2,其中单元格A1引用Book1(Sheet1)中的单元格D1。 当用户在Book1中更改D1时,book2中的值A1也应该在完成后更改。

到目前为止我有这个代码

Row row2 = new Row()
        {
            RowIndex = 1U,
            Spans = new ListValue<StringValue>()
        };

// string formula= "Sum(B2,B5)"; // this one works 
String formula= "'C:\Workbooks\[Book2.xlsx]Sheet1'!$D$1"; // this does not

Cell cell3 = new Cell() { CellReference = "A1" };
CellFormula formula = new CellFormula(formula);
cell3.Append(formula);

CellValue value = new CellValue();
value.Text = "0";
cell3.Append(value);

row2.Append(cell3);

每当我将该行添加到工作簿并保存它时,如果我使用下面的公式,则book2已损坏

formula= "'C:\Workbooks\[Book2.xlsx]Sheet1'!$D$1"; // this does not

但如果我使用如下的简单公式就可以了。

formula= "Sum(B2,B5)"; // this one works 

我不知道如何使用C#添加一个引用OPEN XML sdk中另一个工作簿中的单元格的公式。

1 个答案:

答案 0 :(得分:1)

问题是Excel需要一个外部参考,告知它其他工作表是否存在。要执行此操作,您需要在WorkBookPart上调用AddExternalRelationship方法:

workbookpart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath", 
        new System.Uri(@"C:\Workbooks\Book2.xlsx", UriKind.Absolute), "rId1");

如果您解压缩引用另一个工作簿的Xlsx文件并导航到xl文件夹,您将看到有一个externalLinks文件夹。这是您需要创建的部分。

这是一个完整的代码示例,我将从this MSDN documentation改编而来。

public static void CreateSpreadsheetWorkbook(string filepath)
{
    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
        Create(filepath, SpreadsheetDocumentType.Workbook);

    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();

    SheetData sheetData = new SheetData();
    worksheetPart.Worksheet = new Worksheet(sheetData);

    // Add Sheets to the Workbook.
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
        AppendChild<Sheets>(new Sheets());

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet()
    {
        Id = spreadsheetDocument.WorkbookPart.
            GetIdOfPart(worksheetPart),
        SheetId = 1,
        Name = "mySheet"
    };
    sheets.Append(sheet);

    Row row2 = new Row()
        {
            RowIndex = 1U
        };

    //add your formula as per your question
    String formula = @"'C:\Workbooks\[Book2.xlsx]Sheet1'!$D$1";

    Cell cell3 = new Cell() { CellReference = "A1" };
    CellFormula cellFormula = new CellFormula(formula);
    cell3.Append(cellFormula);

    row2.Append(cell3);

    //add a relationship that points to the correct file
    workbookpart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath",
        new System.Uri(@"C:\Workbooks\Book2.xlsx", UriKind.Absolute), "rId1");

    sheetData.Append(row2);

    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
}