public static void GenerateReceptionRegister(List<sp_Get_RegistruRegeptie_Result> lines, string fileName, DateTime reportDate) { using (var output = File.Open(fileName, FileMode.Create)) { using (var document = DocX.Create(output)) { Paragraph title = document.InsertParagraph(); title.AppendLine("REGISTRU DE RECEPŢIE A PROBELOR DE LAPTE").FontSize(15).Bold().Alignment = Alignment.center; title = document.InsertParagraph(); title.AppendLine("Data:" + reportDate.ToString("dd.MM.yyyy") + " ").FontSize(15).Bold().Alignment = Alignment.right; var t = document.AddTable(lines.Count + 2, 5); //Set header t.Rows[0].Cells[0].Paragraphs.First().Append("Seria PV/Data").Bold().Alignment = Alignment.center; t.Rows[0].Cells[1].Paragraphs.First().Append("Nume/Cod Prelevator").Bold().Alignment = Alignment.center; t.Rows[0].Cells[2].Paragraphs.First().Append("Nume proba").Bold().Alignment = Alignment.center; t.Rows[0].Cells[3].Paragraphs.First().Append("Cod identificare probă").Bold().Alignment = Alignment.center; t.Rows[0].Cells[4].Paragraphs.First().Append("Tipul de analiză").Bold().Alignment = Alignment.center; t.Rows[1].Cells[0].Paragraphs.First().Append("Cerere analiză").Bold().Alignment = Alignment.center; t.Rows[1].Cells[4].Paragraphs.First().Append("a),b),c),d),e)*").Bold().Alignment = Alignment.center; int start = 2; int end = 2; //Add content to this Table. foreach (var commandNrGroup in lines.GroupBy(x => x.NrComanda).ToList()) { start = end; end = start + commandNrGroup.Count(); t.Rows[start].Cells[0].VerticalAlignment = VerticalAlignment.Center; t.Rows[start].Cells[0].Paragraphs.First().Append(commandNrGroup.Key).Alignment = Alignment.center; if (commandNrGroup.Count() > 1) t.MergeCellsInColumn(0, start, end - 1); foreach (var codGroup in commandNrGroup.GroupBy(x => x.NumeCodPrelevator)) { t.Rows[start].Cells[1].VerticalAlignment = VerticalAlignment.Center; t.Rows[start].Cells[1].Paragraphs.First().Append(codGroup.Key).Alignment = Alignment.center; if (codGroup.Count() > 1) t.MergeCellsInColumn(1, start, start + codGroup.Count() - 1); foreach (var probeGroup in codGroup.GroupBy(x => x.NumeProba)) { t.Rows[start].Cells[2].VerticalAlignment = VerticalAlignment.Center; t.Rows[start].Cells[2].Paragraphs.First().Append(probeGroup.Key).Alignment = Alignment.center; if (probeGroup.Count() > 1) t.MergeCellsInColumn(2, start, start + probeGroup.Count() - 1); foreach (var elem in probeGroup) { t.Rows[start].Cells[3].Paragraphs.First().Append(elem.CodProba).Alignment = Alignment.center; t.Rows[start].Cells[4].Paragraphs.First().Append(elem.TipAnaliza).Alignment = Alignment.center; start++; } } } } // Insert the Table into the document. t.Alignment = Alignment.center; t.Design = TableDesign.TableGrid; document.InsertTable(t); document.InsertParagraph().AppendLine(@" a) determinarea numărului total de germeni, b) determinarea numărului de celule somatice, c) determinare proprietăţi fizico-chimice, d) determinarea inhibitorilor, e) determinarea punctului de congelare. În urma verificării calitative şi cantitative efectuate la produsul de încercat/analizat, s-au constatat următoarele: Este acceptat un număr de ________ probe. Un număr de _______ probe nu corespund din punct de vedere cantitativ / calitativ. După încercare produsul este eliminat ca deşeu.").FontSize(12).Alignment = Alignment.left; document.InsertParagraph().Append("Semnătura de primire\n").Alignment = Alignment.right; document.InsertParagraph().Append("Cod: FG – L – 12 – 02 Ediţia: 1/11.05.2015 Revizia: 1/11.05.2015").FontSize(8).Alignment = Alignment.left; document.Save(); } } }
//public ActionResult PrintOrder(string codDocument) //{ // //carico l'ordine // Order order = documentRepository.GetAll().OfType<Order>().FirstOrDefault(x => x.CodDocument == codDocument); // //cliente dell'ordine // Customer cust = (Customer)customerSupplierRepository.GetSingle(order.CodCustomer); // order.CustomerSupplier = cust; // //il suo prodotto con la sua quantità // order.OrderProduct = documentRepository.GetDocumentProductByCodDocumentProduct(order.CodDocumentProduct); // //estimate // order.OrderProduct.Document = (Estimate)documentRepository.GetSingle(order.OrderProduct.CodDocument); // string fileNameMain = Path.Combine(Server.MapPath(@"~/Report"), "OrderHead.docx"); // string fileNameCost = Path.Combine(Server.MapPath(@"~/Report"), "Cost.docx"); // string fileNameSaveAs = Path.Combine(Server.MapPath(@"~/Report"), order.OrderNumberSerie + "-" + order.OrderNumber + ".docx"); // string fileNameSaveAsAfterRepair = Path.Combine(Server.MapPath(@"~/Report"), order.OrderNumberSerie + "-" + order.OrderNumber + "AfterRepair.docx"); // // Store a global reference to the executing assembly. // g_assembly = Assembly.GetExecutingAssembly(); // // Create the document in memory: // var docMain = DocX.Load(fileNameMain); // order.MergeField(docMain); // order.OrderProduct.MergeField(docMain); // order.OrderProduct.Product.MergeField(docMain); // var product = productRepository.GetSingle(order.OrderProduct.Product.CodProduct); // product.ProductParts.FirstOrDefault().MergeField(docMain); // // product.ProductParts.FirstOrDefault().ProductPartPrintableArticles.FirstOrDefault().MergeField(docMain); // foreach (var cost in order.OrderProduct.Costs) // { // // Create the document in memory: // //var docCost = DocX.Load(fileNameCost); // // cost.MergeField(docCost); // //try // //{ // var cd = cost.CostDetails.FirstOrDefault(); // if (cd != null) // { // cd = costDetailRepository.GetSingle(cd.CodCostDetail); // cd.InitCostDetail(taskExecutorRepository.GetAll(), articleRepository.GetAll()); // if (cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintedRollArticleCostDetail" || // cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintingZRollCostDetail" || // cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "ControlTableCostDetail") // { // var docPrintCD = DocX.Load(Path.Combine(Server.MapPath(@"~/Report"), cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() + ".docx")); // cd.MergeField(docPrintCD); // cd.TaskCost.MergeField(docPrintCD); // //il merge con il cost // try // { // cd.TaskCost.ProductPartTask.ProductPart.MergeField(docPrintCD); // } // catch // { } // docMain.InsertDocument(docPrintCD); // docPrintCD.Dispose(); // } // else // { // } // // docMain.InsertDocument(docCost); // } // //} // //catch // //{ } // // docCost.Dispose(); // } // docMain.SaveAs(fileNameSaveAs); // docMain.Dispose(); // // Open a doc file. // Application application = new Application(); // Microsoft.Office.Interop.Word.Document document = application.Documents.Open(fileNameSaveAs, OpenAndRepair: true); // Object fnsa = (Object)fileNameSaveAsAfterRepair; // // Object of Missing "Null Value". // Object oMissing = System.Reflection.Missing.Value; // // Object of false. // Object oFalse = false; // // Save the document. // document.SaveAs // ( // ref fnsa, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing // ); // Close word. // document.Close(); // application.Quit(); // return File(fileNameSaveAsAfterRepair, "application/file", "stampa.docx"); //} public ActionResult PrintOrder(string codDocument, string reportName) { int id; var dbName = reportName.Replace("LabelRollHead", ""); //carico l'ordine Order order = documentRepository.GetAll().OfType<Order>().FirstOrDefault(x => x.CodDocument == codDocument); //cliente dell'ordine Customer cust = (Customer)customerSupplierRepository.GetSingle(order.CodCustomer); order.CustomerSupplier = cust; //il suo prodotto con la sua quantità order.OrderProduct = documentRepository.GetDocumentProductByCodDocumentProduct(order.CodDocumentProduct); //estimate order.OrderProduct.Document = documentRepository.GetSingle(order.OrderProduct.CodDocument); string fileNameMain = Path.Combine(Server.MapPath(@"~/Report"), (reportName == "" || reportName == null) ? "OrderHead.docx" : (reportName + ".docx")); string fileNameCost = Path.Combine(Server.MapPath(@"~/Report"), "Cost" + dbName + ".docx"); if (!System.IO.File.Exists(fileNameCost)) { fileNameCost = Path.Combine(Server.MapPath(@"~/Report"), "Cost" + ".docx"); } string retName = order.OrderNumberSerie + "-" + order.OrderNumber; retName = retName.PurgeFileName(); string fileNameSaveAs = Path.Combine(Server.MapPath(@"~/Report"), retName + dbName + ".docx"); string fileNameSaveAsAfterRepair = fileNameSaveAs;// Path.Combine(Server.MapPath(@"~/Report"), retName + "AfterRepair.docx"); // Store a global reference to the executing assembly. g_assembly = Assembly.GetExecutingAssembly(); //// Create the document in memory: var docMain = DocX.Load(fileNameMain); //questo array mi serve per il merge Queue<string> files = new Queue<string>(); //questo array mi serve per il merge Queue<string> filesExtCost = new Queue<string>(); //questo array mi serve per il merge Queue<string> RepassCost = new Queue<string>(); //questo array mi serve per il merge Queue<string> filesDelivery = new Queue<string>(); order.MergeField(docMain); order.OrderProduct.MergeField(docMain); order.OrderProduct.Product.MergeField(docMain); var product = productRepository.GetSingle(order.OrderProduct.Product.CodProduct); product.ProductParts.FirstOrDefault().MergeField(docMain); try { product.ProductParts.FirstOrDefault().productpartprintings.FirstOrDefault().MergeField(docMain); } catch (Exception) { } // product.ProductParts.FirstOrDefault().ProductPartPrintableArticles.FirstOrDefault().MergeField(docMain); var costs = order.OrderProduct.Costs.OrderBy(x => x.IndexOf); Console.WriteLine(costs); //group costdetails by taskexecutor List<CostDetail> costDetails = new List<CostDetail>(); foreach (var item in costs) { costDetails.AddRange(item.CostDetails); } var TaskExecutorGroups = costDetails.GroupBy(p => p.CodTaskExecutorSelected, p => p, (key, g) => new CostDetailGrouped { CodTaskExecutorSelected = key, CostDetails = g.ToList() }); Console.WriteLine(TaskExecutorGroups); //end grouping foreach (var cost in costs) { // Create the document in memory: //var docCost = DocX.Load(fileNameCost); // cost.MergeField(docCost); //try //{ var cd = cost.CostDetails.FirstOrDefault(); if (cd != null) { cd = costDetailRepository.GetSingle(cd.CodCostDetail); cd.InitCostDetail(taskExecutorRepository.GetAll(), articleRepository.GetAll()); if (cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintedRollArticleCostDetail" || cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintingZRollCostDetail" || cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintedSheetArticleCostDetail" || cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "PrintingSheetCostDetail" || cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "ControlTableCostDetail") { #region SingleCostDetail var cDFileName = Path.Combine(Server.MapPath(@"~/Report"), cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() + dbName + ".docx"); if (!System.IO.File.Exists(cDFileName)) { cDFileName = Path.Combine(Server.MapPath(@"~/Report"), cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() + ".docx"); } var docPrintCD = DocX.Load(cDFileName); cd.MergeField(docPrintCD); cd.TaskCost.MergeField(docPrintCD); cd.MergeField(docMain); cd.TaskCost.MergeField(docMain); try { cd.TaskCost.ProductPartTask.ProductPart.MergeField(docPrintCD); cd.TaskCost.ProductPartTask.ProductPart.MergeField(docMain); } catch { } docPrintCD.SaveAs(Path.Combine(Server.MapPath(@"~/Report"), "Cost" + cost.CodCost + ".docx")); files.Enqueue(Path.Combine(Server.MapPath(@"~/Report"), "Cost" + cost.CodCost + ".docx")); #region Merge con Docx (vecchio metodo) //var xxx = DocX.Load(Path.Combine(Server.MapPath(@"~/Report"), "Cost" + cost.CodCost + ".docx")); //docMain.InsertDocument(xxx); //docPrintCD.Dispose(); //xxx.Dispose(); #endregion #endregion } else { if (!cost.CostDetails.FirstOrDefault().JustPrintedInOrder) { if (cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString() == "RepassRollCostDetail") { //il ripasso potrebbe includere diverse lavorazioni!!! //ripassi con macchine uguali ---> unisco e salvo #region RepassRollCostDetail //apro l'header dei costi supplementari e lo salvo var nomeRepass = Path.Combine(Server.MapPath(@"~/Report"), "RepassCostHeader" + order.CodDocument + ".docx"); var docRepassHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCostHeader" + dbName + ".docx"); if (!System.IO.File.Exists(docRepassHeaderFile)) { docRepassHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCostHeader.docx"); } var docRepassHeader = DocX.Load(docRepassHeaderFile); //estraggo per macchina var relatedCostDetails = TaskExecutorGroups.Where(x => x.CodTaskExecutorSelected == cost.CostDetails.FirstOrDefault().CodTaskExecutorSelected).FirstOrDefault().CostDetails; docRepassHeader.SaveAs(nomeRepass); files.Enqueue(nomeRepass); foreach (var i in relatedCostDetails) { var docPrePressFile = Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCostDetail" + dbName + ".docx"); if (!System.IO.File.Exists(docPrePressFile)) { docPrePressFile = Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCostDetail.docx"); } var docPrePress = DocX.Load(docPrePressFile); var cv = costDetailRepository.GetSingle(i.CodCost); var print = true; if (cv.TaskCost.ProductPartTask != null) { print = !cv.TaskCost.ProductPartTask.OptionTypeOfTask.CodOptionTypeOfTask.Contains("_NO"); } if (cv.TaskCost.ProductTask != null) { print = !cv.TaskCost.ProductTask.OptionTypeOfTask.CodOptionTypeOfTask.Contains("_NO"); } if (print) { cv.MergeField(docPrePress); docPrePress.SaveAs(Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCost" + i.CodCost + ".docx")); RepassCost.Enqueue(Path.Combine(Server.MapPath(@"~/Report"), "RepassRollCost" + i.CodCost + ".docx")); } i.JustPrintedInOrder = true; } id = 0; foreach (var file in RepassCost.Reverse()) { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(nomeRepass, true)) { var altChunkId = "AltChunkId" + id++; Console.WriteLine(altChunkId); var mainPart = myDoc.MainDocumentPart; var chunk = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML, altChunkId); using (System.IO.FileStream fileStream = System.IO.File.Open(file, System.IO.FileMode.Open)) { chunk.FeedData(fileStream); } var altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk(); altChunk.Id = altChunkId; var last = mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last(); mainPart.Document.Body.InsertAfter(altChunk, last); mainPart.Document.Save(); } } #endregion } } var res = cost.CostDetails.FirstOrDefault().TypeOfCostDetail.ToString(); Console.WriteLine(res); } // docMain.InsertDocument(docCost); } } #region costi supplementari //apro l'header dei costi supplementari e lo salvo var nomeExt = Path.Combine(Server.MapPath(@"~/Report"), "ExtCostHeader" + order.CodDocument + ".docx"); var docECHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "ExternalCostHeader" + dbName + ".docx"); if (!System.IO.File.Exists(docECHeaderFile)) { docECHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "ExternalCostHeader.docx"); } var docECHeader = DocX.Load(docECHeaderFile); docECHeader.SaveAs(nomeExt); files.Enqueue(nomeExt); //0=incluso, 1=Aux, 2=escluso var extCost = costs.Where(x => x.TypeOfCalcolous == 1 && (x.Quantity ?? 0) != 0); //prestampa foreach (var cost in extCost) { var docPrePressFile = Path.Combine(Server.MapPath(@"~/Report"), "ExternalCost" + dbName + ".docx"); if (!System.IO.File.Exists(docPrePressFile)) { docPrePressFile = Path.Combine(Server.MapPath(@"~/Report"), "ExternalCost.docx"); } var docPrePress = DocX.Load(docPrePressFile); cost.MergeField(docPrePress); docPrePress.SaveAs(Path.Combine(Server.MapPath(@"~/Report"), "ExtCost" + cost.CodCost + ".docx")); filesExtCost.Enqueue(Path.Combine(Server.MapPath(@"~/Report"), "ExtCost" + cost.CodCost + ".docx")); } id = 0; foreach (var file in filesExtCost.Reverse()) { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(nomeExt, true)) { var altChunkId = "AltChunkId" + id++; Console.WriteLine(altChunkId); var mainPart = myDoc.MainDocumentPart; var chunk = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML, altChunkId); using (System.IO.FileStream fileStream = System.IO.File.Open(file, System.IO.FileMode.Open)) { chunk.FeedData(fileStream); } var altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk(); altChunk.Id = altChunkId; var last = mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last(); mainPart.Document.Body.InsertAfter(altChunk, last); mainPart.Document.Save(); } } #endregion #region consegne //apro l'header dei costi supplementari e lo salvo var nomeDelivery = Path.Combine(Server.MapPath(@"~/Report"), "DeliveryHeader" + order.CodDocument + ".docx"); var docDHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "DeliveryHeader" + dbName + ".docx"); if (!System.IO.File.Exists(docDHeaderFile)) { docDHeaderFile = Path.Combine(Server.MapPath(@"~/Report"), "DeliveryHeader.docx"); } var docDHeader = DocX.Load(docDHeaderFile); docDHeader.SaveAs(nomeDelivery); files.Enqueue(nomeDelivery); #endregion #region Immagine //using (MemoryStream ms = new MemoryStream()) //{ // System.Drawing.Image myImg = System.Drawing.Image.FromFile(Path.Combine(Server.MapPath(@"~/Report"), "montaggio.png")); // myImg.Save(ms, myImg.RawFormat); // Save your picture in a memory stream. // ms.Seek(0, SeekOrigin.Begin); // Novacode.Image img = docMain.AddImage(ms); // Create image. // // Insert an emptyParagraph into this document. // Novacode.Paragraph p = docMain.InsertParagraph("", false); // Picture pic1 = img.CreatePicture(); // Create picture. // // pic1.SetPictureShape(BasicShapes.cube); // Set picture shape (if needed) // p.InsertPicture(pic1, 0); // Insert picture into paragraph. //} #endregion docMain.SaveAs(fileNameSaveAs); docMain.Dispose(); id = 0; foreach (var file in files.Reverse()) { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(fileNameSaveAs, true)) { var altChunkId = "AltChunkId" + id++; Console.WriteLine(altChunkId); var mainPart = myDoc.MainDocumentPart; var chunk = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML, altChunkId); using (System.IO.FileStream fileStream = System.IO.File.Open(file, System.IO.FileMode.Open)) { chunk.FeedData(fileStream); } var altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk(); altChunk.Id = altChunkId; var last = mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last(); mainPart.Document.Body.InsertAfter(altChunk, last); mainPart.Document.Save(); } } //// Open a doc file. //Application application = new Application(); //Microsoft.Office.Interop.Word.Document document = application.Documents.Open(fileNameSaveAs, OpenAndRepair: true); //Object fnsa = (Object)fileNameSaveAsAfterRepair; //// Object of Missing "Null Value". //Object oMissing = System.Reflection.Missing.Value; //// Object of false. //Object oFalse = false; //// Save the document. //document.SaveAs //( // ref fnsa, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing //); // Close word. //document.Close(); //application.Quit(); return File(fileNameSaveAsAfterRepair, "application/file", retName + ".docx"); }