使用 EPPlus 合并单元格?

问题描述 投票:0回答:4

我正在使用 EPPlus 库读取/写入 Excel 文件:http://epplus.codeplex.com/

我试图在编写文档时简单地合并一些单元格:

using (ExcelPackage pck = new ExcelPackage())
{
    //Create the worksheet
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

    //Format the header for column 1-3
    using (ExcelRange rng = ws.Cells["A1:C1"])
    {
        bool merge = rng.Merge;
    }
}

有一个名为 Merge 的属性,它只返回 true 或 false。我以为这可能会合并单元格,但事实并非如此。

有人知道该怎么做吗?

c# asp.net excel epplus
4个回答
201
投票

你必须像这样使用它:

ws.Cells["A1:C1"].Merge = true;

而不是:

using (ExcelRange rng = ws.Cells["A1:C1"])
{
    bool merge = rng.Merge;
}

92
投票

如果要动态合并单元格,还可以使用:

worksheet.Cells[FromRow, FromColumn, ToRow, ToColumn].Merge = true;

所有这些变量都是整数。


11
投票

您可以创建一个扩展方法:

public static void Merge(this ExcelRangeBase range)
{
    ExcelCellAddress start = range.Start;
    ExcelCellAddress end = range.End;
    range.Worksheet.Cells[start.Row, start.Column, end.Row, end.Column].Merge = true;
}

您可以像通过互操作一样使用它:

range.Merge();

-2
投票

enter image description here int inicio = CELDA_CUERPO; 布尔值 = true;

    int COLUMNA_A = 0;
    int finx_a = 0;
    int finy_a = 0;

    int COLUMNA_B = 1;
    int finx_b = 0;
    int finy_b = 0;

//平塔卡贝塞拉:

    for (int i = 0; i < ListaCabecera.Length; i++)
    {
        celda = $"{letras[i]}{CELDA_CABECERA}";

        if (i == 0)
        {
            inicioRango = celda;
        }

        Sheet.Cells[celda].Value = ListaCabecera[i];
        //System.Drawing.Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B7DEE8");
        Sheet.Cells[celda].Style.Font.Color.SetColor(Color.FromArgb(0, 124, 183));
        Sheet.Cells[celda].Style.Fill.PatternType = ExcelFillStyle.Solid;
        Sheet.Cells[celda].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(232, 235, 249));
        Sheet.Cells[celda].Style.Font.Bold = true;
        Sheet.Cells[celda].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    }

    //Pintar detalle:
    for (int i = 0; i < Datos.GetLength(0); i++)
    {
        for (int j = 0; j < Datos.GetLength(1); j++)
        {
            celda = $"{letras[j]}{CELDA_CUERPO + i}";
            finalizaRango = celda;

            if (j < 3) if (Datos[i, j].valor != null && Datos[i, j + 1].valor == null)
                {
                    Sheet.Cells[celda].Style.Font.Color.SetColor(Color.FromArgb(156, 0, 6));
                    Sheet.Cells[celda].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    Sheet.Cells[celda].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 199,206));
                }
            Sheet.Cells[celda].Value = Datos[i, j].valor;
        }

        //::::::::::::::::::: MERGE A ::::::::::::::::::::
        if (i < Datos.GetLength(0) - 1)
        {
            // :::::::::::::::::::::: x :::::::::::::::::::::::::::::::::
            if (values)
            {
                if (Datos[i, COLUMNA_A].valor == Datos[i, COLUMNA_A].valor)
                {
                    values = false;
                    finx_a = inicio + i;
                    finx_b = inicio + i;
                    //list_x.Add(finx_b);
                }
            }
            else
            {
                if (Datos[i - 1, COLUMNA_A].valor != Datos[i, COLUMNA_A].valor)
                {
                    finx_a = inicio + i;
                }

                if (Datos[i - 1, COLUMNA_B].valor != Datos[i, COLUMNA_B].valor)
                {
                    finx_b = inicio + i;
                    //list_x.Add(finx_b);
                }
             }

            // :::::::::::::::::::::: y (A) :::::::::::::::::::::::::::::::::
            if (Datos[i, COLUMNA_A].valor != Datos[i + 1, COLUMNA_A].valor)
            {
                finy_a = inicio + i;
                //list_y.Add(finy);
                Sheet.Cells[$"A{finx_a}:A{finy_a}"].Value = Datos[i, COLUMNA_A].valor;
                Sheet.Cells[$"A{finx_a}:A{finy_a}"].Merge = true;
                Sheet.Cells[$"A{finx_a}:A{finy_a}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            }

            // :::::::::::::::::::::: y (B) :::::::::::::::::::::::::::::::::
            if (Datos[i, COLUMNA_B].valor != Datos[i + 1, COLUMNA_B].valor)
            {
                finy_b = inicio + i;
                //list_y.Add(finy_b);
                Sheet.Cells[$"B{finx_b}:B{finy_b}"].Value = Datos[i, COLUMNA_B].valor;
                Sheet.Cells[$"B{finx_b}:B{finy_b}"].Merge = true;
                Sheet.Cells[$"B{finx_b}:B{finy_b}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            }
       
         }
        //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     }
© www.soinside.com 2019 - 2024. All rights reserved.