using System; using System.Collections.Generic; using System.IO; using System.Data; using NPOI.SS.UserModel; using NPOI.SS.Util; using WebLib; namespace Education.Report { public class 學員簽到表 : ReportBase { private string 計畫名稱; public string 公司名稱; public string 訓練日期; public string 辦理地點; public string 訓練企劃名稱; public string 講師姓名; public string 承辦人員; public DataTable data; public List 訓練日期List; //public MemoryStream DoReport() //{ // int 序號 = 1; // int 工作表序號 = 1; // workbook.RemoveSheetAt(0); // bodyStyleCenter.VerticalAlignment = VerticalAlignment.Justify; // foreach (DataRow r in data.Rows) // { // string data計畫名稱 = DAC.GetString(r["計畫年度名稱"]); // string data站別名稱 = DAC.GetString(r["站別名稱"]); // string data職務名稱 = DAC.GetString(r["職務名稱"]); // string data學員姓名 = DAC.GetString(r["學員姓名"]); // int 簽到格數 = 訓練日期List.Count * 2 - 1; // if (簽到格數 < 0) // 簽到格數 = 0; // // 是否要換頁 // if (計畫名稱 != data計畫名稱) // { // 計畫名稱 = data計畫名稱; // sheet = workbook.CreateSheet("Sheet" + 工作表序號.ToString()) as HSSFSheet; // SetPaperAndMargin(sheet); // sheet.RepeatingRows = new CellRangeAddress(0, 3, -1, -1); // // workbook.SetRepeatingRowsAndColumns(workbook.GetSheetIndex(sheet), -1, -1, 0, 3); // excel.Sheet = sheet; // 工作表序號++; // // 報表抬頭 // excel.MergeCell(0, 0, 0, 7 + 簽到格數); // excel.SetCellValue(0, 0, String.Format("{0}\n{1}\n教育訓練簽到表", 公司名稱, 計畫名稱), titleStyleCenter); // excel.SetRowHeightInPoints(0, HeaderRowHeight * 3); // excel.MergeCell(1, 0, 1, 7 + 簽到格數); // excel.SetCellValue(1, 0, String.Format("辦理時間:{0}\n辦理地點:{1}\n訓練名稱:{2}\n講師姓名:{3}\n承辦人員:{4}", // 訓練日期, 辦理地點, 訓練企劃名稱, 講師姓名, 承辦人員), // bodyStyleLeft); // excel.SetRowHeightInPoints(1, BodyRowHeight * 5); // excel.SetRangeOuterBorder(1, 0, 1, 7 + 簽到格數, BorderStyle.Thin); // excel.MergeCell(2, 0, 4, 0); // excel.SetCellValue(2, 0, "序號", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 0, 4, 0, BorderStyle.Thin); // excel.MergeCell(2, 1, 4, 1); // excel.SetCellValue(2, 1, "單位", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 1, 4, 1, BorderStyle.Thin); // excel.MergeCell(2, 2, 4, 2); // excel.SetCellValue(2, 2, "職務名稱", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 2, 4, 2, BorderStyle.Thin); // excel.MergeCell(2, 3, 4, 3); // excel.SetCellValue(2, 3, "姓名", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 3, 4, 3, BorderStyle.Thin); // excel.MergeCell(2, 4, 2, 4 + 簽到格數); // excel.SetCellValue(2, 4, "簽到", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 4, 2, 4 + 簽到格數, BorderStyle.Thin); // for (int i = 0; i < 訓練日期List.Count; i++) // { // excel.MergeCell(3, 4 + i * 2, 3, 5 + i * 2); // excel.SetCellValue(3, 4 + i * 2, 訓練日期List[i].ToString("yyyy-MM-dd"), bodyStyleCenter); // excel.SetRangeOuterBorder(3, 4 + i * 2, 3, 5 + i * 2, BorderStyle.Thin); // excel.SetCellValue(4, 4 + i * 2, "上午", bodyStyleCenter); // excel.SetCellValue(4, 5 + i * 2, "下午", bodyStyleCenter); // } // excel.MergeCell(2, 5 + 簽到格數, 3, 6 + 簽到格數); // excel.SetCellValue(2, 5 + 簽到格數, "用餐別", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 5 + 簽到格數, 3, 6 + 簽到格數, BorderStyle.Thin); // excel.SetCellValue(4, 5 + 簽到格數, "葷", bodyStyleCenter); // excel.SetCellValue(4, 6 + 簽到格數, "素", bodyStyleCenter); // excel.MergeCell(2, 7 + 簽到格數, 4, 7 + 簽到格數); // excel.SetCellValue(2, 7 + 簽到格數, "備註", bodyStyleCenter); // excel.SetRangeOuterBorder(2, 7 + 簽到格數, 4, 7 + 簽到格數, BorderStyle.Thin); // // 設定各欄寬度 // excel.SetColumnWidthInChars(0, 8); // excel.SetColumnWidthInChars(1, 26); // excel.SetColumnWidthInChars(2, 16); // excel.SetColumnWidthInChars(3, 16); // for (int i = 0; i < 訓練日期List.Count; i++) // { // excel.SetColumnWidthInChars(4 + i * 2, 8); // excel.SetColumnWidthInChars(5 + i * 2, 8); // } // excel.SetColumnWidthInChars(5 + 簽到格數, 8); // excel.SetColumnWidthInChars(6 + 簽到格數, 8); // excel.SetColumnWidthInChars(7 + 簽到格數, 16); // excel.SetRowHeightInPoints(2, BodyRowHeight); // excel.SetRowHeightInPoints(3, BodyRowHeight); // excel.SetRowHeightInPoints(4, BodyRowHeight); // 序號 = 1; // } // // 報表內容 // excel.SetCellValue(序號 + 4, 0, 序號, bodyStyleCenter); // excel.SetCellValue(序號 + 4, 1, data站別名稱, bodyStyleCenter); // excel.SetCellValue(序號 + 4, 2, data職務名稱, bodyStyleCenter); // excel.SetCellValue(序號 + 4, 3, data學員姓名, bodyStyleCenter); // for (int i = 0; i < 訓練日期List.Count; i++) // { // excel.SetCellValue(序號 + 4, 4 + i * 2, "", bodyStyleCenter); // excel.SetCellValue(序號 + 4, 5 + i * 2, "", bodyStyleCenter); // } // excel.SetCellValue(序號 + 4, 5 + 簽到格數, "", bodyStyleCenter); // excel.SetCellValue(序號 + 4, 6 + 簽到格數, "", bodyStyleCenter); // excel.SetCellValue(序號 + 4, 7 + 簽到格數, "", bodyStyleCenter); // excel.SetRowHeightInPoints(序號 + 4, (int)Math.Truncate(BodyRowHeight * 1.5)); // 序號++; // } // MemoryStream ms = new MemoryStream(); // workbook.Write(ms); // return ms; //} public MemoryStream DoReport() { int 序號 = 1; int 工作表序號 = 1; workbook.RemoveSheetAt(0); bodyStyleCenter.VerticalAlignment = VerticalAlignment.Justify; for (int i = 0; i < 訓練日期List.Count; i++) { 計畫名稱 = null; foreach (DataRow r in data.Rows) { string data計畫名稱 = DAC.GetString(r["計畫年度名稱"]); string data站別名稱 = DAC.GetString(r["站別名稱"]); string data職務名稱 = DAC.GetString(r["職務名稱"]); string data學員姓名 = DAC.GetString(r["學員姓名"]); // 是否要換頁 if (計畫名稱 != data計畫名稱) { 計畫名稱 = data計畫名稱; sheet = workbook.CreateSheet("Sheet" + 工作表序號.ToString()); SetPaperAndMargin(sheet); sheet.RepeatingRows = new CellRangeAddress(0, 3, -1, -1); // workbook.SetRepeatingRowsAndColumns(workbook.GetSheetIndex(sheet), -1, -1, 0, 3); excel.Sheet = sheet; 工作表序號++; // 報表抬頭 excel.MergeCell(0, 0, 0, 8); excel.SetCellValue(0, 0, String.Format("{0}\n{1}\n教育訓練簽到表", 公司名稱, 計畫名稱), titleStyleCenter); excel.SetRowHeightInPoints(0, HeaderRowHeight * 3); excel.MergeCell(1, 0, 1, 8); excel.SetCellValue(1, 0, String.Format("辦理時間:{0:yyyy年MM月dd日(dddd)}\n辦理地點:{1}\n訓練名稱:{2}\n講師姓名:{3}\n承辦人員:{4}", 訓練日期List[i], 辦理地點, 訓練企劃名稱, 講師姓名, 承辦人員), bodyStyleLeft); excel.SetRowHeightInPoints(1, BodyRowHeight * 5); excel.SetRangeOuterBorder(1, 0, 1, 8, BorderStyle.Thin); excel.MergeCell(2, 0, 3, 0); excel.SetCellValue(2, 0, "序號", bodyStyleCenter); excel.SetRangeOuterBorder(2, 0, 3, 0, BorderStyle.Thin); excel.MergeCell(2, 1, 3, 1); excel.SetCellValue(2, 1, "單位", bodyStyleCenter); excel.SetRangeOuterBorder(2, 1, 3, 1, BorderStyle.Thin); excel.MergeCell(2, 2, 3, 2); excel.SetCellValue(2, 2, "職務名稱", bodyStyleCenter); excel.SetRangeOuterBorder(2, 2, 3, 2, BorderStyle.Thin); excel.MergeCell(2, 3, 3, 3); excel.SetCellValue(2, 3, "姓名", bodyStyleCenter); excel.SetRangeOuterBorder(2, 3, 3, 3, BorderStyle.Thin); excel.MergeCell(2, 4, 2, 5); excel.SetCellValue(2, 4, 訓練日期List[i].ToString("yyyy-MM-dd"), bodyStyleCenter); excel.SetRangeOuterBorder(2, 4, 2, 5, BorderStyle.Thin); excel.SetCellValue(3, 4, "簽到", bodyStyleCenter); excel.SetCellValue(3, 5, "簽退", bodyStyleCenter); excel.MergeCell(2, 6, 2, 7); excel.SetCellValue(2, 6, "用餐別", bodyStyleCenter); excel.SetRangeOuterBorder(2, 6, 2, 7, BorderStyle.Thin); excel.SetCellValue(3, 6, "葷", bodyStyleCenter); excel.SetCellValue(3, 7, "素", bodyStyleCenter); excel.MergeCell(2, 8, 3, 8); excel.SetCellValue(2, 8, "備註", bodyStyleCenter); excel.SetRangeOuterBorder(2, 8, 3, 8, BorderStyle.Thin); // 設定各欄寬度 excel.SetColumnWidthInChars(0, 8); excel.SetColumnWidthInChars(1, 26); excel.SetColumnWidthInChars(2, 16); excel.SetColumnWidthInChars(3, 16); excel.SetColumnWidthInChars(4, 16); excel.SetColumnWidthInChars(5, 16); excel.SetColumnWidthInChars(6, 8); excel.SetColumnWidthInChars(7, 8); excel.SetColumnWidthInChars(8, 16); excel.SetRowHeightInPoints(2, BodyRowHeight); excel.SetRowHeightInPoints(3, BodyRowHeight); 序號 = 1; } // 報表內容 excel.SetCellValue(序號 + 3, 0, 序號, bodyStyleCenter); excel.SetCellValue(序號 + 3, 1, data站別名稱, bodyStyleCenter); excel.SetCellValue(序號 + 3, 2, data職務名稱, bodyStyleCenter); excel.SetCellValue(序號 + 3, 3, data學員姓名, bodyStyleCenter); excel.SetCellValue(序號 + 3, 4, "", bodyStyleCenter); excel.SetCellValue(序號 + 3, 5, "", bodyStyleCenter); excel.SetCellValue(序號 + 3, 6, "", bodyStyleCenter); excel.SetCellValue(序號 + 3, 7, "", bodyStyleCenter); excel.SetCellValue(序號 + 3, 8, "", bodyStyleCenter); excel.SetRowHeightInPoints(序號 + 3, (int)Math.Truncate(BodyRowHeight * 1.5)); 序號++; } } MemoryStream ms = new MemoryStream(); workbook.Write(ms); return ms; } } }