C# EPPlus字体系列不受影响
我使用asp.NETMVC4和epplus作为nuget软件包,将数据导出到excel文件中。我是这样做的:C# EPPlus字体系列不受影响,c#,asp.net-mvc-4,epplus,C#,Asp.net Mvc 4,Epplus,我使用asp.NETMVC4和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.Cell
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();
}
var excel=new ExcelPackage();
var工作表=excel.Workbook.Worksheets.Add(“消耗”);
workSheet.View.RightToLeft=true;
对于(var col=1;col请尝试以下方法:
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;
这将影响所有行和列出现此问题是因为EPPlus(版本4.5.3.2)不支持字体字符集。
所选字体的字体字符集(“B Zar”)为阿拉伯语(=178)。
我已经在url上分叉了EPPlus并修复了这个bug。
然后,您可以使用此代码支持波斯字体:
workSheet.Cells[1, col].Style.Font.Charset = 178;
你的问题是什么?上面的代码将改变你工作表的字体样式和大小。获取整个文档范围的一个很好的快捷方式是sheet.Cells[sheet.Dimension.Address]
workSheet.Cells[1, col].Style.Font.Charset = 178;
//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;
}