2017-04-25 1 views
0

J'essaie de supprimer toutes les formules d'une feuille à l'aide d'openxml. Ce que je suis en train:Suppression d'une formule d'Excel à l'aide d'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(); 
    } 

Lors de l'ouverture du document Excel, je reçois l'erreur suivante:

<?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> 

Je compare l'ancien fichier calcChain.xml avec l'aide de nouveau généré OpenXML SDK outil . L'ancien fichier a les éléments suivants:

<?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> 

et le nouveau après l'exécution de mon code:

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

</x:calcChain> 

Quelqu'un pourrait-il aider si je manque quelque chose ici.

Répondre

1

Si vous supprimez toutes les formules, quel est le besoin d'avoir un fichier calcChain.xml vide? Avez-vous essayé de le supprimer?

Cela fonctionne pour moi:

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); 
    } 
} 
+0

Cela fonctionne. Mais pourquoi un fichier XML vide causerait-il des erreurs? Y a-t-il une relation qui doit être prise en charge? –