2017-04-25 1 views
0

openxml을 사용하여 시트에서 모든 수식을 제거하려고합니다. 이것은 내가 무엇을 시도하고있다 : 나는 수신하고OpenXML을 사용하여 Excel에서 수식 제거

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

엑셀 문서를 열면 다음과 같은 오류 :

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

그래서 나는 새로 생성 된 사용의 OpenXML SDK 도구를 사용하여 이전 calcChain.xml 파일을 비교 .

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

사람이 도움을 줄 수 오래된 파일은 다음 있습니다.

답변

1

모든 수식을 삭제하는 경우 빈 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); 
    } 
} 
+0

이 작동합니다. 하지만 빈 xml로 인해 오류가 발생하는 이유는 무엇입니까? 관심을 가져야 할 관계가 있습니까? –

관련 문제