Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
35
Copy cells or Region in Excel framework
posted

Good day!

I'm not sure I chose the correct section for the question. I hope it's not a problem.

The question is this: I use the excel framework to work with files and fill in the information in them.

In my work, I need to copy the rows of tables, along with the design and formatting. For this, I created a new sheet called Template. On this sheet, I store the lines that need to be copied to the main sheet. But since the row contains merged cells, as well as the cells have different formatting. The following code does not work

ws.Rows[RowResult].CellFormat.SetFormatting(ws2.Rows[RowSource].CellFormat);

For this reason I have to act in a complicated way

In a sheet with a template, I store a sequence of numbers that tell which cells should be merged.

As example: 

1|4|4|2|1|1|1|1|2|3|3

This means that the first cell is not merged, then there are 4 merged, then 4 more, and so on.

And then I apply the following code.

private void CopyRow(Worksheet ws, Worksheet ws2, int RowSource, int RowResult, int CellsStart, int CellsStop) {
string[] map = ws2.Rows[RowSource].Cells[CellsStop + 1].Value.ToString().Split('|');
  int curPos = CellsStart;
  foreach (string s in map) {
    int c = int.Parse(s);
    if (c > 1) {
      WorksheetMergedCellsRegion r = ws.MergedCellsRegions.Add(RowResult, curPos, RowResult, curPos + (c - 1));
    }
    curPos += c;
  }
  for (int i = CellsStart; i <= CellsStop; i++) {
    ws.Rows[RowResult].Cells[i].CellFormat.SetFormatting(ws2.Rows[RowSource].Cells[i].CellFormat);
  }
}

This code is working. But very very cumbersome and heavy.

Tell me please, is it possible to somehow copy the Region together with the merged cells and their formatting?

Thank you!

Parents
  • 34810
    Offline posted

    Hello Nikolay,

    I have been investigating into your requirement, and from your description and the code you have provided, it looks like you are essentially trying to copy over the merged cell region and formatting of your cells over from one worksheet to another. Please correct me if this impression is inaccurate, as the following is based on it.

    If the above impression is accurate, there does not currently exist a way to "copy" over the merged cell region and formatting over directly. It is worth noting, that the code you have provided could likely be made a little  less "heavy" by utilizing the CellFormat property on the WorksheetMergedCellRegion instead. Perhaps if you created a WorksheetMergedCellsRegion in your "ws2" worksheet, you could format these cells in a "lighter" way by formatting the WorksheetMergedCellsRegion, rather than doing it cell by cell? I am still a little bit unsure of the exact functionality you are looking to achieve in this case, though.

    If you would like to see the ability to copy over cell formats and merged cells, I would recommend suggesting a new product idea for this at https://es.infragistics.com/community/ideas/i/ultimate-ui-for-wpf. This will place you in direct communication with our product management teams who plan and prioritize upcoming features and development based on community and user feedback.

    Please let me know if you have any other questions or concerns on this matter.

Reply Children
No Data