1

Before saving the Workbook I am deleting a CellArea in the Worksheet(I have used ClearContents, DeleteRange, ClearRange). But it keeps on giving an error "Specified argument was out of the range of valid values." However if i remove the delete code and try it saving, It works fine..KIndly Suggest.. My Code goes like this :

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;

1 Answers1

2

I have tested this scenario and during my testing, I had to update your code a bit accordingly (as there are some objects/pointers or other options for which I am not sure about their values. So, I have to use a dummy DataTable and other objects/values accordingly for it) to test his case using my sample input file (Book1.xlsx) with our latest version/fix: Aspose.Cells for .NET v7.3.2.5 and as per my observations, it works absolutely fine. I did not find any issue.

Here is my complete runnable sample code with v7.3.2.5 (Please recommend the user to try it):

Sample code:

DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(int));
for (int i = 0; i < 100; i++)
{
    dt.Rows.Add(i / 10 + 1, "Item " + i.ToString(), i);
}
Workbook _workbook = new Workbook(@"e:\test2\book1.xlsx");
Worksheet _worksheet = _workbook.Worksheets[0];
Range maxRange = _workbook.Worksheets[_worksheet.Name].Cells.MaxDisplayRange;
//workbook.Worksheets[_worksheet.WorksheetName].Cells.ClearRange((int)_worksheet.StartRow, (int)_worksheet.StartColumn, maxRange.RowCount, maxRange.ColumnCount);
_workbook.Worksheets[_worksheet.Name].Cells.ClearRange((int)maxRange.FirstRow, (int)maxRange.FirstColumn, maxRange.RowCount, maxRange.ColumnCount);
//workbook.Worksheets[_worksheet.WorksheetName].Cells.ImportDataTable(data.Tables[_worksheet.FixedWorksheetName], false, _worksheet.ExportDataStartRow ?? 0, 0)
_workbook.Worksheets[_worksheet.Name].Cells.ImportDataTable(dt, false, 0, 0);
//Instantiate the error checking options
ErrorCheckOptionCollection opts = _workbook.Worksheets[_worksheet.Name].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.Name].Cells.MaxDataRow, _workbook.Worksheets[_worksheet.Name].Cells.MaxDataColumn));
string _exportPath = @"e:\test2\ouput_book1.xlsx";
//Save the worksheet at an appropriate configured location and assign path to _exportPath..
_workbook.Save(_exportPath);

For your reference, I have also attached the resource files (Book1.xlsx, ouput_book1.xlsx). If you still face any issue with the latest release, can you please share a runnable code/console application (he may use some dummy datatable dynamically for it too) to reproduce the issue on our end. You can also post the query in Aspose.Cells product support forum.

codewarior
  • 441
  • 2
  • 9
  • Thanks for your vivid Response.. I had figured out that i was somehow passing wrong parameters into the ClearRange method which was making the Workbook not save at the end.. – LetsKickSomeAss in .net Oct 31 '12 at 06:53