删除一系列单元格后,使用Aspose工具保存工作簿时出错

时间:2012-10-18 05:40:54

标签: c# excel aspose aspose-cells

在保存工作簿之前,我正在删除工作表中的CellArea(我使用过ClearContents,DeleteRange,ClearRange)。但它继续发出错误“指定的参数超出了有效值的范围。” 但是,如果我删除删除代码并尝试保存,它工作正常..KIndly Suggest .. 我的代码是这样的:

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 个答案:

答案 0 :(得分:2)

我测试了这个场景,在测试过程中,我不得不相应地更新你的代码(因为有一些对象/指针或其他选项,我不确定它们的值。所以,我有使用我的样本输入文件(Book1.xlsx)和我们的最新版本/修复Aspose.Cells for .NET v7.3.2.5并根据我的观察,使用虚拟DataTable和相应的其他对象/值来测试他的案例它工作得很好。我没有发现任何问题。

以下是我使用v7.3.2.5的完整可运行示例代码(请建议用户试用):

示例代码:

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);

供您参考,我还附上了资源文件(Book1.xlsxouput_book1.xlsx)。如果你仍然遇到最新版本的任何问题,请你分享一个可运行的代码/控制台应用程序(他也可以动态地使用一些虚拟数据表)来重现我们的问题。您也可以在Aspose.Cells product support forum

中发布查询