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; } } } }