6

编辑:这些例子应该都有效。我的问题实际上与 epplus 无关,并且此代码以及标记的答案适用于样式合并单元格。

我希望能够为合并的单元格设置样式,但是,我尝试设置它的样式没有效果。这是我合并单元格的方式:

WorkSheet.Cells["A1:K1"].Merge = true;

这是我尝试在此合并单元格上设置背景和字体颜色的方法:

WorkSheet.Cells["A1:K1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
WorkSheet.Cells["A1:K1"].Style.Fill.BackgroundColor.SetColor(Color.Black);
WorkSheet.Cells["A1:K1"].Style.Font.Color.SetColor(Color.Red);

我尝试过的另一种方法:

WorkSheet.Cells["A1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
WorkSheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.Black);
WorkSheet.Cells["A1"].Style.Font.Color.SetColor(Color.Red);

我尝试过的另一种方法:

WorkSheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
WorkSheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.Black);
WorkSheet.Cells[1, 1].Style.Font.Color.SetColor(Color.Red);

我在这里想念什么?我可以毫无困难地为其他未合并的单元格设置样式,但我合并的单元格不会改变。

4

1 回答 1

12

我无法重现该问题.. 这是合并单元格样式的示例。看看你能不能找出你做错了什么。您所要做的就是运行它。

如果您有任何疑问,请告诉我。

    var stream = new MemoryStream();

// print header
using (var package = new ExcelPackage(stream))
{
    // add a new worksheet to the empty workbook
    var worksheet = package.Workbook.Worksheets.Add("testsheet");
    for(var i = 0; i < 10; i++)
        worksheet.Cells[i + 1, 1].Value = i.ToString();

    worksheet.Cells["A1:A3"].Merge = true;
    worksheet.Cells["A1:A3"].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
    worksheet.Cells["A1:A3"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
    worksheet.Cells["A1:A3"].Style.Border.Left.Style = ExcelBorderStyle.Thin;
    worksheet.Cells["A1:A3"].Style.Border.Right.Style = ExcelBorderStyle.Thin;
    worksheet.Cells["A1:A3"].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
    worksheet.Cells["A1:A3"].Style.Fill.PatternType = ExcelFillStyle.Solid;
    worksheet.Cells["A1:A3"].Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#f0f3f5"));

     package.Save();
}

stream.Seek(0, SeekOrigin.Begin);

using (Stream file = File.Open("sample.xlsx", FileMode.Create))
{
    var buffer = new byte[8 * 1024];
    int len;
    while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
        file.Write(buffer, 0, len);
}
于 2013-12-12T18:58:49.697 回答