Merge cells using EPPlus?

我正在使用 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。我以为这样可以把细胞合并,但是没有。

有人知道怎么做吗?

90893 次浏览

You have to use it like this:

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

而不是:

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

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

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

所有这些变量都是整数。

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

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;
}

You can use this as you would via interop:

range.Merge();

enter image description here Int inicio = CELDA _ CUERPO; 布尔值 = 真值;

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


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

//Pintar cabecera:

    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;
}
       

}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
}