c# - 使用 OpenXML 从 Excel 中删除公式

标签 c# openxml openxml-sdk

我正在尝试使用 openxml 从工作表中删除所有公式。这就是我正在尝试的:

internal static void ReplaceFormulaWithValue()
{
    var res = _worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();      

    foreach (Row row in _worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>())
    {
        foreach (Cell cell in row.Elements<Cell>())
        {
            if (cell.CellFormula != null &&
                  cell.CellValue != null)
            {
                string cellRef = cell.CellReference;
                CalculationChainPart calculationChainPart = _spreadSheet.WorkbookPart.CalculationChainPart;
                CalculationChain calculationChain = calculationChainPart.CalculationChain;
                var calculationCells = calculationChain.Elements<CalculationCell>().ToList();
                CalculationCell calculationCell = calculationCells.Where(c => c.CellReference == cellRef).FirstOrDefault();
                //CalculationCell calculationCell = calculationChain.Elements<CalculationCell>().Where(c => c.CellReference == cell.CellReference).FirstOrDefault();

                string value = cell.CellValue.InnerText;
                UpdateCell(cell, DataTypes.String, value);

                cell.CellFormula.Remove();
                calculationCell.Remove();                   
            }
        }
    }
    SaveChanges();
}

打开 Excel 文档时,我收到以下错误:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error192600_01.xml</logFileName><summary>Errors were detected in file 'C:\DEV\ExcelEditor\ExcelEditor\bin\Debug\Chart.xlsx'</summary>
<removedParts summary="Following is a list of removed parts:">
<removedPart>Removed Part: /xl/calcChain.xml part with XML error.  (Calculation properties) Catastrophic failure Line 1, column 138.</removedPart>
</removedParts></recoveryLog>

因此,我将旧的 calcChain.xml 文件与使用 OpenXML SDK 工具新生成的文件进行比较。旧文件具有以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<calcChain xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
 <c i="1" l="1" r="D2"/>
</calcChain>

以及运行我的代码后的新代码:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<x:calcChain xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">  
</x:calcChain>

如果我在这里遗漏了什么,有人可以帮忙吗?

最佳答案

如果要删除所有公式,为什么需要空的 calcChain.xml?您尝试过删除它吗?

这对我有用:

public static void ReplaceFormulasWithValue()
{
    try
    {
        CalculationChainPart calculationChainPart = _spreadSheet.WorkbookPart.CalculationChainPart;
        CalculationChain calculationChain = calculationChainPart.CalculationChain;
        var calculationCells = calculationChain.Elements<CalculationCell>().ToList();

        foreach (Row row in _worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>())
        {
            foreach (Cell cell in row.Elements<Cell>())
            {
                if (cell.CellFormula != null && cell.CellValue != null)
                {
                    string cellRef = cell.CellReference;                            
                    CalculationCell calculationCell = calculationCells.Where(c => c.CellReference == cellRef).FirstOrDefault();

                    UpdateCell(cell, DataTypes.String, cell.CellValue.InnerText);

                    cell.CellFormula.Remove();
                    if(calculationCell != null)
                    {                       
                        calculationCell.Remove();
                        calculationCells.Remove(calculationCell);
                    }
                    else
                    {
                        //Something is went wrong - log it
                    }                   
                }
                if (calculationCells.Count == 0)
                     _spreadSheet.WorkbookPart.DeletePart(calculationChainPart);

            }
            _worksheetPart.Worksheet.Save();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex);
    }
}

关于c# - 使用 OpenXML 从 Excel 中删除公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607549/

相关文章:

c# - 无法序列化 session 状态... [已更新!]

openxml - 如何将 OpenXml 段落识别为我以编程方式插入的段落?

java - ooxml 架构、IntelliJ IDEA 和 NullPointerException

c# - OpenXML 向现有 Excel 文件添加新行

c# - OpenXML 从工作表中获取工作表名称

c# - Azure Mobile 中的乐观并发功能

c# - 为什么 EF Core 在所需关系被清空时删除实体?

ms-office - 使用 Office "open"XML - 这有多难?

c# - OpenXml 无法打开包,因为 FileMode 或 FileAccess 值对流无效

C# 不能在 ushort 列表项中使用 Linq DefaultIfEmpty?