[英]EPPlus How to retrieve Excel cell formatting into DataGridView?
如何使用EPPlus格式从保存的工作表从Excel加载到DatagridView?
将数据放入DatagridView。
private void importFastExcelCompare(DataGridView dt, string excelFileName,string sheetName)
{
FileInfo fileInfo = new FileInfo(excelFileName);
using (ExcelPackage xlPackage = new ExcelPackage(fileInfo))
{ //create a list to hold all the values
// get the first worksheet in the workbook
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[sName];
// Fetch the WorkSheet size
ExcelCellAddress startCell = worksheet.Dimension.Start;
ExcelCellAddress endCell = worksheet.Dimension.End;
for (Int32 row = startCell.Row; row <= endCell.Row; row++)
{ // place all the data into DataTable
dt.Rows.Add();
for (int col = startCell.Column; col <= endCell.Column; col++)
{
var excelCell = worksheet.Workbook.Worksheets.First().Cells[row, col];
var gridViewCell = dt.Rows[row - 1].Cells[col - 1];
using (var range = excelCell.Worksheet.Cells[row, col, row, col])
{
// gridViewCell.Style.ForeColor = range.Style.Font.Color;
//gridViewCell.Style.ForeColor = System.Drawing.Color.FromArgb(range.Style.Font.Color);
}
gridViewCell.Value = excelCell.Value;
}
}
}
}
请帮忙。 谢谢。
EPPlus单元格具有样式属性。 例如,
worksheet.Cells["C2:C5"].Style.Numberformat.Format
将为您提供单元格的数字格式。 同样,您可以访问以下属性以获取更多的单元格格式。
worksheet.Cells["C2:C5"].Style.Font
worksheet.Cells["C2:C5"].Style.Fill
worksheet.Cells["C2:C5"].Style.Font.Color
您可以查看https://github.com/JanKallman/EPPlus/wiki了解更多详细信息。
对我来说唯一有效的方法是分别评估字体设置。 不幸的是,加载将大大减慢速度。 也许有更好的解决方案....
private void gridFont(ExcelRangeBase eFont, DataGridViewCell dtCell)
{
if (eFont.Style.Font.Bold && eFont.Style.Font.Italic && eFont.Style.Font.UnderLine)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Bold | FontStyle.Underline | FontStyle.Italic);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.Bold && eFont.Style.Font.Italic)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Bold | FontStyle.Italic);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.Bold && eFont.Style.Font.UnderLine)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Bold | FontStyle.Underline);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.Italic && eFont.Style.Font.UnderLine)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Italic | FontStyle.Underline);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.Bold)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Bold);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.Italic)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Italic);
dtCell.Style.Font = myFont;
}
else
{
if (eFont.Style.Font.UnderLine)
{
System.Drawing.Font myFont = new System.Drawing.Font(eFont.Style.Font.Name, eFont.Style.Font.Size, FontStyle.Underline);
dtCell.Style.Font = myFont;
}
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.