final beta 1

master
dk96 2024-12-18 11:40:43 +08:00
parent 476d347ed3
commit aaddec10d4
7 changed files with 184 additions and 1 deletions

View File

@ -0,0 +1 @@
<%@ WebHandler Language="C#" CodeBehind="exportBabyRec.ashx.cs" Class="abbott_2024_event.BackEnd.api.exportBabyRec" %>

View File

@ -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
{
/// <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("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;
}
}
}
}

View File

@ -137,6 +137,7 @@ namespace abbott_2024_event.BackEnd.api
for (int j = 0; j <= 8; j++) for (int j = 0; j <= 8; j++)
{ {
sheet.AutoSizeColumn(j); sheet.AutoSizeColumn(j);
sheet.SetColumnWidth(j, sheet.GetColumnWidth(j) * 17 / 10);
} }

View File

@ -63,6 +63,16 @@ $(document).ready(function () {
loadBabyRec(); 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() { function loadBabyRec() {
@ -102,6 +112,8 @@ function buttonClick(obj) {
line_uid: uid line_uid: uid
} }
$('#line_uid_data').val(uid);
$.ajax({ $.ajax({
url: "api/lineData.ashx", url: "api/lineData.ashx",
type: "POST", type: "POST",

View File

@ -137,7 +137,7 @@
</div><!-- /.modal-header --> </div><!-- /.modal-header -->
<!-- .modal-body --> <!-- .modal-body -->
<div class="modal-body"> <div class="modal-body">
<input type="hidden" id="line_uid_data" />
<!-- .page-section --> <!-- .page-section -->
<div class="page-section"> <div class="page-section">

View File

@ -13325,6 +13325,9 @@
<Compile Include="BackEnd\api\chgPassword.ashx.cs"> <Compile Include="BackEnd\api\chgPassword.ashx.cs">
<DependentUpon>chgPassword.ashx</DependentUpon> <DependentUpon>chgPassword.ashx</DependentUpon>
</Compile> </Compile>
<Compile Include="BackEnd\api\exportBabyRec.ashx.cs">
<DependentUpon>exportBabyRec.ashx</DependentUpon>
</Compile>
<Compile Include="BackEnd\api\exportExcel.ashx.cs"> <Compile Include="BackEnd\api\exportExcel.ashx.cs">
<DependentUpon>exportExcel.ashx</DependentUpon> <DependentUpon>exportExcel.ashx</DependentUpon>
</Compile> </Compile>
@ -17896,6 +17899,7 @@
<Content Include="BackEnd\api\exportExcel.ashx" /> <Content Include="BackEnd\api\exportExcel.ashx" />
<Content Include="BackEnd\api\lineData.ashx" /> <Content Include="BackEnd\api\lineData.ashx" />
<Content Include="BackEnd\api\babyRecList.ashx" /> <Content Include="BackEnd\api\babyRecList.ashx" />
<Content Include="BackEnd\api\exportBabyRec.ashx" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Web.Debug.config"> <None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon> <DependentUpon>Web.config</DependentUpon>

Binary file not shown.