I am using asp.net MVC 4 and epplus as a nuget package to export my data to an excel file. I do it as follows:
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(); }
The problem is that when I download the file and open it in Excel 2016, the font family is not affected, and appears in the font name field. If I focus on the combo box and press Enter, this will affect the font family.
How can I solve this problem?
source share