using System; using System.Collections.Generic; using System.Data; using NPOI.SS.UserModel; using System.IO; using Wellan.Data; using Wellan.Common; /// /// 支出憑證黏存單 /// public class 支出憑證黏存單 : ExcelReportBase { public 支出憑證黏存單() { 報表名稱 = "支出憑證黏存單"; } protected override void Init() { base.Init(); bodyStyleCenterXS.SetFont(bodyFont8PT); } public override MemoryStream 產生報表() { int 總金額 = 0; int 總天數 = 0; List 出差事由List = new List(); string 出差事由 = ""; #region 先加總所有金額以及產生摘要 foreach (DataRow r in 資料來源.Tables[0].Rows) { 總天數 += (WDAC.GetDateTimeValue(r["RPEDT"]) - WDAC.GetDateTimeValue(r["RPRDT"])).Days + 1; 出差事由List.Add(WDAC.GetStringValue(r["RPRSN"])); } for (int i = 0; i < 出差事由List.Count; i++) { 出差事由 += String.Format("{0}.{1}\n", i + 1, 出差事由List[i]); } if (出差事由.Length > 0) 出差事由 = 出差事由.Substring(0, 出差事由.Length - 1); foreach (DataRow r in 資料來源.Tables[1].Rows) { 總金額 += WDAC.GetInt32Value(r["總計"]); } #endregion #region 報表抬頭 excel.MergeCell(0, 0, 0, 16); excel.SetCellValue(0, 0, String.Format("{0}\n承辦{1}\n『{2}』\n{3}", 公司名稱, 客戶名稱, 專案名稱, 報表名稱), headerStyle); excel.MergeCell(1, 0, 1, 16); excel.SetCellValue(1, 0, String.Format("所屬年度:{0}年度", WGuard1.WNTSA(報表時間.Year - WGuard1.CY11, "", false)), titleStyleLeftNoBorder); excel.MergeCell(1, 0, 1, 16); excel.MergeCell(2, 0, 2, 4); excel.SetCellValue(2, 0, "傳票(付款憑單)編號:", titleStyleLeftNoBorder); excel.MergeCell(2, 15, 2, 16); excel.SetCellValue(2, 15, "黏貼單據___張", titleStyleRightNoBorder); excel.SetRangeOuterBorder(2, 0, 2, 16, BorderStyle.Thin); excel.MergeCell(3, 0, 5, 1); excel.SetCellValue(3, 0, "第 號", titleStyleCenterNoBorder); excel.SetRangeOuterBorder(3, 0, 5, 1, BorderStyle.Thin); excel.MergeCell(3, 2, 3, 7); excel.SetCellValue(3, 2, "金 額", titleStyleCenterNoBorder); excel.SetRangeOuterBorder(3, 2, 3, 7, BorderStyle.Thin); excel.SetCellValue(4, 2, "拾", titleStyleCenter); excel.SetCellValue(4, 3, "萬", titleStyleCenter); excel.SetCellValue(4, 4, "仟", titleStyleCenter); excel.SetCellValue(4, 5, "佰", titleStyleCenter); excel.SetCellValue(4, 6, "拾", titleStyleCenter); excel.SetCellValue(4, 7, "元", titleStyleCenter); string s總金額 = String.Format("{0:000000}", 總金額); excel.SetCellValue(5, 2, Convert.ToString(s總金額[0]), titleStyleCenter); excel.SetCellValue(5, 3, Convert.ToString(s總金額[1]), titleStyleCenter); excel.SetCellValue(5, 4, Convert.ToString(s總金額[2]), titleStyleCenter); excel.SetCellValue(5, 5, Convert.ToString(s總金額[3]), titleStyleCenter); excel.SetCellValue(5, 6, Convert.ToString(s總金額[4]), titleStyleCenter); excel.SetCellValue(5, 7, Convert.ToString(s總金額[5]), titleStyleCenter); excel.MergeCell(3, 8, 4, 10); excel.SetCellValue(3, 8, "用途別", titleStyleCenterNoBorder); excel.SetRangeOuterBorder(3, 8, 4, 10, BorderStyle.Thin); excel.MergeCell(5, 8, 5, 10); excel.SetCellValue(5, 8, "用途摘要", titleStyleCenterNoBorder); excel.SetRangeOuterBorder(5, 8, 5, 10, BorderStyle.Thin); excel.MergeCell(3, 11, 4, 16); excel.SetCellValue(3, 11, "國內旅費", titleStyleLeftNoBorder); excel.SetRangeOuterBorder(3, 11, 4, 16, BorderStyle.Thin); excel.MergeCell(5, 11, 5, 16); excel.SetCellValue(5, 11, String.Format("{0:MM}月出差", 報表時間), titleStyleLeftNoBorder); excel.SetRangeOuterBorder(5, 11, 5, 16, BorderStyle.Thin); excel.MergeCell(6, 8, 6, 12); excel.SetCellValue(6, 8, "國內出差旅費報告單", headerStyle); excel.MergeCell(6, 13, 6, 16); excel.SetCellValue(6, 13, String.Format("{0:填表日期:yyyy年MM月dd日}", DateTime.UtcNow.AddHours(8).Date), titleStyleRightNoBorder); excel.SetRangeOuterBorder(6, 0, 6, 16, BorderStyle.Thin); excel.MergeCell(7, 0, 7, 2); excel.MergeCell(7, 3, 7, 6); excel.MergeCell(7, 7, 7, 8); excel.MergeCell(7, 9, 7, 11); excel.MergeCell(7, 12, 7, 13); excel.MergeCell(7, 14, 7, 16); excel.SetCellValue(7, 0, "姓 名", titleStyleCenterNoBorder); excel.SetCellValue(7, 3, WDAC.GetStringValue(資料來源.Tables[0].Rows[0]["BPNME"]), titleStyleCenterNoBorder); excel.SetCellValue(7, 7, "單 位", titleStyleCenterNoBorder); excel.SetCellValue(7, 9, WDAC.GetStringValue(資料來源.Tables[0].Rows[0]["站別"]), titleStyleCenterNoBorder); excel.SetCellValue(7, 12, "職 稱", titleStyleCenterNoBorder); excel.SetCellValue(7, 14, WDAC.GetStringValue(資料來源.Tables[0].Rows[0]["職稱"]), titleStyleCenterNoBorder); excel.SetRangeOuterBorder(7, 0, 7, 2, BorderStyle.Thin); excel.SetRangeOuterBorder(7, 3, 7, 6, BorderStyle.Thin); excel.SetRangeOuterBorder(7, 7, 7, 8, BorderStyle.Thin); excel.SetRangeOuterBorder(7, 9, 7, 11, BorderStyle.Thin); excel.SetRangeOuterBorder(7, 12, 7, 13, BorderStyle.Thin); excel.SetRangeOuterBorder(7, 14, 7, 16, BorderStyle.Thin); excel.MergeCell(8, 0, 8, 2); excel.MergeCell(8, 3, 8, 16); excel.SetCellValue(8, 0, "出差事由", titleStyleCenterNoBorder); excel.SetCellValue(8, 3, 出差事由, titleStyleLeftNoBorder); excel.SetRangeOuterBorder(8, 0, 8, 2, BorderStyle.Thin); excel.SetRangeOuterBorder(8, 3, 8, 16, BorderStyle.Thin); excel.MergeCell(9, 0, 9, 16); excel.SetCellValue(9, 0, String.Format("中華民國 {0:yyyy} 年 {0:MM} 月 {0:dd} 日 至 {1:yyyy} 年 {1:MM} 月 {1:dd} 日止 共計 {2} 天 附單據___張", 報表時間, 報表時間.AddMonths(1).AddDays(-1), 總天數), titleStyleCenterNoBorder); excel.SetRangeOuterBorder(9, 0, 9, 16, BorderStyle.Thin); excel.MergeCell(10, 0, 10, 1); excel.MergeCell(10, 2, 11, 4); excel.MergeCell(10, 5, 11, 7); excel.MergeCell(10, 8, 10, 11); excel.MergeCell(10, 12, 11, 12); excel.MergeCell(10, 13, 11, 13); excel.MergeCell(10, 14, 11, 14); excel.MergeCell(10, 15, 11, 15); excel.MergeCell(10, 16, 11, 16); excel.SetCellValue(10, 0, String.Format("{0:yyyy}年", 報表時間), titleStyleCenterNoBorder); excel.SetCellValue(11, 0, "月", titleStyleCenter); excel.SetCellValue(11, 1, "日", titleStyleCenter); excel.SetCellValue(10, 2, "起訖地點", titleStyleCenterNoBorder); excel.SetCellValue(10, 5, "工作摘要", titleStyleCenterNoBorder); excel.SetCellValue(10, 8, "交通費", titleStyleCenterNoBorder); excel.SetCellValue(11, 8, "捷運\n及\n汽車", titleStyleCenter); excel.SetCellValue(11, 9, "火\n車", titleStyleCenter); excel.SetCellValue(11, 10, "高\n鐵", titleStyleCenter); excel.SetCellValue(11, 11, "飛\n機", titleStyleCenter); excel.SetCellValue(10, 12, "住宿費\n+\n過路費", titleStyleCenterNoBorder); excel.SetCellValue(10, 13, "雜費", titleStyleCenterNoBorder); excel.SetCellValue(10, 14, "單據\n號數", titleStyleCenterNoBorder); excel.SetCellValue(10, 15, "總計", titleStyleCenterNoBorder); excel.SetCellValue(10, 16, "備註", titleStyleCenterNoBorder); excel.SetRangeOuterBorder(10, 0, 10, 1, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 2, 11, 4, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 5, 11, 7, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 8, 10, 11, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 12, 11, 12, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 13, 11, 13, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 14, 11, 14, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 15, 11, 15, BorderStyle.Thin); excel.SetRangeOuterBorder(10, 16, 11, 16, BorderStyle.Thin); excel.SetRowHeight(0, HeaderRowHeight * 4); excel.SetRowHeight(1, BodyRowHeight); excel.SetRowHeight(2, BodyRowHeight); excel.SetRowHeight(3, BodyRowHeight); excel.SetRowHeight(4, BodyRowHeight); excel.SetRowHeight(5, BodyRowHeight * 2); excel.SetRowHeight(6, BodyRowHeight * 2); excel.SetRowHeight(7, BodyRowHeight); excel.SetRowHeight(8, BodyRowHeight * (資料來源.Tables[0].Rows.Count > 4 ? 資料來源.Tables[0].Rows.Count : 4)); excel.SetRowHeight(9, BodyRowHeight); excel.SetRowHeight(10, BodyRowHeight); excel.SetRowHeight(11, BodyRowHeight * 2); #endregion #region 報表資料 int rowId = 12; int beginRow = 12; foreach (DataRow r in 資料來源.Tables[1].Rows) { DataRow[] master = 資料來源.Tables[0].Select("RPREN='" + WDAC.GetStringValue(r["單號"]) + "'"); excel.SetCellValue(rowId, 0, String.Format("{0:MM}", r["日期"]), bodyStyleCenter); excel.SetCellValue(rowId, 1, String.Format("{0:dd}", r["日期"]), bodyStyleCenter); excel.MergeCell(rowId, 2, rowId, 4); excel.SetCellValue(rowId, 2, WDAC.GetStringValue(r["起地點"]) + "~" + WDAC.GetStringValue(r["迄地點"]), bodyStyleCenterS); excel.SetRangeOuterBorder(rowId, 2, rowId, 4, BorderStyle.Thin); excel.MergeCell(rowId, 5, rowId, 7); 出差事由 = WDAC.GetStringValue(master[0]["RPRSN"]); if (master.Length > 0 && 出差事由List.Contains(出差事由)) excel.SetCellValue(rowId, 5, String.Format("同出差事由 {0}", 出差事由List.IndexOf(出差事由) + 1), bodyStyleCenterS); else excel.SetCellValue(rowId, 5, "", bodyStyleCenter); excel.SetRangeOuterBorder(rowId, 5, rowId, 7, BorderStyle.Thin); excel.SetCellValue(rowId, 8, WDAC.GetDoubleValue(r["捷運"]) + WDAC.GetDoubleValue(r["汽車"]), bodyStyleCenter); excel.SetCellValue(rowId, 9, WDAC.GetDoubleValue(r["火車"]), bodyStyleCenter); excel.SetCellValue(rowId, 10, WDAC.GetDoubleValue(r["高鐵"]), bodyStyleCenter); excel.SetCellValue(rowId, 11, WDAC.GetDoubleValue(r["飛機"]), bodyStyleCenter); excel.SetCellValue(rowId, 12, WDAC.GetDoubleValue(r["住宿費"]) + WDAC.GetDoubleValue(r["過路費"]), bodyStyleCenter); excel.SetCellValue(rowId, 13, WDAC.GetDoubleValue(r["雜費"]), bodyStyleCenter); excel.SetCellValue(rowId, 14, WDAC.GetStringValue(r["單據號數"]), bodyStyleCenter); excel.SetCellValue(rowId, 15, WDAC.GetDoubleValue(r["總計"]), bodyStyleCenter); excel.SetCellValue(rowId, 16, WDAC.GetStringValue(r["備註"]), bodyStyleCenterXS); excel.SetRowHeight(rowId, (int)(BodyRowHeight * 1.5)); rowId++; } // 補空行 if (資料來源.Tables[1].Rows.Count < 10) { for (int n = 資料來源.Tables[1].Rows.Count; n < 10; n++) { excel.SetCellValue(rowId, 0, "", bodyStyleCenter); excel.SetCellValue(rowId, 1, "", bodyStyleCenter); excel.MergeCell(rowId, 2, rowId, 4); excel.SetCellValue(rowId, 2, "", bodyStyleCenter); excel.SetRangeOuterBorder(rowId, 2, rowId, 4, BorderStyle.Thin); excel.MergeCell(rowId, 5, rowId, 7); excel.SetCellValue(rowId, 5, "", bodyStyleCenter); excel.SetRangeOuterBorder(rowId, 5, rowId, 7, BorderStyle.Thin); excel.SetCellValue(rowId, 8, "", bodyStyleCenter); excel.SetCellValue(rowId, 9, "", bodyStyleCenter); excel.SetCellValue(rowId, 10, "", bodyStyleCenter); excel.SetCellValue(rowId, 11, "", bodyStyleCenter); excel.SetCellValue(rowId, 12, "", bodyStyleCenter); excel.SetCellValue(rowId, 13, "", bodyStyleCenter); excel.SetCellValue(rowId, 14, "", bodyStyleCenter); excel.SetCellValue(rowId, 15, "", bodyStyleCenter); excel.SetCellValue(rowId, 16, "", bodyStyleCenter); excel.SetRowHeight(rowId, (int)(BodyRowHeight * 0.8)); rowId++; } } #endregion #region 合計及表尾批示 excel.MergeCell(rowId, 0, rowId, 4); excel.SetCellValue(rowId, 0, "合 計", titleStyleCenter); excel.SetRangeOuterBorder(rowId, 0, rowId, 4, BorderStyle.Thin); excel.MergeCell(rowId, 5, rowId, 7); excel.SetRangeOuterBorder(rowId, 5, rowId, 7, BorderStyle.Thin); excel.SetCellFormula(rowId, 8, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 8), excel.RCtoA1(rowId - 1, 8)), bodyStyleCenter); excel.SetCellFormula(rowId, 9, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 9), excel.RCtoA1(rowId - 1, 9)), bodyStyleCenter); excel.SetCellFormula(rowId, 10, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 10), excel.RCtoA1(rowId - 1, 10)), bodyStyleCenter); excel.SetCellFormula(rowId, 11, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 11), excel.RCtoA1(rowId - 1, 11)), bodyStyleCenter); excel.SetCellFormula(rowId, 12, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 12), excel.RCtoA1(rowId - 1, 12)), bodyStyleCenter); excel.SetCellFormula(rowId, 13, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 13), excel.RCtoA1(rowId - 1, 13)), bodyStyleCenter); excel.SetCellValue(rowId, 14, "", titleStyleCenter); excel.SetCellFormula(rowId, 15, String.Format("SUM({0}:{1})", excel.RCtoA1(beginRow, 15), excel.RCtoA1(rowId - 1, 15)), bodyStyleCenter); excel.SetCellValue(rowId, 16, "", titleStyleCenter); excel.SetRowHeight(rowId, BodyRowHeight); rowId++; excel.MergeCell(rowId, 0, rowId, 4); excel.MergeCell(rowId, 5, rowId, 9); excel.MergeCell(rowId, 10, rowId, 13); excel.MergeCell(rowId, 14, rowId, 16); if (專案編號.Length >= 6 && 專案編號.Substring(專案編號.Length - 6, 6) == "B00001") { excel.SetCellValue(rowId, 0, "申 請 人", titleStyleCenter); excel.SetCellValue(rowId, 5, "單 位 主 管", titleStyleCenter); excel.SetCellValue(rowId, 10, "財 會 經 辦", titleStyleCenter); excel.SetCellValue(rowId, 14, "人 資 單 位", titleStyleCenter); } else { excel.SetCellValue(rowId, 0, "申 請 人", titleStyleCenter); excel.SetCellValue(rowId, 5, "計 畫 督 導", titleStyleCenter); excel.SetCellValue(rowId, 10, "人 事 單 位", titleStyleCenter); excel.SetCellValue(rowId, 14, "財 務 主 管", titleStyleCenter); } excel.SetRangeOuterBorder(rowId, 0, rowId, 4, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 5, rowId, 9, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 10, rowId, 13, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 14, rowId, 16, BorderStyle.Thin); excel.SetRowHeight(rowId, BodyRowHeight); rowId++; excel.MergeCell(rowId, 0, rowId, 4); excel.SetCellValue(rowId, 0, WDAC.GetStringValue(資料來源.Tables[0].Rows[0]["BPNME"]), bodyStyleCenter); excel.MergeCell(rowId, 5, rowId, 9); excel.MergeCell(rowId, 10, rowId, 13); excel.MergeCell(rowId, 14, rowId, 16); excel.SetRangeOuterBorder(rowId, 0, rowId, 4, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 5, rowId, 9, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 10, rowId, 13, BorderStyle.Thin); excel.SetRangeOuterBorder(rowId, 14, rowId, 16, BorderStyle.Thin); excel.SetRowHeight(rowId, BodyRowHeight * 3); #endregion excel.SetColumnWidth(0, 4); excel.SetColumnWidth(1, 4); excel.SetColumnWidth(2, 4); excel.SetColumnWidth(3, 4); excel.SetColumnWidth(4, 4); excel.SetColumnWidth(5, 4); excel.SetColumnWidth(6, 4); excel.SetColumnWidth(7, 4); excel.SetColumnWidth(8, 7); excel.SetColumnWidth(9, 7); excel.SetColumnWidth(10, 7); excel.SetColumnWidth(11, 7); excel.SetColumnWidth(12, 7); excel.SetColumnWidth(13, 7); excel.SetColumnWidth(14, 7); excel.SetColumnWidth(15, 9); excel.SetColumnWidth(16, 20); MemoryStream ms = new MemoryStream(); workbook.Write(ms); return ms; } }