2016-10-13 45 views
3

我使用asp.net MVC 4和epplus作为Nuget包导出我的数据到excel文件。我这样做,如下所示:EPPlus字体家庭不受影响

 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键,字体系列将受到影响。

我该如何解决这个问题?

回答

1

答案就在这里:

当我改变字形,全身字体,如“宋体”,“宋体”,等等。它的影响,但是当我也使用另一种字体!

这是因为安装的字体应该是真正的类型,而我尝试使用的字体不是真正的类型。

3

试试这个:

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; 
1
workSheet.Cells.Style.Font.Name = "Arial Narrow"; 
workSheet.Cells.Style.Font.Size = 10; 

这将影响到所有

+0

你的问题是什么?上面的代码将改变你的崇拜单的字体样式和大小。 –