using NPOI.SS.UserModel; using NPOI.SS.Util; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using WebLib; namespace Education.Report { public class 非講師匯出 : ReportBase { public List<非講師Item> data; public List export_columns; private List columns = new List() { new ExportColumn() { Key="編號" , Title="編號" , Width=10 , FieldName="編號" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="姓名" , Title="姓名" , Width=10 , FieldName="姓名" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="身分證字號" , Title="身分證字號" , Width=16 , FieldName="身分證字號" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="藝名網名" , Title="藝名網名" , Width=10 , FieldName="藝名網名" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="聯絡電話" , Title="聯絡電話" , Width=16 , FieldName="聯絡電話" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="電子郵件" , Title="電子郵件" , Width=25 , FieldName="電子郵件" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="戶籍地址" , Title="戶籍地址_郵編" , Width=10 , FieldName="戶籍地址_郵編" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="戶籍地址" , Title="戶籍地址_縣市" , Width=10 , FieldName="戶籍地址_縣市" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="戶籍地址" , Title="戶籍地址_鄉鎮市區" , Width=10 , FieldName="戶籍地址_鄉鎮市區" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="戶籍地址" , Title="戶籍地址_街道地址" , Width=30 , FieldName="戶籍地址_街道地址" }, new ExportColumn() { Key="居住地址" , Title="居住地址_郵編" , Width=10 , FieldName="居住地址_郵編" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="居住地址" , Title="居住地址_縣市" , Width=10 , FieldName="居住地址_縣市" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="居住地址" , Title="居住地址_鄉鎮市區" , Width=10 , FieldName="居住地址_鄉鎮市區" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="居住地址" , Title="居住地址_街道地址" , Width=30 , FieldName="居住地址_街道地址" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="職務內容" , Title="職務內容" , Width=100 , FieldName="職務內容" }, new ExportColumn() { Key="合作滿意度" , Title="合作滿意度" , Width=100 , FieldName="合作滿意度" }, new ExportColumn() { Key="窗口" , Title="窗口", Width=100 }, new ExportColumn() { Key="是否接受邀約" , Title="是否接受邀約", Width=10, FieldName= "接受邀約", Format = "{0}", Align=HorizontalAlignment.Center }, new ExportColumn() { Key="聯繫狀況與報價" , Title="聯繫狀況與報價", Width=100 }, new ExportColumn() { Key="備註" , Title="備註", Width=100 } }; 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(); // 報表抬頭 int col = 0; int colCount = 0; 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; 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; } } }