实现碳排放-导出碳排放趋势报表接口

This commit is contained in:
曾艳 2024-08-27 21:17:19 +08:00
parent f896f76693
commit 0bd2971441
51 changed files with 231 additions and 15 deletions

View File

@ -50,6 +50,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="api\export_carbon_trendData.cs" />
<Compile Include="api\get_airconditioning_load.cs" />
<Compile Include="api\get_big_screen.cs" />
<Compile Include="api\get_boot_strategy.cs" />

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataService.api
{
public class export_carbon_trendData
{
public string time { get; set; }
public decimal? EH { get; set; }
public decimal? carbon { get; set; }
}
}

View File

@ -1 +1 @@
c3cf913734ddb361f6463ef3db77ddcaf8233bc9
6017feb255b2d0674f29d5c15533bc86cfcc26f7

View File

@ -0,0 +1,188 @@
using DataService.api;
using Newtonsoft.Json;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Http.Results; // 添加对System.Web.Http.Results的引用
using System.Net.Http.Headers;
using NPOI.SS.Formula;
namespace LonglslandExhibitionCenter.Controllers.api
{
/// <summary>
/// 碳排放-导出碳排放趋势
/// </summary>
public class ExportCarbonTrendController : ApiController
{
DataService.BLL.electricity_data bll = new DataService.BLL.electricity_data();
public HttpResponseMessage Get()
{
try
{
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
var data = new List<export_carbon_trendData>();
var now = DateTime.Now;
var time_count = Convert.ToInt32(DateTime.Now.Month);
for (int i = 0; i < time_count; i++)
{
string sdate;
if (i == 0)
{
sdate = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
}
else
{
sdate = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00");
//sdate = Convert.ToDateTime(sdate1).AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
}
var edate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00");
var etime = Convert.ToDateTime(edate).ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + etime);
if (b == false)
{
bll.CreateTable(etime);
}
string stime = Convert.ToDateTime(sdate).ToString("yyyyMM");
var c = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (c == false)
{
bll.CreateTable(stime);
}
var alist = bll.GetModelListDate(" EntireTime='" + sdate + "' and Reserve1='配电室低压'", stime);
var blist = bll.GetModelListDate(" EntireTime='" + edate + "' and Reserve1='配电室低压'", etime);
var model = new export_carbon_trendData()
{
time = DateTime.Now.AddMonths(-i).ToString("MM月"),
EH = Convert.ToDecimal(alist.Sum(x => x.EH) - blist.Sum(x => x.EH)),
carbon = Convert.ToDecimal(Math.Round(Convert.ToDouble(alist.Sum(x => x.EH) - blist.Sum(x => x.EH)) / 1000 * 0.5703, 3))
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
var adata = data.OrderBy(x => x.time).ToList();
// 1. 加载模板
var templatePath = HostingEnvironment.MapPath("/Upload/长岛海洋生态文明展览馆碳排放统计报表.docx");
// 使用单独的流加载模板
using (var templateStream = File.OpenRead(templatePath))
{
// 创建 XWPFDocument 实例
using (var docStream = new MemoryStream())
{
templateStream.CopyTo(docStream);
docStream.Position = 0; // 重置流的位置
var doc = new XWPFDocument(docStream);
// 3. 替换占位符
ReplacePlaceholders(doc, adata);
// 4. 生成内存中的字节流
var memoryStream = new MemoryStream();
doc.Write(memoryStream);
memoryStream.Position = 0; // 重置流的位置以便读取
HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
response.Content = new StreamContent(memoryStream);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/docx");
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = $"{now.ToString("yyyy年")}长岛海洋生态文明展览馆碳排放统计报表.docx"
};
return response;
}
}
}
catch (Exception ex)
{
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(new { code = 500, msg = "失败:" + ex.Message }), Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
}
private void ReplacePlaceholders(XWPFDocument doc, List<export_carbon_trendData> data)
{
// 替换标题部分的占位符
ReplaceTextInDocument(doc, "year-month", DateTime.Now.ToString("yyyy年MM月"));
ReplaceTextInDocument(doc, "electricity", data.Where(a => a.time == DateTime.Now.ToString("MM月")).FirstOrDefault().EH.ToString());
ReplaceTextInDocument(doc, "carbon", data.Where(a => a.time == DateTime.Now.ToString("MM月")).FirstOrDefault().carbon.ToString());
// 替换表格中的数据
var table = doc.Tables.First();
if (table != null)
{
for (int i = 1; i < table.Rows.Count; i++)
{
var month_str = table.Rows[i].GetCell(0).GetText();
if (month_str != "合计")
{
string month1 = month_str.Replace("月", "");
month1 = month1.Length < 2 ? "0" + month1 + "月" : month1 + "月";
var data_model = data.Where(a => a.time == month1).FirstOrDefault();
if (data_model != null)
{
//table.Rows[i].GetCell(1).SetText(data_model.EH.ToString());
//table.Rows[i].GetCell(2).SetText(data_model.carbon.ToString());
SetTextWithFontAndSize(table.Rows[i].GetCell(1), data_model.EH.ToString(), "SimSun", 14);
SetTextWithFontAndSize(table.Rows[i].GetCell(2), data_model.carbon.ToString(), "SimSun", 14);
}
}
else
{
//table.Rows[i].GetCell(1).SetText(data.Sum(a => a.EH).ToString());
//table.Rows[i].GetCell(2).SetText(data.Sum(a => a.carbon).ToString());
SetTextWithFontAndSize(table.Rows[i].GetCell(1), data.Sum(a => a.EH).ToString(), "SimSun", 14);
SetTextWithFontAndSize(table.Rows[i].GetCell(2), data.Sum(a => a.carbon).ToString(), "SimSun", 14);
}
}
}
}
private void SetTextWithFontAndSize(XWPFTableCell cell, string text, string fontName, int fontSize)
{
// 创建新的段落
XWPFParagraph paragraph1 = cell.Paragraphs[0];
// 创建新的 Run
XWPFRun run = paragraph1.CreateRun();
// 设置字体和字号
run.SetFontFamily(fontName, FontCharRange.None); // 设置字体
run.FontSize = fontSize; // 设置字号大小
// 设置文本
run.SetText(text);
}
private void ReplaceTextInDocument(XWPFDocument doc, string placeholder, string replacement)
{
foreach (XWPFParagraph paragraph in doc.Paragraphs)
{
foreach (XWPFRun run in paragraph.Runs)
{
if (run.Text != null)
{
}
if (run.Text != null && run.Text.Contains(placeholder))
{
run.SetText(run.Text.Replace(placeholder, replacement), 0);
}
}
}
}
}
}

View File

@ -236,6 +236,7 @@
<Compile Include="Areas\HelpPage\SampleGeneration\SampleDirection.cs" />
<Compile Include="Areas\HelpPage\SampleGeneration\TextSample.cs" />
<Compile Include="Areas\HelpPage\XmlDocumentationProvider.cs" />
<Compile Include="Controllers\api\ExportCarbonTrendController.cs" />
<Compile Include="Controllers\api\GetAirconditioningLoadController.cs" />
<Compile Include="Controllers\api\GetBigScreenController.cs" />
<Compile Include="Controllers\api\GetBootStrategyController.cs" />
@ -340,6 +341,9 @@
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Upload\长岛海洋生态文明展览馆碳排放统计报表.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View File

@ -5,7 +5,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<Project>
<PropertyGroup>
<_PublishTargetUrl>F:\项目\长岛展览馆\发布文件</_PublishTargetUrl>
<History>True|2024-08-21T03:09:27.6586574Z;True|2024-08-19T10:17:16.4446095+08:00;True|2024-08-15T13:20:14.7663530+08:00;True|2024-08-14T10:07:28.2388461+08:00;True|2024-08-14T10:07:03.3134871+08:00;True|2024-08-14T10:06:43.4199921+08:00;True|2024-08-14T10:05:39.6303984+08:00;True|2024-08-14T10:00:18.4168360+08:00;True|2024-08-14T09:50:15.7791885+08:00;True|2024-08-11T20:53:54.5556138+08:00;True|2024-08-10T16:00:18.6655642+08:00;True|2024-08-10T10:41:02.8962798+08:00;True|2024-08-10T10:31:34.0807175+08:00;True|2024-08-10T10:11:01.4518697+08:00;True|2024-08-09T17:46:23.3977253+08:00;True|2024-08-09T14:57:51.6409237+08:00;True|2024-08-09T14:12:02.8124286+08:00;True|2024-08-09T13:57:09.6566238+08:00;True|2024-08-09T13:55:09.1957591+08:00;True|2024-08-09T13:53:12.0978886+08:00;True|2024-08-09T10:21:25.5364378+08:00;True|2024-08-08T17:30:17.0495176+08:00;True|2024-08-08T14:01:01.6427032+08:00;True|2024-08-08T10:26:20.9380493+08:00;True|2024-08-06T16:16:42.4971554+08:00;True|2024-04-15T08:46:26.1708600+08:00;True|2024-04-15T08:43:47.1675051+08:00;True|2024-03-20T09:52:41.5444999+08:00;True|2024-03-20T09:52:28.9463180+08:00;True|2024-03-19T16:26:27.2407972+08:00;True|2024-03-19T15:50:07.1464827+08:00;True|2024-03-14T15:48:46.0852411+08:00;True|2024-03-12T11:15:35.2934238+08:00;True|2024-03-07T16:29:08.9381292+08:00;True|2024-03-05T14:31:05.6269677+08:00;True|2024-03-04T14:37:08.7040845+08:00;True|2024-02-28T11:11:35.8506164+08:00;</History>
<History>True|2024-08-27T13:03:21.5928516Z;True|2024-08-21T11:09:27.6586574+08:00;True|2024-08-19T10:17:16.4446095+08:00;True|2024-08-15T13:20:14.7663530+08:00;True|2024-08-14T10:07:28.2388461+08:00;True|2024-08-14T10:07:03.3134871+08:00;True|2024-08-14T10:06:43.4199921+08:00;True|2024-08-14T10:05:39.6303984+08:00;True|2024-08-14T10:00:18.4168360+08:00;True|2024-08-14T09:50:15.7791885+08:00;True|2024-08-11T20:53:54.5556138+08:00;True|2024-08-10T16:00:18.6655642+08:00;True|2024-08-10T10:41:02.8962798+08:00;True|2024-08-10T10:31:34.0807175+08:00;True|2024-08-10T10:11:01.4518697+08:00;True|2024-08-09T17:46:23.3977253+08:00;True|2024-08-09T14:57:51.6409237+08:00;True|2024-08-09T14:12:02.8124286+08:00;True|2024-08-09T13:57:09.6566238+08:00;True|2024-08-09T13:55:09.1957591+08:00;True|2024-08-09T13:53:12.0978886+08:00;True|2024-08-09T10:21:25.5364378+08:00;True|2024-08-08T17:30:17.0495176+08:00;True|2024-08-08T14:01:01.6427032+08:00;True|2024-08-08T10:26:20.9380493+08:00;True|2024-08-06T16:16:42.4971554+08:00;True|2024-04-15T08:46:26.1708600+08:00;True|2024-04-15T08:43:47.1675051+08:00;True|2024-03-20T09:52:41.5444999+08:00;True|2024-03-20T09:52:28.9463180+08:00;True|2024-03-19T16:26:27.2407972+08:00;True|2024-03-19T15:50:07.1464827+08:00;True|2024-03-14T15:48:46.0852411+08:00;True|2024-03-12T11:15:35.2934238+08:00;True|2024-03-07T16:29:08.9381292+08:00;True|2024-03-05T14:31:05.6269677+08:00;True|2024-03-04T14:37:08.7040845+08:00;True|2024-02-28T11:11:35.8506164+08:00;</History>
<LastFailureDetails />
</PropertyGroup>
<ItemGroup>
@ -82,10 +82,10 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<publishTime>05/08/2024 00:05:28</publishTime>
</File>
<File Include="bin/DataService.dll">
<publishTime>08/21/2024 10:53:04</publishTime>
<publishTime>08/27/2024 16:21:01</publishTime>
</File>
<File Include="bin/DataService.pdb">
<publishTime>08/21/2024 10:53:04</publishTime>
<publishTime>08/27/2024 16:21:01</publishTime>
</File>
<File Include="bin/Enums.NET.dll">
<publishTime>11/19/2022 06:40:50</publishTime>
@ -106,10 +106,10 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<publishTime>08/13/2024 17:44:43</publishTime>
</File>
<File Include="bin/LonglslandExhibitionCenter.dll">
<publishTime>08/22/2024 08:47:34</publishTime>
<publishTime>08/27/2024 21:03:15</publishTime>
</File>
<File Include="bin/LonglslandExhibitionCenter.pdb">
<publishTime>08/22/2024 08:47:34</publishTime>
<publishTime>08/27/2024 21:03:15</publishTime>
</File>
<File Include="bin/MathNet.Numerics.dll">
<publishTime>04/03/2022 22:02:06</publishTime>
@ -378,6 +378,9 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<File Include="bin/System.Web.WebPages.Razor.dll">
<publishTime>08/13/2024 17:44:44</publishTime>
</File>
<File Include="bin/Upload/长岛海洋生态文明展览馆碳排放统计报表.docx">
<publishTime>08/27/2024 17:56:50</publishTime>
</File>
<File Include="bin/WebGrease.dll">
<publishTime>08/13/2024 17:44:45</publishTime>
</File>
@ -627,6 +630,9 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<File Include="Scripts/modernizr-2.8.3.js">
<publishTime>08/13/2024 17:44:43</publishTime>
</File>
<File Include="Upload/长岛海洋生态文明展览馆碳排放统计报表.docx">
<publishTime>08/27/2024 17:56:50</publishTime>
</File>
<File Include="Util/MqttClientService.cs">
<publishTime>02/23/2024 18:15:19</publishTime>
</File>
@ -646,7 +652,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<publishTime>08/13/2024 17:44:43</publishTime>
</File>
<File Include="Web.config">
<publishTime>08/21/2024 11:09:23</publishTime>
<publishTime>08/27/2024 21:03:16</publishTime>
</File>
</ItemGroup>
</Project>

View File

@ -0,0 +1,61 @@
长岛海洋生态文明展览馆
碳排放统计报表
2021年烟台市提出打造`长岛国际零碳岛'组织专家团队进行了科学研究规划成果在2023年联合国气候变化迪拜大会发布目标是到2035年长岛全域实现净零排放。"
year-month长岛海洋生态文明展览馆用电量为electricity千瓦时二氧化碳排放量为carbon吨。
月份
用电量kWh
碳排放量tCO2
备注
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
合计

View File

@ -0,0 +1,61 @@
长岛海洋生态文明展览馆
碳排放统计报表
2021年烟台市提出打造`长岛国际零碳岛'组织专家团队进行了科学研究规划成果在2023年联合国气候变化迪拜大会发布目标是到2035年长岛全域实现净零排放。"
year-month长岛海洋生态文明展览馆用电量为electricity千瓦时二氧化碳排放量为carbon吨。
月份
用电量kWh
碳排放量tCO2
备注
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
合计

View File

@ -1 +1 @@
4b591e42a6dd7d138363b45be2e425a31b74a7b6
dcca6df9edb39b80de334543423b2ffe45b3e4f9

View File

@ -359,3 +359,4 @@ F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibition
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.Text.Encoding.CodePages.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.Threading.Tasks.Extensions.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.ValueTuple.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\Upload\长岛海洋生态文明展览馆碳排放统计报表.docx

View File

@ -1 +1 @@
1130d80daa9fc1cd10922448edfc6b9291466279
50b9a3ad61bd81ee30edc846430d43764af1d686

View File

@ -359,3 +359,4 @@ F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibition
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.Text.Encoding.CodePages.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.Threading.Tasks.Extensions.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\System.ValueTuple.xml
F:\项目\长岛展览馆\项目\LonglslandExhibitionCenter\LonglslandExhibitionCenter\bin\Upload\长岛海洋生态文明展览馆碳排放统计报表.docx

View File

@ -0,0 +1,61 @@
长岛海洋生态文明展览馆
碳排放统计报表
2021年烟台市提出打造`长岛国际零碳岛'组织专家团队进行了科学研究规划成果在2023年联合国气候变化迪拜大会发布目标是到2035年长岛全域实现净零排放。"
year-month长岛海洋生态文明展览馆用电量为electricity千瓦时二氧化碳排放量为carbon吨。
月份
用电量kWh
碳排放量tCO2
备注
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
合计

View File

@ -0,0 +1,61 @@
长岛海洋生态文明展览馆
碳排放统计报表
2021年烟台市提出打造`长岛国际零碳岛'组织专家团队进行了科学研究规划成果在2023年联合国气候变化迪拜大会发布目标是到2035年长岛全域实现净零排放。"
year-month长岛海洋生态文明展览馆用电量为electricity千瓦时二氧化碳排放量为carbon吨。
月份
用电量kWh
碳排放量tCO2
备注
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
合计

Binary file not shown.

Binary file not shown.

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>am/UoKx1xcbJZe2j6qPEiRFzkrkEoJm0CaKgQWbU5QI=</dsig:DigestValue>
<dsig:DigestValue>iv32QMruZnKNEzz+kjmmksq99mmjWfL88nEu1GZD+2g=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -42,14 +42,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="DataService.dll" size="137216">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="DataService.dll" size="137728">
<assemblyIdentity name="DataService" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>ofUi/SXQiFo/xrBGmLMyy2R9zKdk7aWMJ95SkwBIhCA=</dsig:DigestValue>
<dsig:DigestValue>a/nR4DV6YEQ/KwyHLMWEtpI6EpPcs7wguD1ag2UAtTg=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>am/UoKx1xcbJZe2j6qPEiRFzkrkEoJm0CaKgQWbU5QI=</dsig:DigestValue>
<dsig:DigestValue>iv32QMruZnKNEzz+kjmmksq99mmjWfL88nEu1GZD+2g=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -42,14 +42,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="DataService.dll" size="137216">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="DataService.dll" size="137728">
<assemblyIdentity name="DataService" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>ofUi/SXQiFo/xrBGmLMyy2R9zKdk7aWMJ95SkwBIhCA=</dsig:DigestValue>
<dsig:DigestValue>a/nR4DV6YEQ/KwyHLMWEtpI6EpPcs7wguD1ag2UAtTg=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

Binary file not shown.

Binary file not shown.