如何使用gembox电子表格在excell中复制和插入特定的行

请帮忙。 我使用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"); } 

你不能像这样使用: 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"); 

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

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