demo20230512/Controllers/TestController.cs

108 lines
4.2 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using Weee.Models;
using Weee.Supports;
using Weee.DataTransferObject;
using Weee.Service;
using Weee.DAL;
namespace Weee.Controllers
{
public class TestController : Controller
{
private WeeeLCADataService dataService;
public TestController(WeeeLCADataService dataService)
{
this.dataService = dataService;
}
[Route("Test")]
public ActionResult Index()
{
ProductLCA productLca = dataService.GetProductLCA(1);
// get tamplate file path
string sourceFileName = "CompanyCarbonFootprintReport.docx";
string sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", sourceFileName);
// generate target file path
string targetFileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".docx";
string targetFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", targetFileName);
// copy file
System.IO.File.Copy(sourceFilePath, targetFilePath);
// get new file
WordprocessingDocument wordDocument = WordprocessingDocument.Open(targetFilePath, true);
// prepare BeReplacedTextDTO and data
BeReplacedTextDTO beReplacedText = new BeReplacedTextDTO();
beReplacedText.ProductName = productLca.TargetProduct.Name;
beReplacedText.CompanyName = productLca.Owner.Name;
beReplacedText.FabName = productLca.TargetFab.Name;
beReplacedText.FabAddress = productLca.TargetFab.Address;
beReplacedText.PartNumber = productLca.TargetProduct.SerialNumber;
beReplacedText.ProductSpec = productLca.TargetProduct.SpecDescription;
var duringInventory = "";
if (productLca.StartDate != null && productLca.EndDate != null)
{
duringInventory = productLca.StartDate.ToShortDateString() + " ~ " + productLca.EndDate.ToShortDateString();
}
beReplacedText.DuringInventory = duringInventory;
beReplacedText.MaterialProductionAndTransportPhase = "Stage One";
beReplacedText.MaterialProductionAndTransportPhasePercent = "Stage One Percent";
beReplacedText.ProductionPhase = "Stage Two";
beReplacedText.ProductionPhasePercent = "Stage Two Percent";
beReplacedText.DeliveryPhase = "Stage Three";
beReplacedText.DeliveryPhasePercent = "Stage Three Percent";
beReplacedText.UsagePhase = "Stage Four";
beReplacedText.UsagePhasePercent = "Stage Four Percent";
beReplacedText.AbandonedPhase = "Stage Five";
beReplacedText.AbandonedPhasePercent = "Stage Five Percent";
beReplacedText.PowerYear = "Power Year";
beReplacedText.AllPhaseCarbonEmission = "Total Carbon Emission";
beReplacedText.BiggestCarbonEmissionPhase = "Biggest Carbon Emission";
beReplacedText.BiggestCarbonEmissionPercent = "Biggest Carbon Emission Percent";
beReplacedText.SecondCarbonEmissionPhase = "Second Carbon Emission";
beReplacedText.SecondCarbonEmissionPercent = "Second Carbon Emission Percent";
// replace text on file
ProductLCADocParser docParser = new ProductLCADocParser(wordDocument
, dataService.GetDbContext() );
docParser.ReplaceAllTextTagWithData(beReplacedText);
// insert image on file
// insert table on file
// save file into local
docParser.SaveDocument();
// upload file to azure
// assign azure url in to model
// delete file
// System.IO.File.Delete(targetFilePath);
return View("index");
}
}
}