ClosedXML文件保存的范围错误

时间:2018-02-01 17:54:11

标签: c# excel closedxml

我正在使用ClosedXML修改现有的Excel文件。一切都很好,直到我保存文件:

  

指定的参数超出了有效值的范围。\ r \ n参数名称:单元格A4和X3超出了“Delinquency!A3:X3”的范围。

     

在ClosedXML.Excel.XLRangeBase.GetRange(XLAddress newFirstCellAddress,   XLAddress newLastCellAddress)

     

在ClosedXML.Excel.XLRangeBase.Range(IXLCell firstCell,IXLCell)   lastCell)

     

在ClosedXML.Excel.XLRange.ClosedXML.Excel.IXLRange.Range(IXLCell)   firstCell,IXLCell lastCell)

     

在ClosedXML.Excel.XLWorkbook.PopulateAutoFilter(XLAutoFilter)   xlAutoFilter,AutoFilter autoFilter)

     

在   ClosedXML.Excel.XLWorkbook.GenerateWorksheetPartContent(WorksheetPart   worksheetPart,XLWorksheet xlWorksheet,Boolean evaluateFormulae,   SaveContext context)

     

在ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument   document,SaveOptions选项)

     

在ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath,   SpreadsheetDocumentType spreadsheetDocumentType,SaveOptions选项)

     

在ClosedXML.Excel.XLWorkbook.SaveAs(String file,SaveOptions options)

     

在ClosedXML.Excel.XLWorkbook.SaveAs(String file,Boolean validate,   Boolean evaluateFormulae)at   ST_6bc5fa72e8174cd4a207b1b253d87b23.ScriptMain.Main()in   C:\ Users \用户jd​​oe的\应用程序数据\本地\ TEMP \ VSTA \ 48f142323f754c1487ca867a1cf952c5 \ ScriptMain.cs:线   279

我使用ClosedXML的每一段代码都在下面:

cx.XLWorkbook doc = new cx.XLWorkbook(docName);
cx.IXLWorksheet ws = doc.Worksheet(sheetname);

ws.SelectedRanges.Clear();
doc.SaveAs(docName, false);

private static int FindRow(cx.IXLWorksheet worksheet, string findText)
{
  int result = -1;
  cx.IXLRows usedrange = (cx.IXLRows)worksheet.RowsUsed();
  string address = string.Empty;
  foreach (cx.IXLRow row in usedrange)
  {
    int rowval = row.RangeAddress.FirstAddress.RowNumber;
    cx.IXLCell cell = worksheet.Cell(rowval, 3);
    if (cell.Value.ToString() == findText)
    {
       result = rowval;
       break;
    }
  }
  usedrange = null;
  return result;
}

private static string FindValue(cx.IXLWorksheet worksheet, int columnName, int rowval)
{
  string returnval = string.Empty;
  cx.IXLCell fcell = worksheet.Cell(rowval, columnName);
  if (fcell != null)
    returnval = fcell.Value.ToString();
  return returnval;
}

private static void ReplaceValue (cx.IXLWorksheet worksheet, string replaceText, int columnName, int rowval)
{
   cx.IXLCell rcell = worksheet.Cell(rowval, columnName);
   rcell.Value = replaceText;
}

工作表名称确实是Delinquency,其要搜索的相关数据从第3行开始,并且经过第X列。但是为什么我保存文件时会出现此错误?

(清除SelectedRanges并将usedRange设置为null都是我试图让文件正确保存的东西。)

我还在OpenXML验证器中打开了源文件,只是为了确保文件本身没有导致此问题的问题。

我做错了什么?

0 个答案:

没有答案