初步評估,ClosedXML支援不少Excel VBA風格的簡潔API,在ReportViewer匯出檔案相容性測試又比NPOI及EPPlus好,看起來很值得一試!
/// 將目錄下的目錄檔案結構匯出成Excel工作表
/// <param name="dirPath">要匯出的目錄路徑</param>
/// <param name="excelPath">匯出Excel路徑</param>
/// <param name="filter">過濾函數,傳入Path進行判斷,傳回true時表排除</param>
public static void WebTreeToExcel(
string dirPath, string excelPath,
Func<string, bool> filter = null)
List<WebItem> list = new List<WebItem>();
explore(list, dirPath, 0);
XLWorkbook workbook = new XLWorkbook();
var sheet = workbook.Worksheets.Add("Site Tree");
foreach (string colName in "Path;File;Description".Split(';'))
sheet.Cell(1, colIdx++).Value = colName;
var header = sheet.Range("A1:C1");
header.Style.Fill.BackgroundColor = XLColor.Green;
header.Style.Font.FontColor = XLColor.Yellow;
header.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
foreach (var item in list)
if (filter != null && filter(item.Path))
sheet.Cell(rowIdx, 1).Value = item.Path;
sheet.Cell(rowIdx, 2).Value =
new String(' ', item.Layer * 4) + item.Name;
sheet.Cell(rowIdx, 2).Style.Font.FontColor = XLColor.Blue;
sheet.Column(2).AdjustToContents();
sheet.Column(2).Width += 2;
sheet.Column(3).Width = 50;
workbook.SaveAs(excelPath);