C# 插入公式时出现错误。。Excel在“中发现不可读的内容”;ab.xlsx";。是否要恢复该文件

C# 插入公式时出现错误。。Excel在“中发现不可读的内容”;ab.xlsx";。是否要恢复该文件,c#,openxml,C#,Openxml,我的要求是插入单元格的公式。我使用下面的方法插入公式。其插入公式正确,工作良好。 但是,当我插入公式时,我的excel文件被更正并显示消息 Excel在“exceltemplate.xlsx”中发现无法读取的内容 是否要恢复…的内容。 我搜索了很多,但没有得到解决。 请帮助解决此问题 public void InsertFormula(string filepath, string SheetName, string strCellIndex, string strFormula) {

我的要求是插入单元格的公式。我使用下面的方法插入公式。其插入公式正确,工作良好。 但是,当我插入公式时,我的excel文件被更正并显示消息

Excel在“exceltemplate.xlsx”中发现无法读取的内容

是否要恢复…的内容。 我搜索了很多,但没有得到解决。 请帮助解决此问题

public void InsertFormula(string filepath, string SheetName, string strCellIndex, string strFormula)
{
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(filepath, true))
    {
        IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == SheetName);
        if (sheets.Count() == 0)
        {
            // The specified worksheet does not exist.
            return;
        }
        WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
        Worksheet worksheet = worksheetPart.Worksheet;
        SheetData sheetData = worksheet.GetFirstChild<SheetData>();

        Row row1 = new Row()
        {
            RowIndex = (UInt32Value)4U,
            Spans = new ListValue<StringValue>()
        };

        Cell cell = new Cell() { CellReference = strCellIndex };
        CellFormula cellformula = new CellFormula();
        cellformula.Text = strFormula;
        cell.DataType = CellValues.Number;
        CellValue cellValue = new CellValue();
        cellValue.Text = "0";
        cell.Append(cellformula);
        cell.Append(cellValue);
        row1.Append(cell);

        sheetData.Append(row1);
        worksheet.Save();
        document.Close();
    }
}
public void InsertFormula(字符串文件路径、字符串SheetName、字符串strcelindex、字符串strFormula)
{
使用(SpreadsheetDocument=SpreadsheetDocument.Open(filepath,true))
{
IEnumerable sheets=document.WorkbookPart.Workbook.subjections()。其中(s=>s.Name==SheetName);
如果(sheets.Count()==0)
{
//指定的工作表不存在。
返回;
}
工作表部件工作表部件=(工作表部件)document.WorkbookPart.GetPartById(sheets.First().Id);
工作表=工作表零件工作表;
SheetData SheetData=工作表.GetFirstChild();
行row1=新行()
{
行索引=(uint32值)4U,
span=新的ListValue()
};
Cell Cell=new Cell(){CellReference=strCellIndex};
CellFormula CellFormula=新的CellFormula();
cellformula.Text=标准公式;
cell.DataType=CellValues.Number;
CellValue CellValue=新的CellValue();
cellValue.Text=“0”;
cell.Append(cellformula);
cell.Append(cellValue);
行1.追加(单元格);
sheetData.Append(第1行);
工作表。保存();
document.Close();
}
}

该功能有两个问题

第一个问题是您显式地将RowIndex设置为4U。要为其分配公式的单元格必须位于第4行,例如单元格C4。由于单元格引用是作为参数(strCellIndex)传入的,因此不能保证这一点

即使你解决了这个问题,我们还有下一个(更隐蔽的)问题

第二个问题更难解决。Row类必须按顺序插入SheetData类(作为子对象),并按RowIndex排序。假设您仍然希望RowIndex硬编码为4U。这意味着,如果现有Excel文件包含第2、3和7行,则必须将Row类插入具有RowIndex 3的Row类后面。这一点很重要,否则Excel会吐血(正如您已经体验到的)

第二个问题的解决方案需要更多的工作。考虑SeTeDATA类的函数插入()、插入项()和插入项(),或者实际上是大多数的开放式XML SDK类。迭代SheetData的子类,直到找到RowIndex大于要插入的Row类的Row类。然后使用InsertBefore()


我将留给您一个有趣的错误检查任务,例如,如果没有行类可以开始,或者所有行类的行索引都小于要插入的行类,或者(这里是有趣的一个)现有行类的行索引与要插入的行类的行索引相同。

exceltemplate.*aspx**听起来不正确…sry。。它的.xlsx@DanielHilgarthi删除了rowindex 4U,现在动态地传递rowindex。它工作起来很漂亮。非常感谢。