【发布时间】:2018-09-19 14:42:16
【问题描述】:
我有一个现有的电子表格。我想将一些单元格从general 或custom 重新格式化为Number。但是由于某种原因,当我重新格式化它们以编号时,单元格值会更改为某个随机数。
这是我的代码的样子
using (var d = SpreadsheetDocument.Open(_docPath, true))
{
var workSheet = GetWorksheetPart(d.WorkbookPart.Workbook.WorkbookPart, "Project Summary");
foreach (var cellRef in _ProjectRequirementsFormService.NumberCellReferences)
{
Cell theCell = workSheet.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellRef).FirstOrDefault();
if (theCell != null) {
//For some reason This line of code replaces the cell value with a value 5197
//and every iteration of the loop increments the value of the next cell found 5198, 5199, etc....
theCell.DataType = CellValues.Number;
}
}
...
}
public WorksheetPart GetWorksheetPart(WorkbookPart workbookPart, string sheetName)
{
string relId = workbookPart.Workbook.Descendants<Sheet>().First(s => sheetName.Equals(s.Name)).Id;
return (WorksheetPart)workbookPart.GetPartById(relId);
}
现在由于某种原因这行代码theCell.DataType = CellValues.Number; 将单元格值替换为值 5197
并且循环的每次迭代都会增加找到的下一个单元格的值 5198、5199 等......
当我评论那条有缺陷的行时,我在单元格中得到了正确的值,但格式不是 Number
这是我添加代码行以将其更改为数字格式时的样子。为什么它会改变所有单元格的值,我该如何解决?
链接到示例文件
https://drive.google.com/file/d/0B7UImeY4gR3VdnBBZmdWSHJqN2lhRmFqY1N6THJXNmIzZDhF/view?usp=sharing
【问题讨论】:
-
请附上您的原始excel文件。
-
@AccessDenied 我该怎么做?
-
通过 google drive 或 dropbox 或 onedrive 共享它并提供此文件的链接
-
@AccessDenied 刚刚编辑了链接。
标签: c# openxml-sdk