From aaddec10d425162585c775725f5b1ce62a299b49 Mon Sep 17 00:00:00 2001 From: dk96 Date: Wed, 18 Dec 2024 11:40:43 +0800 Subject: [PATCH] final beta 1 --- BackEnd/api/exportBabyRec.ashx | 1 + BackEnd/api/exportBabyRec.ashx.cs | 165 +++++++++++++++++++ BackEnd/api/exportExcel.ashx.cs | 1 + BackEnd/assets/javascript/custom/userList.js | 12 ++ BackEnd/userList.aspx | 2 +- abbott_2024_event.csproj | 4 + bin/abbott_2024_event.dll | Bin 67584 -> 69632 bytes 7 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 BackEnd/api/exportBabyRec.ashx create mode 100644 BackEnd/api/exportBabyRec.ashx.cs diff --git a/BackEnd/api/exportBabyRec.ashx b/BackEnd/api/exportBabyRec.ashx new file mode 100644 index 0000000..5a621c7 --- /dev/null +++ b/BackEnd/api/exportBabyRec.ashx @@ -0,0 +1 @@ +<%@ WebHandler Language="C#" CodeBehind="exportBabyRec.ashx.cs" Class="abbott_2024_event.BackEnd.api.exportBabyRec" %> diff --git a/BackEnd/api/exportBabyRec.ashx.cs b/BackEnd/api/exportBabyRec.ashx.cs new file mode 100644 index 0000000..a0757dc --- /dev/null +++ b/BackEnd/api/exportBabyRec.ashx.cs @@ -0,0 +1,165 @@ +using System; +using System.Web; +using NPOI; +//using NPOI.SS.UserModel; +using NPOI.HPSF; +using NPOI.HSSF; +using NPOI.HSSF.UserModel; +using NPOI.POIFS; +using NPOI.Util; +using System.IO; +using System.Runtime.Serialization.Json; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Web.SessionState; +using System.Collections.Generic; +using System.Data; +using System.Text; +using System.Data.SqlClient; +using System.Runtime.Remoting; +using Dapper; +using System.Linq; +using NPOI.SS.Formula.Functions; +using NPOI.XSSF.UserModel; +using System.Web.UI.WebControls; +namespace abbott_2024_event.BackEnd.api +{ + /// + /// exportBabyRec 的摘要描述 + /// + public class exportBabyRec : IHttpHandler + { + SqlConnection conn = new SqlConnection(globalClass.appsettings("DBConnectionString")); + public authToken authToken; + public void ProcessRequest(HttpContext context) + { + authToken objAuth = new authToken(); + + if (!objAuth.user_isLogin) + { + context.Response.Write("尚未登入,請登入後使用"); + return; + } + + string line_uid = (context.Request["line_uid"] == null) ? "" : context.Request["line_uid"].ToString(); + string babyData_uid = (context.Request["baby_uid"] == null) ? "" : context.Request["baby_uid"].ToString(); + + lineUser lineUser = conn.QueryFirstOrDefault("select * from lineUser where line_uid = @line_uid", new { line_uid = line_uid }); + + if (lineUser == null) { + context.Response.Write("無此Line Uid資料"); + return; + } + + babyData babyData = conn.QueryFirstOrDefault("select * from babyData where line_uid = @line_uid and babyData_uid = @babyData_uid", new { line_uid = line_uid, babyData_uid = babyData_uid }); + + if (babyData == null) { + context.Response.Write("無此寶寶資料"); + return; + } + + List babyRecs = conn.Query("select * from babyRec where line_uid = @line_uid and babyData_uid = @babyData_uid order by babyRec_months desc, babyRec_sn desc", new { line_uid = line_uid, babyData_uid = babyData_uid }).ToList(); + + HSSFWorkbook workbook = new HSSFWorkbook(); + MemoryStream ms = new MemoryStream(); + HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(); + HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0); + + + + HSSFRow row; + + row = (HSSFRow)sheet.GetRow(0); + row.CreateCell(0); + row.CreateCell(1); + row.CreateCell(2); + row.CreateCell(3); + sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3)); + + row.CreateCell(0).SetCellValue("Line 名稱:" + lineUser.line_displayName + " ,Line UID:" + lineUser.line_uid); + + HSSFRow babyRow = (HSSFRow)sheet.CreateRow(1); + babyRow.CreateCell(0); + babyRow.CreateCell(1); + babyRow.CreateCell(2); + babyRow.CreateCell(3); + sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 3)); + + string gender = ""; + + if (babyData.babyData_sexual == "M") + { + gender = "男"; + } + else + { + gender = "女"; + } + babyRow.CreateCell(0).SetCellValue("寶寶姓名:" + babyData.babyData_name + " (" + gender + ") " + " ,生日:" + babyData.babyData_birthday.ToString("yyyy/MM/dd")); + + + + HSSFRow blankRow = (HSSFRow)sheet.CreateRow(2); + blankRow.CreateCell(0); + blankRow.CreateCell(1); + blankRow.CreateCell(2); + blankRow.CreateCell(3); + sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 0, 3)); + + + HSSFRow titleRow = (HSSFRow)sheet.CreateRow(3); + + titleRow.CreateCell(0).SetCellValue("測驗日期"); + titleRow.CreateCell(1).SetCellValue("測驗時年齡"); + titleRow.CreateCell(2).SetCellValue("寶寶身高"); + titleRow.CreateCell(3).SetCellValue("寶寶成長百分比"); + + int pageNum = 4; + + foreach (babyRec rec in babyRecs) { + string baby_inpercent = ""; + + if (rec.babyRec_inpercent > 97) + { + baby_inpercent = "大於 97%"; + } + else + { + baby_inpercent = rec.babyRec_inpercent.ToString() + "%"; + + } + + HSSFRow excelRow = (HSSFRow)sheet.CreateRow(pageNum); + excelRow.CreateCell(0).SetCellValue(rec.babyRec_recdate.ToString("yyyy/MM/dd")); + excelRow.CreateCell(1).SetCellValue(rec.babyRec_yearMonthStr); + excelRow.CreateCell(2).SetCellValue(rec.babyRec_height); + excelRow.CreateCell(3).SetCellValue(baby_inpercent); + pageNum++; + } + + + for (int j = 0; j <= 3; j++) + { + sheet.AutoSizeColumn(j); + sheet.SetColumnWidth(j, sheet.GetColumnWidth(j) * 17 / 10); + } + + workbook.Write(ms); + ms.Flush(); + + string fileName = HttpUtility.UrlEncode(lineUser.line_displayName + "的[" + babyData.babyData_name + "]寶寶成長資料_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls", Encoding.UTF8); + context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; + context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=" + fileName + ";filename*=utf-8''" + fileName)); + //context.Response.ContentType = "application/vnd.ms-excel;charset=utf-8"; + context.Response.BinaryWrite(ms.ToArray()); + } + + public bool IsReusable + { + get + { + return false; + } + } + } +} \ No newline at end of file diff --git a/BackEnd/api/exportExcel.ashx.cs b/BackEnd/api/exportExcel.ashx.cs index 1716467..8341a19 100644 --- a/BackEnd/api/exportExcel.ashx.cs +++ b/BackEnd/api/exportExcel.ashx.cs @@ -137,6 +137,7 @@ namespace abbott_2024_event.BackEnd.api for (int j = 0; j <= 8; j++) { sheet.AutoSizeColumn(j); + sheet.SetColumnWidth(j, sheet.GetColumnWidth(j) * 17 / 10); } diff --git a/BackEnd/assets/javascript/custom/userList.js b/BackEnd/assets/javascript/custom/userList.js index 850abdd..3fc7396 100644 --- a/BackEnd/assets/javascript/custom/userList.js +++ b/BackEnd/assets/javascript/custom/userList.js @@ -63,6 +63,16 @@ $(document).ready(function () { loadBabyRec(); }); + $('#downloadBtn').click(function () { + var line_uid = $('#line_uid_data').val(); + var baby_uid = $('#baby_select').val(); + + + var url = "api/exportBabyRec.ashx?line_uid=" + line_uid + "&baby_uid=" + baby_uid; + window.open(url); + + }); + }); function loadBabyRec() { @@ -102,6 +112,8 @@ function buttonClick(obj) { line_uid: uid } + $('#line_uid_data').val(uid); + $.ajax({ url: "api/lineData.ashx", type: "POST", diff --git a/BackEnd/userList.aspx b/BackEnd/userList.aspx index 06e5a32..ec33a27 100644 --- a/BackEnd/userList.aspx +++ b/BackEnd/userList.aspx @@ -137,7 +137,7 @@