namespace DumpWebTree
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
public static class Utility
{
//簡易的目錄/檔案資料物件
public class WebItem
{
public int Layer; //目錄深度
public bool IsFolder; //是否為目錄
public string Name; //目錄或檔案名稱
public string Path; //完整路徑
public WebItem(string path, int layer, bool isFolder = false)
{
Name = System.IO.Path.GetFileName(path);
IsFolder = isFolder;
Path = path;
Layer = layer;
}
}
//用遞迴巡弋所有目錄、檔案
private static void explore(List<WebItem> list, string path, int layer)
{
WebItem folder = new WebItem(path, layer, true);
list.Add(folder);
foreach (string d in Directory.GetDirectories(path))
explore(list, d, layer + 1);
foreach (string f in Directory.GetFiles(path))
{
WebItem file = new WebItem(f, layer + 1);
list.Add(file);
}
}
//使用Extension Method擴充方法,以較簡便方式建立CellStyle
public static ICellStyle QuickCreateStyle(
this HSSFWorkbook wb,
short foreColor, //前景色
short backgroundColor = -1, //背景色
HorizontalAlignment textAlignment = HorizontalAlignment.LEFT
)
{
var style = wb.CreateCellStyle();
var font = wb.CreateFont();
font.Color = foreColor;
style.SetFont(font);
if (backgroundColor >= 0)
{
style.FillForegroundColor = backgroundColor;
style.FillPattern = FillPatternType.SOLID_FOREGROUND;
}
style.Alignment = textAlignment;
return style;
}
/// <summary>
/// 將目錄下的目錄檔案結構匯出成Excel工作表
/// </summary>
/// <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);
//建立Excel
HSSFWorkbook wb = new HSSFWorkbook();
var sheet = wb.CreateSheet("Site Tree");
var row = sheet.CreateRow(0);
var styleHeader = wb.QuickCreateStyle(
HSSFColor.YELLOW.index, HSSFColor.GREEN.index,
HorizontalAlignment.CENTER);
//寫入標題
int colIndex = 0;
foreach (string colName in "Path;File;Description".Split(';'))
{
var cell = row.CreateCell(colIndex++);
cell.SetCellType(CellType.STRING);
cell.SetCellValue(colName);
cell.CellStyle = styleHeader;
}
//建立Folder Style
var styleFolder = wb.QuickCreateStyle(HSSFColor.BLUE.index);
int rowIndex = 1;
foreach (var item in list)
{
//若bypass檢測傳回true,則略過該筆
if (filter != null && filter(item.Path))
continue;
row = sheet.CreateRow(rowIndex++);
//將Path放在第一欄(稍後隱藏)
var cell = row.CreateCell(0);
cell.SetCellValue(item.Path);
//存入檔名或目錄名
cell = row.CreateCell(1);
if (item.IsFolder)
cell.CellStyle = styleFolder;
cell.SetCellType(CellType.STRING);
cell.SetCellValue(new String(' ', item.Layer * 4) + item.Name);
}
//第一欄隱藏
sheet.SetColumnHidden(0, true);
//自動伸縮欄寬
sheet.AutoSizeColumn(1);
sheet.SetColumnWidth(1, sheet.GetColumnWidth(1) + 256);
sheet.SetColumnWidth(2, 100 * 256);
using (FileStream fs =
new FileStream(excelPath, FileMode.Create))
{
wb.Write(fs);
}
}
}
}