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<扣繳明細Item> data; public DateTime? 起日; public DateTime? 迄日; private List columns = new List() { 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=40 , FieldName="活動名稱事由" }, new ExportColumn() { Key="扣繳類別" , Title="扣繳類別" , Width=20 , FieldName="扣繳類別名稱" }, new ExportColumn() { Key="扣繳金額" , Title="扣繳金額" , Width=15 , FieldName="講師勞務費" , Align=HorizontalAlignment.Right, MoneyFormat=true }, new ExportColumn() { Key="應扣繳稅額" , Title="應扣繳稅額" , Width=15 , FieldName="應扣繳稅額" , Align=HorizontalAlignment.Right, MoneyFormat=true }, new ExportColumn() { Key="服務費" , Title="服務費" , Width=15 , FieldName="服務費" , Align=HorizontalAlignment.Right, MoneyFormat=true } }; public byte[] 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 => true).ToList(); // 報表抬頭 int rowId = 0; excel.MergeCell(rowId, 0, rowId, 6, BorderStyle.None); excel.SetCellValue(rowId, 0, PublicVariable.CompanyName, headerStyle); excel.SetRowHeightInPoints(rowId, 20); rowId++; excel.MergeCell(rowId, 0, rowId, 6, BorderStyle.None); if (起日 != null && 迄日 != null) excel.SetCellValue(rowId, 0, string.Format("[日期:{0:yyyy-MM-dd} ~ {1:yyyy-MM-dd}] 扣繳領據明細", 起日, 迄日), headerStyle); else if (起日 != null && 迄日 == null) excel.SetCellValue(rowId, 0, string.Format("[日期:{0:yyyy-MM-dd} ~ ] 扣繳領據明細", 起日), headerStyle); else if (起日 == null && 迄日 != null) excel.SetCellValue(rowId, 0, string.Format("[日期:~ {0:yyyy-MM-dd}] 扣繳領據明細", 迄日), headerStyle); else excel.SetCellValue(rowId, 0, "[日期:全部] 扣繳領據明細", headerStyle); excel.SetRowHeightInPoints(rowId, 20); rowId++; excel.MergeCell(rowId, 0, rowId, 6, BorderStyle.None); excel.SetCellValue(rowId, 0, string.Format("領款人姓名:{0} 身分證字號:{1}", data.FirstOrDefault()?.領款人姓名, data.FirstOrDefault()?.身分證字號), titleStyleLeftNoBorder); excel.SetRowHeightInPoints(rowId, 16); rowId++; // 表頭 int col = 0; foreach (var column in activeColumns) { excel.SetCellValue(rowId, col, column.Title, bodyStyleCenter); excel.SetColumnWidthInChars(col, column.Width); col++; } rowId++; // 報表內容 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.MergeCell(rowId, 0, rowId, 3, BorderStyle.Thin); excel.SetCellValue(rowId, 0, "總計", bodyStyleRight); excel.SetCellValue(rowId, 4, DAC.GetDouble(data.Sum(p => p.講師勞務費)), bodyNumberStyle2); excel.SetCellValue(rowId, 5, DAC.GetDouble(data.Sum(p => p.應扣繳稅額)), bodyNumberStyle2); excel.SetCellValue(rowId, 6, DAC.GetDouble(data.Sum(p => p.服務費)), bodyNumberStyle); MemoryStream ms = new MemoryStream(); workbook.Write(ms); return ms.ToArray(); } } }