using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Data; using NPOI.SS.UserModel; using NPOI.SS.Util; using WebLib; namespace Education.Report { public class 講師匯出 : ReportBase { public List<講師> data; public List export_columns; private List columns = new List() { new ExportColumn() { Key="姓名" , Title="姓名" , Width=20 , FieldName="姓名" }, new ExportColumn() { Key="出生日期" , Title="出生日期" , Width=20 , FieldName="出生日期" , Align=HorizontalAlignment.Center, Format="{0:yyyy-MM-dd}" }, new ExportColumn() { Key="性別" , Title="性別" , Width=10 , FieldName="性別" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="身份證字號" , Title="身份證字號" , Width=20 , FieldName="身份證字號" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="電話" , Title="電話" , Width=20 , FieldName="電話" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="手機" , Title="手機" , Width=20 , FieldName="手機" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="電子郵件" , Title="電子郵件" , Width=30 , FieldName="電子郵件" }, new ExportColumn() { Key="戶籍地址" , Title="戶籍地址" , Width=50 , FieldName="戶籍地址" }, new ExportColumn() { Key="居住地址" , Title="居住地址" , Width=50 , FieldName="居住地址" }, new ExportColumn() { Key="現職" , Title="現職" , Width=50 , FieldName="現職" }, new ExportColumn() { Key="最高學歷" , Title="最高學歷" , Width=100 , FieldName="學歷" }, new ExportColumn() { Key="授課區域" , Title="授課區域" , Width=100 , FieldName="授課區域TXT" }, new ExportColumn() { Key="授課專長領域" , Title="授課專長領域" , Width=100 , FieldName="授課專長領域" }, new ExportColumn() { Key="授課經歷概述" , Title="授課經歷概述" , Width=100 , FieldName="授課經歷概述" }, new ExportColumn() { Key="重要經歷" , Title="重要經歷" , Width=100 , FieldName="重要經歷" }, new ExportColumn() { Key="重要研究與著作" , Title="重要研究與著作" , Width=100 , FieldName="重要研究與著作" }, new ExportColumn() { Key="合作狀態" , Title="合作狀態" , Width=100 , FieldName="合作狀態RPT" }, new ExportColumn() { Key="合作方式" , Title="合作方式" , Width=100 , FieldName="合作方式" }, new ExportColumn() { Key="證照資料" , Title="證照資料" , Width=100 , FieldName="證照資料" }, new ExportColumn() { Key="異常標註" , Title="異常標註" , Width=100 , FieldName="異常標註" }, new ExportColumn() { Key="講師來源" , Title="講師來源" , Width=100 , FieldName="講師來源" }, new ExportColumn() { Key="推薦授課主題" , Title="推薦授課主題" , Width=100 , FieldName="推薦授課主題" }, new ExportColumn() { Key="TTQS滿意度" , Title="TTQS滿意度" , Width=20 , FieldName="平均滿意度" , Align=HorizontalAlignment.Right, Format="{0:F2}" }, new ExportColumn() { Key="講師評鑑(跟課)" , Title="講師評鑑(跟課)" , Width=20 , FieldName="講師評鑑" , Align=HorizontalAlignment.Right, Format="{0:F2}" }, new ExportColumn() { Key="講師評鑑(學員)" , Title="講師評鑑(學員)" , Width=20 , FieldName="學員滿意度" , Align=HorizontalAlignment.Right, Format="{0:F2}" }, }; public MemoryStream DoReport() { workbook.RemoveSheetAt(0); sheet = workbook.CreateSheet("講師資料匯出"); SetPaperAndMargin(sheet); sheet.RepeatingRows = new CellRangeAddress(0, 2, -1, -1); excel.Sheet = sheet; // 1. 預先過濾出需要匯出的欄位 var activeColumns = columns.Where(c => export_columns.Contains(c.Key)).ToList(); // 報表抬頭 excel.SetCellValue(0, 0, "序號", bodyStyleCenter); excel.SetColumnWidthInChars(0, 10); int col = 1; int colCount = 1; foreach (var column in activeColumns) { excel.SetCellValue(0, col, column.Title, bodyStyleCenter); excel.SetColumnWidthInChars(col, column.Width); col++; colCount++; } int rowId = 1; foreach (var r in data) { // 報表內容 col = 0; excel.SetCellValue(rowId, col++, rowId, bodyStyleCenter); foreach (var column in activeColumns) { var cellStyle = bodyStyleLeft; switch (column.Align) { case HorizontalAlignment.Center: cellStyle = bodyStyleCenter; break; case HorizontalAlignment.Right: cellStyle = bodyStyleRight; break; } var value = r.FieldValue(column.FieldName); SetDataValue(rowId, col, column, value, cellStyle); col++; } rowId++; } // 報表尾 excel.SetCellValue(rowId+1, 0, "總筆數", bodyStyleCenter); excel.SetCellValue(rowId+1, 1, data.Count, bodyStyleCenter); MemoryStream ms = new MemoryStream(); workbook.Write(ms); return ms; } } }