2016-10-13 46 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

你的問題是什麼?上面的代碼將改變你的崇拜單的字體樣式和大小。 –