demo20230512/Supports/ProductLCADocParser.cs

285 lines
14 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Office;
using Weee.DataTransferObject;
namespace Weee.Supports
{
public class ProductLCADocParser
{
private WordprocessingDocument wordDocument;
public ProductLCADocParser(WordprocessingDocument wordDocument)
{
this.wordDocument = wordDocument;
}
public void ReplaceAllTextTagWithData(BeReplacedTextDTO beReplacedText)
{
var body = wordDocument.MainDocumentPart.Document.Body;
foreach (var paragraph in body.Elements<Paragraph>())
{
ReplaceText(paragraph, beReplacedText);
}
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, beReplacedText);
}
}
}
}
var tables = body.Elements<Table>().ToList();
// generate table 5-1
var table5_1 = tables[0];
GenerateTable(beReplacedText.Table1Data, table5_1);
// generate table 5-2
var table5_2 = tables[1];
GenerateTable(beReplacedText.Table2Data, table5_2);
// generate table 5-3
var table5_3 = tables[2];
var header5_3 = table5_3.ToList()[2];
var template5_3 = table5_3.ToList()[3];
table5_3.RemoveAllChildren<TableRow>();
table5_3.AppendChild<TableRow>((TableRow)header5_3);
foreach (var dataRow in beReplacedText.Table3And5Data)
{
var newRow = (TableRow)template5_3.CloneNode(true);
var textFields = GetTableRowTextField(newRow);
textFields[0].Text = "原物料取得與零件製造";
textFields[1].Text = dataRow.MaterialName;
textFields[2].Text = dataRow.SupplierName;
textFields[3].Text = dataRow.DistributionDescription;
table5_3.AppendChild<TableRow>(newRow);
}
var buttomRow5_3 = (TableRow)template5_3.CloneNode(true);
var buttomTextFields5_3 = GetTableRowTextField(buttomRow5_3);
buttomTextFields5_3[0].Text = "產品生產製造";
buttomTextFields5_3[1].Text = "標的產品製造";
buttomTextFields5_3[2].Text = beReplacedText.CompanyName;
buttomTextFields5_3[3].Text = "盤查期間總排碳量÷所有直接人員總工時×標的產品工時1/盤查期間產品產量。";
table5_3.AppendChild<TableRow>(buttomRow5_3);
// generate table 5-5
var table5_5 = tables[5];
var header5_5_1 = table5_5.ToList()[2];
var header5_5_2 = table5_5.ToList()[3];
var template5_5 = table5_5.ToList()[4];
table5_5.RemoveAllChildren<TableRow>();
table5_5.AppendChild<TableRow>((TableRow)header5_5_1);
table5_5.AppendChild<TableRow>((TableRow)header5_5_2);
foreach (var dataRow in beReplacedText.Table3And5Data)
{
var newRow = (TableRow)template5_5.CloneNode(true);
var textFields = GetTableRowTextField(newRow);
textFields[0].Text = "原物料取得與零件製造";
textFields[1].Text = dataRow.MaterialName;
textFields[2].Text = dataRow.SupplierName;
textFields[3].Text = "v";
textFields[4].Text = "";
textFields[5].Text = "";
//textFields[6].Text = dataRow.DistributionDescription;
table5_5.AppendChild<TableRow>(newRow);
}
var rowOfOtherParts = (TableRow)template5_5.CloneNode(true);
var otherPartsTextFields = GetTableRowTextField(rowOfOtherParts);
otherPartsTextFields[0].Text = "原物料取得與零件製造";
otherPartsTextFields[1].Text = "其它零件";
otherPartsTextFields[2].Text = "供應商提供";
otherPartsTextFields[3].Text = "";
otherPartsTextFields[4].Text = "v";
otherPartsTextFields[5].Text = "";
otherPartsTextFields[6].Text = "";
otherPartsTextFields[7].Text = "Ecoinvent2.1";
otherPartsTextFields[8].Text = "";
table5_5.AppendChild<TableRow>(rowOfOtherParts);
var rowOfProduction1 = (TableRow)template5_5.CloneNode(true);
var productionTextFields1 = GetTableRowTextField(rowOfProduction1);
productionTextFields1[0].Text = "產品生產製造";
productionTextFields1[1].Text = "能資源";
productionTextFields1[2].Text = beReplacedText.CompanyName;
productionTextFields1[3].Text = "v";
productionTextFields1[4].Text = "";
productionTextFields1[5].Text = "";
// productionTextFields1[6].Text = "電費收據/水費收據/燃氣收據/蒸氣用量收據 Ecoinvent2.1 IPCC 2006";
table5_5.AppendChild<TableRow>(rowOfProduction1);
var rowOfProduction2 = (TableRow)template5_5.CloneNode(true);
var productionTextFields2 = GetTableRowTextField(rowOfProduction2);
productionTextFields2[0].Text = "產品生產製造";
productionTextFields2[1].Text = "廢棄物運輸";
productionTextFields2[2].Text = beReplacedText.CompanyName;
productionTextFields2[3].Text = "";
productionTextFields2[4].Text = "v";
productionTextFields2[5].Text = "";
productionTextFields2[6].Text = "";
productionTextFields2[7].Text = "Ecoinvent2.1";
productionTextFields2[8].Text = "";
table5_5.AppendChild<TableRow>(rowOfProduction2);
var rowOfDelivery = (TableRow)template5_5.CloneNode(true);
var deliveryTextFields = GetTableRowTextField(rowOfDelivery);
deliveryTextFields[0].Text = "產品配送";
deliveryTextFields[1].Text = "產品運輸";
deliveryTextFields[2].Text = beReplacedText.CompanyName;
deliveryTextFields[3].Text = "";
deliveryTextFields[4].Text = "v";
deliveryTextFields[5].Text = "";
deliveryTextFields[6].Text = "";
deliveryTextFields[7].Text = "Ecoinvent2.1";
deliveryTextFields[8].Text = "";
table5_5.AppendChild<TableRow>(rowOfDelivery);
var rowOfRecovery = (TableRow)template5_5.CloneNode(true);
var recoveryTextFields = GetTableRowTextField(rowOfRecovery);
recoveryTextFields[0].Text = "產品廢棄回收";
recoveryTextFields[1].Text = "產品廢棄排放";
recoveryTextFields[2].Text = beReplacedText.CompanyName;
recoveryTextFields[3].Text = "";
recoveryTextFields[4].Text = "v";
recoveryTextFields[5].Text = "";
recoveryTextFields[6].Text = "";
recoveryTextFields[7].Text = "3R Report, Ecoinvent2.1";
recoveryTextFields[8].Text = "";
table5_5.AppendChild<TableRow>(rowOfRecovery);
}
public void InsertChart(MemoryStream chartStream)
{
if (chartStream == null) return;
var chart = wordDocument.MainDocumentPart.ImageParts.ToList().Last();
chartStream.Seek(0, SeekOrigin.Begin);
chart.FeedData(chartStream);
}
public void SaveDocument()
{
wordDocument.MainDocumentPart.Document.Save();
wordDocument.Close();
}
private void GenerateTable(List<BeReplacedTextDTO.TableParameterRow> 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.Display;
textFields[1].Text = dataRow.Value.ToString("G5");
textFields[2].Text = dataRow.Description;
table.AppendChild<TableRow>(newRow);
}
}
private void ReplaceText(Paragraph paragraph, BeReplacedTextDTO beReplacedText)
{
var text = paragraph.InnerText;
text = text.Replace("ProductNameTag", beReplacedText.ProductName);
text = text.Replace("CompanyNameTag", beReplacedText.CompanyName);
text = text.Replace("FabNameTag", beReplacedText.FabName);
text = text.Replace("FabAddressTag", beReplacedText.FabAddress);
text = text.Replace("PartNumberTag", beReplacedText.PartNumber);
text = text.Replace("ProductSpecTag", beReplacedText.ProductSpec);
text = text.Replace("DuringInventoryTag", beReplacedText.DuringInventory);
text = text.Replace("TurnOnHourPerDayInWorkDayTag", beReplacedText.TurnOnHourPerDayInWorkDay);
text = text.Replace("TurnOnHourPerDayInOffDayTag", beReplacedText.TurnOnHourPerDayInOffDay);
text = text.Replace("WorkDayPerYearTag", beReplacedText.WorkDayPerYear);
text = text.Replace("WattInOperationTag", beReplacedText.WattInOperation);
text = text.Replace("WattInLowOperationTag", beReplacedText.WattInLowOperation);
text = text.Replace("EnergyConsumptionTag", beReplacedText.EnergyConsumption);
text = text.Replace("UsageStageDescriptionTag", beReplacedText.UsageStageDescription);
text = text.Replace("PowerFactorTag", beReplacedText.PowerFactor);
text = text.Replace("AbandonedStageDescriptionTag", beReplacedText.AbandonedStageDescription);
text = text.Replace("MaterialProductionAndTransportPhaseTag", beReplacedText.MaterialProductionAndTransportPhase);
text = text.Replace("MaterialProductionAndTransportPhasePercentTag", beReplacedText.MaterialProductionAndTransportPhasePercent);
text = text.Replace("ProductionPhaseTag", beReplacedText.ProductionPhase);
text = text.Replace("ProductionPhasePercentTag", beReplacedText.ProductionPhasePercent);
text = text.Replace("DeliveryPhaseTag", beReplacedText.DeliveryPhase);
text = text.Replace("DeliveryPhasePercentTag", beReplacedText.DeliveryPhasePercent);
text = text.Replace("UsagePhaseTag", beReplacedText.UsagePhase);
text = text.Replace("UsagePhasePercentTag", beReplacedText.UsagePhasePercent);
text = text.Replace("AbandonedPhaseTag", beReplacedText.AbandonedPhase);
text = text.Replace("AbandonedPhasePercentTag", beReplacedText.AbandonedPhasePercent);
text = text.Replace("PowerYearTag", beReplacedText.PowerYear);
text = text.Replace("TotalCarbonEmissionTag", beReplacedText.AllPhaseCarbonEmission);
text = text.Replace("BiggestCarbonEmissionStageTag", beReplacedText.BiggestCarbonEmissionPhase);
text = text.Replace("BiggestCarbonEmissionPercentTag", beReplacedText.BiggestCarbonEmissionPercent);
text = text.Replace("SecondCarbonEmissionStageTag", beReplacedText.SecondCarbonEmissionPhase);
text = text.Replace("SecondCarbonEmissionPercentTag", beReplacedText.SecondCarbonEmissionPercent);
text = text.Replace("SecondDataTypeOfPartsKgCO2eTag", beReplacedText.SecondDataTypeOfPartsKgCO2e);
text = text.Replace("SecondDataTypeOfPartsPercentTag", beReplacedText.SecondDataTypeOfPartsPercent);
text = text.Replace("PrimaryDataTypeOfPartsKgCO2eTag", beReplacedText.PrimaryDataTypeOfPartsKgCO2e);
text = text.Replace("PrimaryDataTypeOfPartsPercentTag", beReplacedText.PrimaryDataTypeOfPartsPercent);
text = text.Replace("MaterialScopeOneDistributeKgCO2eTag", beReplacedText.MaterialScopeOneDistributeKgCO2e);
text = text.Replace("MaterialScopeTwoDistributeKgCO2eTag", beReplacedText.MaterialScopeTwoDistributeKgCO2e);
text = text.Replace("MaterialScopeThreeDistributeKgCO2eTag", beReplacedText.MaterialScopeThreeDistributeKgCO2e);
text = text.Replace("ProductLcaScopeOneTag", beReplacedText.ProductLcaScopeOne);
text = text.Replace("ProductLcaScopeTwoTag", beReplacedText.ProductLcaScopeTwo);
text = text.Replace("ProductLcaScopeThreeTag", beReplacedText.ProductLcaScopeThree);
text = text.Replace("ProductLcaWasteTransportDistributeByHourTag", beReplacedText.ProductLcaWasteTransportDistributeByHour);
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 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;
}
}
}