EPPlus字体系列不受影响。

18

我正在使用asp.net MVC 4和epplus作为nuget包,将我的数据导出到Excel文件中。我是这样做的:

var excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Consumption");
workSheet.View.RightToLeft = true;
for (var col = 1; col <= totalCols; col++)
{
    workSheet.Cells[1, col].Style.Font.Name = "B Zar";
    workSheet.Cells[1, col].Style.Font.Size = 16;
    workSheet.Cells[1, col].Style.Font.Bold = true;
    workSheet.Cells[1, col].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    workSheet.Cells[1, col].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
    workSheet.Cells[1, col].Value = ds.Tables[0].Columns[col - 1].ColumnName;
}

for (var row = 1; row <= totalRows; row++)
    for (var col = 0; col < totalCols; col++)
    {
        workSheet.Cells[row + 1, col + 1].Style.Font.Name = "B Zar";
        workSheet.Cells[row + 1, col + 1].Style.Font.Size = 11;
        workSheet.Cells[row + 1, col + 1].Value = ds.Tables[0].Rows[row - 1][col];
    }

workSheet.Cells[1, 1, totalRows + 1, totalCols].Style.Border.Top.Style =
    workSheet.Cells[1, 1, totalRows + 1, totalCols].Style.Border.Bottom.Style =
        workSheet.Cells[1, 1, totalRows + 1, totalCols].Style.Border.Right.Style =
            workSheet.Cells[1, 1, totalRows + 1, totalCols].Style.Border.Left.Style =
                OfficeOpenXml.Style.ExcelBorderStyle.Thin;
workSheet.Cells[1, 1, totalRows + 1, totalCols].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

using (var memoryStream = new MemoryStream())
{
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=Consumptions.xlsx");
    excel.SaveAs(memoryStream);
    memoryStream.WriteTo(Response.OutputStream);
    Response.Flush();
    Response.End();
}

问题在于当我下载文件并在Excel 2016中打开时,字体族没有受到影响,但在字体名称框中会显示。如果我专注于组合框并按Enter键,则字体族将受到影响。

我该如何解决这个问题?

5个回答

25

试一下这个:

var allCells = sheet.Cells[1, 1, sheet.Dimension.End.Row, sheet.Dimension.End.Column];
var cellFont = allCells.Style.Font;
cellFont.SetFromFont(new Font("Times New Roman", 12));
cellFont.Bold = true;
cellFont.Italic = true;

4
获取整个文档范围的简便方法是:sheet.Cells[sheet.Dimension.Address]。 - Nick

9
workSheet.Cells.Style.Font.Name = "Arial Narrow";
workSheet.Cells.Style.Font.Size = 10;

这将影响所有的行和列。

4
这个问题的原因是EPPlus(版本4.5.3.2)不支持字体字符集。所选字体('B Zar')的字体字符集为ARABIC(=178)。我已经在https://github.com/mzatkhahi/EPPlus上派生了EPPlus并修复了这个错误。然后,您可以使用此代码来支持波斯字体。
workSheet.Cells[1, col].Style.Font.Charset = 178;

1
这个问题发生在使用EPPlus时,当我们使用阿拉伯语或波斯语字体时,我们必须使用字符集,这样才能正常工作。
var allCells = workSheet.Cells[1, 1, workSheet.Dimension.End.Row, 
workSheet.Dimension.End.Column];
var cellFont = allCells.Style.Font;
cellFont.SetFromFont("B Nazanin", 16);
cellFont.Charset = 178;

0
//For specific cell range
using (var range = worksheet.Cells[From Row, From Column, To Row, To Column]) 
{
         range.Style.Font.Bold = true;
}
//For understanding,   
//Column Number = Worksheet.Dimension.End.Column   
//Row Number = Worksheet.Dimension.End.Row

// OR  
//For Whole row 
using(var package = new OfficeOpenXml.ExcelPackage())
{
    worksheet.Row(5).CustomHeight = false;
    worksheet.Row(5).Height = 50;
    worksheet.Row(5).Style.Font.Bold = true ;
    worksheet.Row(5).Style.WrapText = true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接