|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO; |
3 | 4 | using GeneXus.Office.Excel; |
4 | 5 | using OfficeOpenXml; |
@@ -104,6 +105,7 @@ public short Save() |
104 | 105 | { |
105 | 106 | using (var stream = new MemoryStream()) |
106 | 107 | { |
| 108 | + ApplyPendingAutoFit(); |
107 | 109 | p.SaveAs(stream); |
108 | 110 | p.Dispose(); |
109 | 111 | GxFile file = new GxFile(Path.GetDirectoryName(xlsFileName), xlsFileName, GxFileType.Private); |
@@ -328,6 +330,22 @@ public short AutoFit |
328 | 330 | private String errDescription = "OK"; |
329 | 331 | private short readOnly = 0; |
330 | 332 | private bool autoFit = false; |
| 333 | + private HashSet<(ExcelWorksheet sheet, int col)> _pendingAutoFitColumns = new HashSet<(ExcelWorksheet, int)>(); |
| 334 | + |
| 335 | + internal void MarkColumnForAutoFit(ExcelWorksheet sheet, int column) |
| 336 | + { |
| 337 | + _pendingAutoFitColumns.Add((sheet, column)); |
| 338 | + } |
| 339 | + |
| 340 | + private void ApplyPendingAutoFit() |
| 341 | + { |
| 342 | + foreach (var (sheet, col) in _pendingAutoFitColumns) |
| 343 | + { |
| 344 | + sheet.Column(col).AutoFit(); |
| 345 | + } |
| 346 | + _pendingAutoFitColumns.Clear(); |
| 347 | + } |
| 348 | + |
331 | 349 |
|
332 | 350 | public short UnBind() { return -1; } |
333 | 351 | public short Hide() { return -1; } |
@@ -593,9 +611,8 @@ public string Text |
593 | 611 | } |
594 | 612 | else |
595 | 613 | pCells[i].Value = value; |
596 | | - |
597 | | - fitColumnWidth(); |
598 | 614 | } |
| 615 | + fitColumnWidth(); |
599 | 616 | } |
600 | 617 | catch (Exception e) |
601 | 618 | { |
@@ -631,7 +648,7 @@ private void fitColumnWidth() |
631 | 648 | { |
632 | 649 | for (int columnIndex = pCellsRange.Start.Column; columnIndex <= pCellsRange.End.Column; columnIndex++) |
633 | 650 | { |
634 | | - pSelectedSheet.Column(columnIndex).AutoFit(); |
| 651 | + doc.MarkColumnForAutoFit(pSelectedSheet, columnIndex); |
635 | 652 | } |
636 | 653 | } |
637 | 654 | } |
|
0 commit comments