public static MemoryStream GenerateExcel(Human human, Nutrition nutrition, double Tdee) { string filepath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); filepath = filepath + "/File"; MemoryStream outputStream = new MemoryStream(); using (ExcelPackage ep = new ExcelPackage(outputStream)) { ep.Workbook.Worksheets.Add("sheet1"); ExcelWorksheet sheet = ep.Workbook.Worksheets["sheet1"]; sheet.Cells["A1"].Value = $"您目前的身體資訊為:身高:{human.Height}cm,體重:{human.Weight}kg,年齡:{human.Age}歲"; sheet.Cells["A2"].Value = "計算結果如下"; sheet.Cells["A3"].Value = "TDEE:"; sheet.Cells["B3"].Value = string.Format("{0:F0}kcal", Tdee); sheet.Cells["A4"].Value = "碳水化合物"; sheet.Cells["B4"].Value = string.Format("{0:F2}g", nutrition.Carbon); sheet.Cells["A5"].Value = "蛋白質"; sheet.Cells["B5"].Value = string.Format("{0:F2}g", nutrition.Protein); sheet.Cells["A6"].Value = "脂肪"; sheet.Cells["B6"].Value = string.Format("{0:F2}g", nutrition.Fat); sheet.Cells.AutoFitColumns(); if (File.Exists(filepath)) { File.Delete(filepath); } FileStream fs = new FileStream(filepath + "/Tdee計算結果.xls", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); ep.SaveAs(fs); fs.Close(); } outputStream.Position = 0; return(outputStream); }
//計算每天所需營養素 public Nutrition CalculateNutrition(Human human) { CalculateTdee(human); double _weightbylbl = human.Weight * 2.2; //體重換算成磅計算 double protein = 0, fat = 0, carbon = 0; protein = _weightbylbl; //每磅體重1g蛋白 fat = _weightbylbl * 0.4; //每磅體重0.4g脂肪 carbon = (_tdee - (protein * 4) - (fat * 9)) / 4; var temp = new Nutrition() { Carbon = carbon, Protein = protein, Fat = fat }; return(temp); }