我使用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上打开时,字体系列不受影响,但在“字体名称”框中显示。如果我将焦点放在组合框上并按回车键,字体系列将受到影响。
我怎样才能解决这个问题?

最佳答案

试试这个:

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;

关于c# - EPPlus字体家族不受影响,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40032251/

10-11 22:46
查看更多