【问题标题】:how to copy and insert a specific rows at excell with gembox spreadsheet如何使用 gembox 电子表格在 excel 中复制和插入特定行
【发布时间】:2013-03-01 11:36:36
【问题描述】:

请帮忙。我使用 gembox.spreadsheet 库在 excel 文件的 2 张工作表中的特定行处插入和复制。 但它仍然存在无效参数的问题。

public void InsertCopyData()
{
    SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
    ExcelFile ef = new ExcelFile();

    // Loads Excel file.
    ef.LoadXls(@"C:\templateExcel\DataTable.xls");

    // Selects first and 2nd worksheet.
    ExcelWorksheet w1 = ef.Worksheets[0];
    ExcelWorksheet w2 = ef.Worksheets[1];

    //insert copy file  
    w1.InsertCopy(w1.Rows["A1"], w2.Rows["A4"]);


    //Saves the file in XLS format.
    ef.SaveXls(@"C:\templateExcel\Insert DataTable.xls");
}

【问题讨论】:

    标签: c# excel copy row gembox-spreadsheet


    【解决方案1】:

    你不能这样使用:ef.LoadXls(@"C:\templateExcel\DataTable.xls");

    您应该写入以加载现有的 Excel 文件。

    GemBox.Spreadsheet.ExcelFile ef = new GemBox.Spreadsheet.ExcelFile();
    ef = GemBox.Spreadsheet.**ExcelFile.Load**("D:\\Example.xlsx");
    ExcelWorksheet ws = ef.Worksheets["Sheet1"];  (or)   ExcelWorksheet ws = ef.Worksheets[0]; 
    // writing data to excel file code...
    ws.Cells[0, 0].Value = example_1;
    ws.Cells[1, 0].Value = example_2;
    ef.Save("D:\\Example.xlsx");
    

    【讨论】:

      【解决方案2】:

      请注意,GemBox.Spreadsheet 可以让您插入工作表、行和/或列。 您与无效参数一起使用的 API 用于插入工作表副本,为了插入行副本,请使用以下内容:

      // Inserts specified number of copied rows before the current row.
      var currentRow = w1.Rows["A1"];
      currentRow.InsertCopy(1, w2.Rows["A4"]);
      

      【讨论】:

      • 这可行,但会导致两个问题: ArgumentOutOfRangeException - 调用 GetUsedCellRange(true) 时,行索引不能大于最大行索引 (1048575) b。 Excel 2013 显示警告“我们发现某些内容存在问题”
      • 恐怕我需要调查您的输入和输出 Excel 文件才能确切地告诉您问题出在哪里。您应该通过this link 提交支持请求,并提供一些信息(或文件),以便我们能够重现您的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多