216 lines
7.9 KiB
C#
216 lines
7.9 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// exportBabyRec 的摘要描述
|
|
/// </summary>
|
|
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<lineUser>("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<babyData>("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<babyRec> babyRecs = conn.Query<babyRec>("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("媽媽姓名:" + 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("寶寶成長百分比");
|
|
titleRow.CreateCell(4).SetCellValue("utm_source");
|
|
titleRow.CreateCell(5).SetCellValue("utm_medium");
|
|
titleRow.CreateCell(6).SetCellValue("utm_campaign");
|
|
titleRow.CreateCell(7).SetCellValue("utm_content");
|
|
titleRow.CreateCell(8).SetCellValue("utm_term");
|
|
|
|
int pageNum = 4;
|
|
|
|
foreach (babyRec rec in babyRecs) {
|
|
|
|
|
|
string inpercent = "";
|
|
// Replace the switch statement with an if-else block since babyRec_inpercent is a double and cannot be used in a switch statement.
|
|
|
|
|
|
|
|
|
|
switch ((int)rec.babyRec_inpercent)
|
|
{
|
|
case 3:
|
|
{
|
|
inpercent = "低於3%";
|
|
break;
|
|
}
|
|
case 15:
|
|
{
|
|
inpercent = "3~14%";
|
|
break;
|
|
}
|
|
case 25:
|
|
{
|
|
inpercent = "15~49%";
|
|
break;
|
|
}
|
|
case 50:
|
|
{
|
|
inpercent = "15~49%";
|
|
break;
|
|
}
|
|
case 75:
|
|
{
|
|
inpercent = "50%以上 ";
|
|
break;
|
|
}
|
|
case 85:
|
|
{
|
|
inpercent = "50%以上 ";
|
|
break;
|
|
}
|
|
case 97:
|
|
{
|
|
inpercent = "50%以上 ";
|
|
break;
|
|
}
|
|
case 100:
|
|
{
|
|
inpercent = "50%以上 ";
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
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(inpercent);
|
|
excelRow.CreateCell(4).SetCellValue(rec.utm_source);
|
|
excelRow.CreateCell(5).SetCellValue(rec.utm_medium);
|
|
excelRow.CreateCell(6).SetCellValue(rec.utm_campaign);
|
|
excelRow.CreateCell(7).SetCellValue(rec.utm_content);
|
|
excelRow.CreateCell(8).SetCellValue(rec.utm_term);
|
|
pageNum++;
|
|
}
|
|
|
|
|
|
for (int j = 0; j <= 8; 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;
|
|
}
|
|
}
|
|
}
|
|
} |