0
我想從工作表中刪除所有公式。這正是我試圖:使用OpenXML從Excel中刪除公式使用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>
誰能幫助,如果我在這裏失去了一些東西。
這工作。但爲什麼會空的XML導致錯誤?有需要照顧的一些關係嗎? –