2017-10-17 10 views
2

Le texte d'une cellule fusionnée entre les lignes est coupé car Excel ignore les cellules fusionnées lors du redimensionnement automatique d'une ligne.OfficeOpenXML excel Redimensionnement des lignes avec des cellules fusionnées entre elles

Comment puis-je redimensionner la hauteur de la cellule à son contenu pour les lignes pour lesquelles le redimensionnement ne fonctionne pas?

Je poste ceci là pour aider d'autres qui ont eu ce problème. J'ai posté une solution en guise de réponse.

+0

@TomBlodget Merci. Les modifications pour faciliter la lecture. –

Répondre

2

Pour cette solution, je ne suis montrant le code lié à redimensionner les lignes n'affichant des données

Solution:

/// 
/// Merged Row Model Psuedo Code 
/// 
MergedRow { 
    // Fields merged across rows 
    ... 
    // list of rows with non merged data 
    list<indvidulerow> data 
} 

/// 
/// Code to find rows with merged cells. 
/// 

... // worksheet prep 
var mergedRows = new Dictionary<int, MergedRow>(); 
... // begin generating rows 
... 
// code generating the first merged cell of a row 
using (var r = sheet.Cells[rowIndex, 1, rowIndex+ mergedRow.Data.Count - 1, 1]){ 
if(mergedRow.Data.count>1){ 
    r.Merge=true; 
    mergedRows.Add(rowIndex, mergedRow); 
} 
... // code for remainder of of cell 


/// 
/// Code to Resize Rows. Making last row expand enough to see 
/// all content in merged cells 
/// 

foreach (var firstRowIndex in mergedRows.Keys){ 
    var numRowsMerged = mergedRows[firstRowIndex].Data.Count; 
    var columnHeights = new List<ColumnHeightCalcModel>(); 
    for (var colIndex = 0; colIndex < totalColumns; colIndex++){ 
     var calcModel = new ColumnHeightCalcModel(); 
     var combinedHeight = 0.0; 
     var lastRowHeight = 0.0; 
     for (rowIndex = 0; rowIndex < numRowsMerged; rowIndex++){ 
      var cell = sheet.Cells[firstRowIndex + rowIndex, colIndex + 1]; 
      var cellText = cell.Text; 
      var cellmerged = cell.Merge; 
      var cellWidth = sheet.Column(colIndex+1).Width; 
      var font = cell.Style.Font; 
      if (string.IsNullOrEmpty(cellText)) combinedHeight += 0.0; 
      var bitmap =new Bitmap(1,1); 
      var graphics = Graphics.FromImage(bitmap); 
      var pixelWidth = Convert.ToInt32(cellWidth * 6.0); //6.0 pixels per excel column width 
      var drawingFont = new Font(font.Name, font.Size); 
      var size = graphics.MeasureString(cellText, drawingFont, pixelWidth); 

      //72 DPI and 96 points per inch. Excel height in points with max of 409 per Excel requirements. 
      combinedHeight += Math.Min(Convert.ToDouble(size.Height) * 72/96, 409); 
      if (cellmerged) break; 
      lastRowHeight = Math.Min(Convert.ToDouble(size.Height) * 72/96, 409);       
     } 
     calcModel.TotalCombinedHeight = combinedHeight; 
     calcModel.LastRowHeight = lastRowHeight; 
     columnHeights.Add(calcModel); 
    } 
    var row = sheet.Row(firstRowIndex + numRowsMerged - 1); 
    row.CustomHeight = true; 
    var maxCombo = columnHeights.Max(col => col.TotalCombinedHeight); 
    var maxLast = columnHeights.Max(col => col.LastRowHeight); 
    row.Height = maxCombo - maxLast; 
} 

Solution Fin

Liens qui m'a aidé à trouver cette solution

Le lien suivant était pour les colonnes fusionnées mais il a aidé avec les lignes fusionnées.

Autofit Row Height of Merged Cell in EPPlus