如何将 Excel 列与动态值合并

How to merge Excel column with dynamic value

我试图在前 2 行中有相同值时合并 excel 列。该值来自我的数据库查询。目前我生成的是这样的:

我想像这样合并列:

这是我的函数代码:

 public static async Task<MemoryStream> getNewListingExcelWithContentColumnMerge<T>(List<T> list, List<string> header, string name, string headerTitle)
    {
        try
        {
            int row = 3;
            int col = 1;

            MemoryStream output = new MemoryStream();
            using (ExcelPackage package = new ExcelPackage())
            {
                ExcelWorksheet ws = package.Workbook.Worksheets.Add(name);
                ws.PrinterSettings.PaperSize = ePaperSize.A4;
                ws.PrinterSettings.FitToPage = true;

                ws.Cells["A1:D1"].Merge = true;
                ws.Cells["A1:D1"].Value = headerTitle;

                for (int i = 0; i < header.Count(); i++)
                {
                    ws.Cells[row, col].Value = header[i];
                    col++;

                }

                ws.Cells[1, 1, 1, col].Style.Font.Bold = true;

                col = 1;
                row++;
                for (int i = 0; i < list.Count(); i++)
                {
                    Type type = list[i].GetType();
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo property in properties)
                    {
                        object asd = property.GetValue(list[i], null);
                        //Add data to the next line if consist of '\n'.
                        if (asd != null && asd.ToString().Contains(Environment.NewLine))
                        {
                            var splitValue = asd.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                            var sameValuePlacementList = new List<int>();
                            for (var j = 0; j < splitValue.Count(); j++)
                            {
                                if (j == 0)
                                {
                                    ws.Cells[row, col].Value = splitValue[j];
                                    ws.Cells[row, col].Style.WrapText = true;
                                }
                                else
                                {
                                    ws.Cells[row, col].RichText.Add(Environment.NewLine + splitValue[j]);
                                }
                            }
                        }
                        else
                        {
                            ws.Cells[row, col].Value = asd;
                        }
                        col++;
                    }
                    col = 1;
                    row++;
                }

                ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].AutoFitColumns();
                ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Left.Style = ExcelBorderStyle.Thin;

                package.SaveAs(output);

                return output;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }

接下来我可以尝试什么?

你可以这样做:

ws.Range[ws.Cells[1, 1], ws.Cells[4, 1]].Merge();

它将合并第 1 列中从 1 到 4 的行。

                int tempRow = startRowForMerge;
                int firstSameRowIndex = 0;
                int lastSameRowIndex = 0;
                int maxCount = tempRow + list.Count() - 1; // -1 because array start from 0
                bool needToMerge = false;
              
                foreach(int tempColumn in mergeColumnList)
                {
                    tempRow = startRowForMerge;
                    for (int i = 0; i < list.Count(); i++)
                    {
                        if (tempRow < maxCount)
                        {
                            if (row > 4 && ws.Cells[tempRow, tempColumn].Value.ToString() == ws.Cells[tempRow - 1, tempColumn].Value.ToString())
                            {
                                needToMerge = true;
                                if (firstSameRowIndex == 0)
                                {
                                    firstSameRowIndex = tempRow - 1;
                                }
                                    lastSameRowIndex = tempRow;
                            }
                            else
                            {
                                if (needToMerge == true)  // to merge data in between the list
                                {
                                    ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Merge = true;
                                    ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                    needToMerge = false;
                                    firstSameRowIndex = 0;
                                    lastSameRowIndex = 0;
                                }
                            }
                        }
                        else
                        {
                            if (needToMerge == true)  // to merge data in last row 
                            {
                                ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Merge = true;
                                ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                needToMerge = false;
                                firstSameRowIndex = 0;
                                lastSameRowIndex = 0;
                            }
                        }

                        tempRow++;
                    }
                }