感谢您的帮助,我得到了一个例外: 保存文件C:\ U .....时出错 {"对象引用未设置为对象的实例。"}
堆栈跟踪: 在OfficeOpenXml.ExcelPackage.Save() 在MINAG.SMEA.Business.ExcelBusinessService.CreateTableExcel(Decimal surveyId)的c:\ Users \ luciano \ Desktop \ New folder \ implementation \ MINAG.SMEA.Business \ ExcelBusinessService.cs:line 2048
(仅限堆栈跟踪)
我目前的代码是:
FileInfo newFile = new FileInfo(file);
ExcelPackage excPck = new ExcelPackage(newFile);
foreach (SURVEY_BASE_TABLE_LIST item in survey.SURVEY_BASE_TABLE_LIST)
{
BASE_TABLE_LIST table = item.BASE_TABLE_LIST;
Int32 limit = table.CODE.Length;
if (limit > 31)
limit = 31;
string dynamicRangeFormula = "OFFSET('" + table.CODE.Substring(0, limit) + "'!$A$1;0;0;COUNTA('" + table.CODE.Substring(0, limit) + "'!$A$1:$A$1000)+1;COUNTA('" + table.CODE.Substring(0, limit) + "'!$1:$1))";
var ws = excPck.Workbook.Worksheets.Add("Pivot_" + table.CODE);
ExcelNamedRange newRange = new ExcelNamedRange(table.CODE.Substring(0, limit), null, ws, "C3", excPck.Workbook.Names.Count)
{
Formula = dynamicRangeFormula
};
var excelPivot = ws.PivotTables.Add(ws.Cells["A1"], newRange, "Pivot_" + table.CODE);
}
excPck.Save();
有什么想法吗?
答案 0 :(得分:0)
要构建一个新的范围,您希望ExcelNamedRange
具有公共构造函数而不是ExcelRange
。所以像这样的东西会给你一个新的命名范围,其中包含一个公式:
var ws = excPck.Workbook.Worksheets.Add("Pivot_" + table.CODE);
var newRange = new ExcelNamedRange("NewRange", null, ws, "C3", excPck.Workbook.Names.Count)
{
Formula = "SUM(A1:A4)"
};
excPck.Workbook.Names.Add(table.CODE, newRange);