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:
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); } }
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!
Good afternoon!Yes, you understood me perfectly correctly!
Thank you for your advice!
I tried it and it really is a little less heavy.
It is very sad that there is no such mechanism now. I'll try to post the idea, I hope this will give the result.Thank you!
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.