EPPlus: LoadFromCollection を適用した後、各セルの周囲に境界線を割り当てるにはどうすればよいですか? 質問する

EPPlus: LoadFromCollection を適用した後、各セルの周囲に境界線を割り当てるにはどうすればよいですか? 質問する

エクスポート 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(); 

ベストアンサー1

モデルの列数がわかっている場合は、関数を使用して行数をカウントし、次のように実行できます。

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();

おすすめ記事