在保存工作簿之前,我正在删除工作表中的 CellArea(我使用了 ClearContents、DeleteRange、ClearRange)。但它不断给出错误“指定的参数超出了有效值的范围。” 但是,如果我删除删除代码并尝试保存,它工作正常..请建议.. 我的代码是这样的:
foreach (GcmTemplateWorksheets _worksheet in Worksheetdetails)
{
if (_worksheet.IsTransposeRequired ?? false)
{
Range maxRange = _workbook.Worksheets[_worksheet.WorksheetName].Cells.MaxDisplayRange;
_workbook.Worksheets[_worksheet.WorksheetName].Cells.ClearRange((int)_worksheet.StartRow, (int)_worksheet.StartColumn, maxRange.RowCount, maxRange.ColumnCount);
}
_workbook.Worksheets[_worksheet.WorksheetName].Cells.ImportDataTable(data_.Tables[_worksheet.FixedWorksheetName], false, _worksheet.ExportDataStartRow ?? 0, 0);
//Instantiate the error checking options
ErrorCheckOptionCollection opts = _workbook.Worksheets[_worksheet.WorksheetName].ErrorCheckOptions;
int index = opts.Add();
ErrorCheckOption opt = opts[index];
//Disable the numbers stored as text option
opt.SetErrorCheck(ErrorCheckType.TextNumber, false);
opt.AddRange(CellArea.CreateCellArea(0, 0, _workbook.Worksheets[_worksheet.WorksheetName].Cells.MaxDataRow, _workbook.Worksheets[_worksheet.WorksheetName].Cells.MaxDataColumn));
}
string _exportPath = System.Configuration.ConfigurationManager.AppSettings["ExportTemplatePath"].ToString();
//Save the worksheet at an appropriate configured location and assign path to _exportPath..
_workbook.Save(_exportPath);
return _exportPath;