Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EPPlus:应用LoadFromCollection后,如何在每个单元格周围分配边框?_C#_Asp.net Mvc 5_Epplus - Fatal编程技术网

C# EPPlus:应用LoadFromCollection后,如何在每个单元格周围分配边框?

C# EPPlus:应用LoadFromCollection后,如何在每个单元格周围分配边框?,c#,asp.net-mvc-5,epplus,C#,Asp.net Mvc 5,Epplus,在我的export ActionResult中,我能够将模型加载到我的ExcelPackage中 当应用LoadFromCollection时,我遇到的问题是在每个单元格周围分配一个边框。虽然AutoFitColumns正确应用,但我应用的边框样式仅适用于单元格[“D1”],而不适用于表 BorderAround成功地在整个表周围放置边框,但我更愿意将边框应用于表内的单元格。我有办法做到吗 // Fill worksheet with data to export var modelCells

在我的export ActionResult中,我能够将模型加载到我的ExcelPackage中

当应用
LoadFromCollection
时,我遇到的问题是在每个单元格周围分配一个边框。虽然
AutoFitColumns
正确应用,但我应用的边框样式仅适用于
单元格[“D1”]
,而不适用于表

BorderAround
成功地在整个表周围放置边框,但我更愿意将边框应用于表内的单元格。我有办法做到吗

// Fill worksheet with data to export
var modelCells = worksheet.Cells["D1"];
var border = modelCells.Style.Border.Top.Style = modelCells.Style.Border.Left.Style = modelCells.Style.Border.Right.Style = modelCells.Style.Border.Bottom.Style = ExcelBorderStyle.Medium;                    

modelCells
    .LoadFromCollection(Collection: exportQuery, PrintHeaders: true)
    .AutoFitColumns(); 

如果我知道模型的列数,我可以使用函数计算行数并执行以下操作:

var modelRows = exportQuery.Count()+1;    
string modelRange = "D1:F" + modelRows.ToString();
var modelTable = worksheet.Cells[modelRange];
或者,有更多的背景。我验证了EPPlus将接受单元格[]中的字符串变量,这允许我选择整个表并正确应用边框样式和
AutoFitColumns{}
。我需要手动做的就是在
modelRange
变量中输入起始列和结束列

var modelCells = worksheet.Cells["D1"];
var modelRows = exportQuery.Count()+1;    
string modelRange = "D1:F" + modelRows.ToString();
var modelTable = worksheet.Cells[modelRange];

// Assign borders
modelTable.Style.Border.Top.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Left.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Right.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;


// Fill worksheet with data to export
modelCells.LoadFromCollection(Collection: exportQuery, PrintHeaders: true);
modelTable.AutoFitColumns();

这将完成技巧-工作表.Cells[工作表.Dimension.Address]

using (ExcelPackage excel = new ExcelPackage())
        {
            excel.Workbook.Worksheets.Add(sheetName);

            excel.SaveAs(excelFile);

            string headerRange = "A1:" + char.ConvertFromUtf32(dtJobs.Columns.Count + 64) + "1";

            // Target a worksheet
            var worksheet = excel.Workbook.Worksheets[sheetName];

            #region design Header
            //worksheet.Cells[headerRange].Style.Font.Bold = true;
            worksheet.Cells[headerRange].Style.Font.Size = 11;
            worksheet.Cells[headerRange].Style.Fill.PatternType = ExcelFillStyle.Solid;
            worksheet.Cells[headerRange].Style.Fill.BackgroundColor.SetColor(Color.DarkGray);
            //worksheet.Cells[headerRange].Style.WrapText = true;
            worksheet.Cells[headerRange].Style.Font.Color.SetColor(Color.White);
            worksheet.Cells[headerRange].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            worksheet.Cells[headerRange].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            #endregion

            var excelWorksheet = excel.Workbook.Worksheets[sheetName];

            excelWorksheet.Cells[excelWorksheet.Dimension.End.Row, 1].LoadFromDataTable(dtJobs, true);

            worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Top.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Left.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Right.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

            excel.SaveAs(excelFile);
            return filePath;
        }

虽然您似乎不需要
var border=
,因为它从未被使用过,但它似乎是有效的。虽然变量没有直接使用,但等号链的作用是打开所有边上的边界。为了清楚起见,我将重新编写它,使其更加明确。当然,我的意思是,
var border=
部分是多余的,而不是整行,但现在看起来确实更清晰了。在excelrange周围添加边框的捷径是:
modelTable.Style.border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin)
using (ExcelPackage excel = new ExcelPackage())
        {
            excel.Workbook.Worksheets.Add(sheetName);

            excel.SaveAs(excelFile);

            string headerRange = "A1:" + char.ConvertFromUtf32(dtJobs.Columns.Count + 64) + "1";

            // Target a worksheet
            var worksheet = excel.Workbook.Worksheets[sheetName];

            #region design Header
            //worksheet.Cells[headerRange].Style.Font.Bold = true;
            worksheet.Cells[headerRange].Style.Font.Size = 11;
            worksheet.Cells[headerRange].Style.Fill.PatternType = ExcelFillStyle.Solid;
            worksheet.Cells[headerRange].Style.Fill.BackgroundColor.SetColor(Color.DarkGray);
            //worksheet.Cells[headerRange].Style.WrapText = true;
            worksheet.Cells[headerRange].Style.Font.Color.SetColor(Color.White);
            worksheet.Cells[headerRange].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            worksheet.Cells[headerRange].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            #endregion

            var excelWorksheet = excel.Workbook.Worksheets[sheetName];

            excelWorksheet.Cells[excelWorksheet.Dimension.End.Row, 1].LoadFromDataTable(dtJobs, true);

            worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Top.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Left.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Right.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[worksheet.Dimension.Address].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

            excel.SaveAs(excelFile);
            return filePath;
        }