247 lines
12 KiB
C#
247 lines
12 KiB
C#
using DocumentFormat.OpenXml;
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
using Resources;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.UI.DataVisualization.Charting;
|
|
using Weee.DataTransferObject;
|
|
|
|
namespace Weee.Supports
|
|
{
|
|
public class OrganizationLCADocParser
|
|
{
|
|
private WordprocessingDocument wordDocument;
|
|
public OrganizationLCADocParser(WordprocessingDocument wordDocument)
|
|
{
|
|
this.wordDocument = wordDocument;
|
|
}
|
|
public void ReplaceAllTextTagWithData(OrganizationLCAReportDTO data)
|
|
{
|
|
//產生衍生屬性
|
|
var body = wordDocument.MainDocumentPart.Document.Body;
|
|
var derived = new DerivedProperties();
|
|
derived.TotalValue = data.WorkhourSum +
|
|
data.PowerSum +
|
|
data.CarSum +
|
|
data.GasolineSum +
|
|
data.KitchenSum +
|
|
data.FireEQSum +
|
|
data.RefSum +
|
|
data.SteamSum;
|
|
derived.WorkhourPercent = derived.TotalValue == 0 ? 0 : data.WorkhourSum / derived.TotalValue;//DL-75 begin
|
|
derived.PowerPercent = derived.TotalValue == 0 ? 0 : data.PowerSum / derived.TotalValue;
|
|
derived.CarPercent = derived.TotalValue == 0 ? 0 : data.CarSum / derived.TotalValue;
|
|
derived.GasolinePercent = derived.TotalValue == 0 ? 0 : data.GasolineSum / derived.TotalValue;
|
|
derived.KitchenPercent = derived.TotalValue == 0 ? 0 : data.KitchenSum / derived.TotalValue;
|
|
derived.FireEQPercent = derived.TotalValue == 0 ? 0 : data.FireEQSum / derived.TotalValue;
|
|
derived.RefPercent = derived.TotalValue == 0 ? 0 : data.RefSum / derived.TotalValue;
|
|
derived.SteamPercent = derived.TotalValue == 0 ? 0 : data.SteamSum / derived.TotalValue;
|
|
|
|
derived.ScopeOneTotalValuePercent = derived.TotalValue == 0 ? 0 : data.ScopeOneTotalValue / derived.TotalValue;//DL-75 end
|
|
derived.ScopeTwoTotalValue = derived.TotalValue - data.ScopeOneTotalValue;
|
|
|
|
derived.MainSurveyFormName = "總工時";
|
|
derived.MainSurveyFormPercent = derived.WorkhourPercent;
|
|
var max = derived.WorkhourPercent;
|
|
if (max < derived.PowerPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.PowerPercent;
|
|
derived.MainSurveyFormName = "用電量";
|
|
}
|
|
if (max < derived.CarPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.CarPercent;
|
|
derived.MainSurveyFormName = "車輛";
|
|
}
|
|
if (max < derived.GasolinePercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.GasolinePercent;
|
|
derived.MainSurveyFormName = "柴油";
|
|
}
|
|
if (max < derived.KitchenPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.KitchenPercent;
|
|
derived.MainSurveyFormName = "廚房";
|
|
}
|
|
if (max < derived.FireEQPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.FireEQPercent;
|
|
derived.MainSurveyFormName = "消防設備";
|
|
}
|
|
if (max < derived.RefPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.RefPercent;
|
|
derived.MainSurveyFormName = "冷媒";
|
|
}
|
|
if (max < derived.SteamPercent)
|
|
{
|
|
derived.MainSurveyFormPercent = max = derived.SteamPercent;
|
|
derived.MainSurveyFormName = "用蒸氣量";
|
|
}
|
|
//replace Tag
|
|
foreach (var paragraph in body.Elements<Paragraph>())
|
|
{
|
|
ReplaceText(paragraph, data, derived);
|
|
}
|
|
|
|
foreach (var table in body.Elements<Table>())
|
|
{
|
|
foreach (var row in table.Elements<TableRow>())
|
|
{
|
|
foreach (var cell in row.Elements<TableCell>())
|
|
{
|
|
foreach (var paragraph in cell.Elements<Paragraph>())
|
|
{
|
|
ReplaceText(paragraph, data, derived);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//generateTable
|
|
var tables = body.Elements<Table>().ToList();
|
|
|
|
GenerateTable(data.Table1Data, tables[2]);
|
|
|
|
//generate Pie Chart
|
|
var chart = new Chart();
|
|
chart.Height = 720;
|
|
chart.Width = 1024;
|
|
|
|
var chartArea = chart.ChartAreas.Add("PercentageOfCarbonEmission");
|
|
|
|
var series = chart.Series.Add("PercentageOfCarbonEmission");
|
|
series.ChartType = SeriesChartType.Pie;
|
|
series.IsValueShownAsLabel = true;
|
|
series.Points.Add((double)derived.WorkhourPercent).Label = "總工時 " + derived.WorkhourPercent.ToString("P2");
|
|
series.Points.Add((double)derived.PowerPercent).Label = "用電量 " + derived.PowerPercent.ToString("P2");
|
|
series.Points.Add((double)derived.CarPercent).Label = "車輛 " + derived.CarPercent.ToString("P2");
|
|
series.Points.Add((double)derived.GasolinePercent).Label = "柴油 " + derived.GasolinePercent.ToString("P2");
|
|
series.Points.Add((double)derived.KitchenPercent).Label = "廚房 " + derived.KitchenPercent.ToString("P2");
|
|
series.Points.Add((double)derived.FireEQPercent).Label = "消防設備 " + derived.FireEQPercent.ToString("P2");
|
|
series.Points.Add((double)derived.RefPercent).Label = "冷媒 " + derived.RefPercent.ToString("P2");
|
|
series.Points.Add((double)derived.SteamPercent).Label = "用蒸氣量 " + derived.SteamPercent.ToString("P2");
|
|
|
|
|
|
var image = wordDocument.MainDocumentPart.ImageParts.ToList().Last();
|
|
var imageMemoryStream = new MemoryStream();
|
|
chart.SaveImage(imageMemoryStream, ChartImageFormat.Png);
|
|
imageMemoryStream.Seek(0, SeekOrigin.Begin);
|
|
image.FeedData(imageMemoryStream);
|
|
//imageMemoryStream = new MemoryStream();
|
|
//chart.SaveImage(imageMemoryStream, ChartImageFormat.Png);
|
|
|
|
}
|
|
|
|
private void ReplaceText(Paragraph paragraph, OrganizationLCAReportDTO beReplacedText, DerivedProperties derived)
|
|
{
|
|
var text = paragraph.InnerText;
|
|
string decimalFormat = "G5";
|
|
//地雷: CompanyNameTag是 CertificationCompanyNameTag的substring , CertificationCompanyNameTag必須先replace
|
|
text = text.Replace("CertificationCompanyNameTag", beReplacedText.CertificationCompanyName);
|
|
text = text.Replace("CompanyNameTag", beReplacedText.CompanyName);
|
|
text = text.Replace("FabNameTag", beReplacedText.FabName);
|
|
text = text.Replace("FabAddressTag", beReplacedText.FabAddress);
|
|
text = text.Replace("DuringInventoryTag", beReplacedText.DuringInventory);
|
|
|
|
text = text.Replace("ScopeOneTotalValueTag", beReplacedText.ScopeOneTotalValue.ToString(decimalFormat));
|
|
text = text.Replace("WorkhourSumTag", beReplacedText.WorkhourSum.ToString(decimalFormat));
|
|
text = text.Replace("PowerSumTag", beReplacedText.PowerSum.ToString(decimalFormat));
|
|
|
|
text = text.Replace("CarSumTag", beReplacedText.CarSum.ToString(decimalFormat));
|
|
text = text.Replace("GasolineSumTag", beReplacedText.GasolineSum.ToString(decimalFormat));
|
|
text = text.Replace("KitchenSumTag", beReplacedText.KitchenSum.ToString(decimalFormat));
|
|
text = text.Replace("FireEQSumTag", beReplacedText.FireEQSum.ToString(decimalFormat));
|
|
|
|
text = text.Replace("RefSumTag", beReplacedText.RefSum.ToString(decimalFormat));
|
|
text = text.Replace("SteamSumTag", beReplacedText.SteamSum.ToString(decimalFormat));
|
|
|
|
|
|
text = text.Replace("MainSurveyFormNameTag", derived.MainSurveyFormName);
|
|
text = text.Replace("ScopeTwoTotalValueTag", derived.ScopeTwoTotalValue.ToString(decimalFormat));
|
|
text = text.Replace("MainSurveyFormPercentTag", derived.MainSurveyFormPercent.ToString("P2"));
|
|
text = text.Replace("TotalValueTag", derived.TotalValue.ToString(decimalFormat));
|
|
text = text.Replace("WorkhourPercentTag", derived.WorkhourPercent.ToString("P2"));
|
|
text = text.Replace("PowerPercentTag", derived.PowerPercent.ToString("P2"));
|
|
text = text.Replace("CarPercentTag", derived.CarPercent.ToString("P2"));
|
|
text = text.Replace("GasolinePercentTag", derived.GasolinePercent.ToString("P2"));
|
|
text = text.Replace("KitchenPercentTag", derived.KitchenPercent.ToString("P2"));
|
|
text = text.Replace("FireEQPercentTag", derived.FireEQPercent.ToString("P2"));
|
|
text = text.Replace("RefPercentTag", derived.RefPercent.ToString("P2"));
|
|
text = text.Replace("SteamPercentTag", derived.SteamPercent.ToString("P2"));
|
|
text = text.Replace("ScopeOneTotalValuePercentTag", derived.ScopeOneTotalValuePercent.ToString("P2"));
|
|
|
|
|
|
if (text != paragraph.InnerText)
|
|
{
|
|
var propertity = paragraph.Elements<ParagraphProperties>().First();
|
|
|
|
var runProperty = new RunProperties();
|
|
var runFont = new RunFonts();
|
|
runFont.Ascii = "標楷體";
|
|
runFont.EastAsia = "標楷體";
|
|
|
|
var fontSize = new FontSize();
|
|
fontSize.Val = new StringValue("28");
|
|
|
|
runProperty.Append(runFont);
|
|
runProperty.Append(fontSize);
|
|
|
|
paragraph.RemoveAllChildren();
|
|
|
|
var newRun = new Run();
|
|
newRun.PrependChild<RunProperties>(runProperty);
|
|
newRun.Append(new Text(text));
|
|
|
|
paragraph.PrependChild(propertity);
|
|
paragraph.AppendChild(newRun);
|
|
}
|
|
}
|
|
private void GenerateTable(List<OrganizationLCAReportDTO.table1Row> TableData, Table table)
|
|
{
|
|
var header = table.ToList()[2]; // header
|
|
var template = table.ToList()[3]; // get 1 row for template
|
|
table.RemoveAllChildren<TableRow>();
|
|
table.AppendChild<TableRow>((TableRow)header);
|
|
foreach (var dataRow in TableData)
|
|
{
|
|
var newRow = (TableRow)template.CloneNode(true);
|
|
var textFields = GetTableRowTextField(newRow);
|
|
textFields[0].Text = dataRow.EquipmentName;
|
|
textFields[1].Text = dataRow.RefName;
|
|
textFields[2].Text = dataRow.Value.ToString("G5"); ;
|
|
table.AppendChild<TableRow>(newRow);
|
|
}
|
|
}
|
|
private List<Text> GetTableRowTextField(TableRow targetRow)
|
|
{
|
|
var result = targetRow.Elements<TableCell>()
|
|
.SelectMany(x => x.Elements<Paragraph>())
|
|
.SelectMany(x => x.Elements<Run>())
|
|
.Select(x => x.LastChild)
|
|
.Cast<Text>()
|
|
.ToList();
|
|
return result;
|
|
}
|
|
private class DerivedProperties
|
|
{
|
|
public decimal TotalValue { get; set; }
|
|
public decimal ScopeTwoTotalValue { get; set; }
|
|
public string MainSurveyFormName { get; set; }
|
|
public decimal MainSurveyFormPercent { get; set; }
|
|
public decimal WorkhourPercent { get; set; }
|
|
public decimal PowerPercent { get; set; }
|
|
public decimal CarPercent { get; set; }
|
|
public decimal GasolinePercent { get; set; }
|
|
public decimal KitchenPercent { get; set; }
|
|
public decimal FireEQPercent { get; set; }
|
|
public decimal RefPercent { get; set; }
|
|
public decimal SteamPercent { get; set; }
|
|
public decimal ScopeOneTotalValuePercent { get; set; }
|
|
}
|
|
}
|
|
} |