。我正在使用下面的方法來插入公式。其插入式正確配方工作正常。 但是當我公式插入我的excel文件得到corrpted並顯示插入公式給出錯誤。Excel在「ab.xlsx」中發現不可讀的內容。你是否想要恢復我要求插入單元格公式的
「的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();
}
}
* exceltemplate。** aspx ***聽起來不太合適... – 2013-05-13 11:31:01
sry ..它的.xlsx @DanielHilgarth – 2013-05-13 12:22:42