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 List export_columns; public DateTime? 起日; public DateTime? 迄日; public string 身分別; public string 憑證類別; 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, Format="{0:yyyy/MM/dd}" }, 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=20 , FieldName="領據編號" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="活動名稱/事由" , Title="活動名稱/事由" , Width=50 , FieldName="活動名稱事由" }, new ExportColumn() { Key="費用別" , Title="費用別" , Width=20 , FieldName="費用別" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="單價" , Title="單價" , Width=20 , FieldName="單價" , Align=HorizontalAlignment.Right, MoneyFormat=true }, new ExportColumn() { Key="數量" , Title="數量" , Width=20 , FieldName="數量" , Align=HorizontalAlignment.Right, MoneyFormat=true, MondyDigits=2 }, new ExportColumn() { Key="單位" , Title="單位" , Width=20 , FieldName="單位" , Align=HorizontalAlignment.Center }, new ExportColumn() { Key="扣繳類別" , Title="扣繳類別" , Width=25 , FieldName="扣繳類別名稱" , Align=HorizontalAlignment.Center }, 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 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 => true).ToList(); // 報表抬頭 int rowId = 0; excel.MergeCell(rowId, 0, rowId, 1, BorderStyle.Thin); excel.SetCellValue(rowId, 0, "活動日期:", bodyStyleLeft); excel.MergeCell(rowId, 2, rowId, 3, BorderStyle.Thin); if (起日 != null && 迄日 != null) excel.SetCellValue(rowId, 2, string.Format("{0:yyyy-MM-dd} ~ {1:yyyy-MM-dd}", 起日, 迄日), bodyStyleLeft); else if (起日 != null && 迄日 == null) excel.SetCellValue(rowId, 2, string.Format("{0:yyyy-MM-dd} ~ ", 起日), bodyStyleLeft); else if (起日 == null && 迄日 != null) excel.SetCellValue(rowId, 2, string.Format("~ {0:yyyy-MM-dd}", 迄日), bodyStyleLeft); else excel.SetCellValue(rowId, 2, "全部", bodyStyleLeft); rowId++; excel.MergeCell(rowId, 0, rowId, 1, BorderStyle.Thin); excel.SetCellValue(rowId, 0, "身分別:", bodyStyleLeft); excel.MergeCell(rowId, 2, rowId, 3, BorderStyle.Thin); if (string.IsNullOrEmpty(身分別)) excel.SetCellValue(rowId, 2, "全部", bodyStyleLeft); else excel.SetCellValue(rowId, 2, 身分別, bodyStyleLeft); rowId++; excel.MergeCell(rowId, 0, rowId, 1, BorderStyle.Thin); excel.SetCellValue(rowId, 0, "憑證類別:", bodyStyleLeft); excel.MergeCell(rowId, 2, rowId, 3, BorderStyle.Thin); if (string.IsNullOrEmpty(憑證類別)) excel.SetCellValue(rowId, 2, "全部", bodyStyleLeft); else excel.SetCellValue(rowId, 2, 憑證類別, bodyStyleLeft); rowId++; // 表頭 excel.SetCellValue(rowId, 0, "項次", bodyStyleCenter); excel.SetColumnWidthInChars(0, 8); int col = 1; int colCount = 1; foreach (var column in activeColumns) { excel.SetCellValue(3, col, column.Title, bodyStyleCenter); excel.SetColumnWidthInChars(col, column.Width); col++; colCount++; } rowId++; // 報表內容 foreach (var r in data) { // 序號 col = 0; excel.SetCellValue(rowId, col++, rowId - 3, 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.MergeCell(rowId, 0, rowId, 4); //excel.SetCellValue(rowId, 0, "合計", bodyStyleLeft); //excel.SetCellValue(rowId, 5, string.Format("{0:###,###,##0}", data.Sum(p => p.講師勞務費合計)), bodyStyleRight); //excel.SetCellValue(rowId, 6, string.Format("{0:###,###,##0}", data.Sum(p => p.應扣繳稅額合計)), bodyStyleRight); //excel.SetCellValue(rowId, 7, string.Format("{0:###,###,##0}", data.Sum(p => p.服務費合計)), bodyStyleRight); MemoryStream ms = new MemoryStream(); workbook.Write(ms); return ms; } } }