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

时间:2015-06-22 04:34:27

标签: c# asp.net-mvc-5 epplus

在我的导出ActionResult中,我能够将模型加载到ExcelPackage中。

我遇到问题的地方是在应用LoadFromCollection后为每个单元格分配边框。正确应用AutoFitColumns时,我应用的边框样式仅适用于Cells["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(); 

3 个答案:

答案 0 :(得分:44)

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

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

或者,有更多背景。我验证了EPPlus将接受Cells []中的字符串变量,这允许我选择整个表格并正确应用我的边框样式和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();

答案 1 :(得分:1)

这可以解决问题-worksheet.Cells [worksheet.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;
        }

答案 2 :(得分:0)

        var package = new ExcelPackage(new MemoryStream());
        var ws = package.Workbook.Worksheets.Add("Test");
        var modelTable = ws.Cells;
        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;
        modelTable.AutoFitColumns();
        // calculate
        ws.Calculate();

        saveFileDialog_SaveExcel.Filter = "Excel files (*.xlsx)|*.xlsx";
        var dialogResult = saveFileDialog_SaveExcel.ShowDialog();
        if (dialogResult == DialogResult.OK)
        {
            package.SaveAs(new FileInfo(saveFileDialog_SaveExcel.FileName));
        }