1

How can you copy a row in ClosedXML?

var sheet = oldFile.Worksheet(1);
IXLRow headerRow = sheet.Row(1);

var newFile = new XLWorkbook();
var ws = newFile.Worksheets.Add("Sheet to copy to");

// how to copy header row to ws?

// this doesn't work
// ws.Cell(1, 1).InsertData(headerRow);

Additionally, say I had a collection of rows from the oldFile.

var rowsIWantToCopy = someColumn.CellsUsed()
                                .Where(c => c.Value.ToString() == "someValue")
                                .Select(c => c.WorksheetRow())
                                .ToList();

// copy rows to a new worksheet

I have tried utilizing the ranges of the columns too. That doesn't seem to work because they are the incorrect range for the new file.

thalacker
  • 2,389
  • 3
  • 23
  • 44

1 Answers1

-1

You can try using this

ws.Range("A1").Value = headerRow;

The collection probably needs to be copied line by line.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 15:10
  • Not working: "The left-hand side of an assignment must be a variable, property or indexer" – Bastien Vandamme May 29 '23 at 10:14