private void button1_Click(object sender, EventArgs e) { PriceData.getPriceDataToDay("ACB"); if (File.Exists("template_output.docx")) { File.Delete("template_output.docx"); } File.Copy("template_input.docx", "template_output.docx"); //OpenXMLUtils.createDocument("t.docx"); //OpenXMLUtils.CreateTable("t.docx"); //OpenXMLUtils.replacePicture("template.docx"); using (WordprocessingDocument doc = WordprocessingDocument.Open("template_output.docx", true)) { SearchAndReplacer.SearchAndReplace(doc, "[date]", DateTime.Now.ToShortDateString(), true); SearchAndReplacer.SearchAndReplace(doc, "[Market Overview]", "Với một phiên tăng giá tiếp tục trên cả 2 sàn trong ngày 10/1, các tín hiệu xác nhận sự đảo chiều giảm điểm đã không xuất hiện, đà tăng giá vẫn chưa có dấu hiệu dừng lại. ", true); SearchAndReplacer.SearchAndReplace(doc, "[change1]", "+2.53%", true); SearchAndReplacer.SearchAndReplace(doc, "[change2]", "+1.54%", true); SearchAndReplacer.SearchAndReplace(doc, "[gtdc1]", "460.12", true); SearchAndReplacer.SearchAndReplace(doc, "[gtdc2]", "60.53", true); SearchAndReplacer.SearchAndReplace(doc, "[klgd1]", "79.394.580", true); SearchAndReplacer.SearchAndReplace(doc, "[klgd2]", "67.007.880", true); SearchAndReplacer.SearchAndReplace(doc, "[gtgd1]", "1.523,346", true); SearchAndReplacer.SearchAndReplace(doc, "[gtgd2]", "470,871", true); // Dispose the previous instance. if (documentManipulator != null) { documentManipulator.Dispose(); } documentManipulator = new WordDocumentImageManipulator(doc); int i = 1; foreach (var blip in documentManipulator.GetAllImages()) { if (i == 1) { documentManipulator.ReplaceImage( blip, new FileInfo("lineChart.jpg")); i++; continue; } documentManipulator.ReplaceImage( blip, new FileInfo("ck" + (i-1) + ".png")); i++; } string tblTag = "StockTableTag"; MainDocumentPart mainPart = doc.MainDocumentPart; //StockService.baseDS.stockCodeDataTable tbl = Gateway.PriceData.getPriceDataToDay(); SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == tblTag).Single(); // This should return only one table. Table theTable = ccWithTable.Descendants<Table>().Single(); // Get the last row in the table. TableRow theRow = theTable.Elements<TableRow>().Last(); foreach (var item in lstStockCodes) { try { StockService.baseDS.priceDataRow row = Gateway.PriceData.getPriceDataToDay(item); TableRow rowCopy = (TableRow)theRow.CloneNode(true); rowCopy.Descendants<TableCell>().ElementAt(0).Append(new Paragraph (new Run(new Text(row.stockCode.ToString())))); rowCopy.Descendants<TableCell>().ElementAt(1).Append(new Paragraph (new Run(new Text(row.openPrice.ToString())))); rowCopy.Descendants<TableCell>().ElementAt(2).Append(new Paragraph (new Run(new Text(row.closePrice.ToString())))); rowCopy.Descendants<TableCell>().ElementAt(3).Append(new Paragraph (new Run(new Text(row.volume.ToString())))); theTable.AppendChild(rowCopy); } catch (Exception ex) { continue; } } // Remove the empty placeholder row from the table. theTable.RemoveChild(theRow); // Save the changes to the table back into the document. mainPart.Document.Save(); } MessageBox.Show("Input->template_input.docx and Ouput->template_output.docx. Success!!! The program will be closed!!!"); this.Close(); }
private void button1_Click(object sender, EventArgs e) { PriceData.getPriceDataToDay("ACB"); if (File.Exists("template_output.docx")) { File.Delete("template_output.docx"); } File.Copy("template_input.docx", "template_output.docx"); //OpenXMLUtils.createDocument("t.docx"); //OpenXMLUtils.CreateTable("t.docx"); //OpenXMLUtils.replacePicture("template.docx"); using (WordprocessingDocument doc = WordprocessingDocument.Open("template_output.docx", true)) { SearchAndReplacer.SearchAndReplace(doc, "[date]", DateTime.Now.ToShortDateString(), true); SearchAndReplacer.SearchAndReplace(doc, "[Market Overview]", "Với một phiên tăng giá tiếp tục trên cả 2 sàn trong ngày 10/1, các tín hiệu xác nhận sự đảo chiều giảm điểm đã không xuất hiện, đà tăng giá vẫn chưa có dấu hiệu dừng lại. ", true); SearchAndReplacer.SearchAndReplace(doc, "[change1]", "+2.53%", true); SearchAndReplacer.SearchAndReplace(doc, "[change2]", "+1.54%", true); SearchAndReplacer.SearchAndReplace(doc, "[gtdc1]", "460.12", true); SearchAndReplacer.SearchAndReplace(doc, "[gtdc2]", "60.53", true); SearchAndReplacer.SearchAndReplace(doc, "[klgd1]", "79.394.580", true); SearchAndReplacer.SearchAndReplace(doc, "[klgd2]", "67.007.880", true); SearchAndReplacer.SearchAndReplace(doc, "[gtgd1]", "1.523,346", true); SearchAndReplacer.SearchAndReplace(doc, "[gtgd2]", "470,871", true); // Dispose the previous instance. if (documentManipulator != null) { documentManipulator.Dispose(); } documentManipulator = new WordDocumentImageManipulator(doc); int i = 1; foreach (var blip in documentManipulator.GetAllImages()) { if (i == 1) { documentManipulator.ReplaceImage( blip, new FileInfo("lineChart.jpg")); i++; continue; } documentManipulator.ReplaceImage( blip, new FileInfo("ck" + (i - 1) + ".png")); i++; } string tblTag = "StockTableTag"; MainDocumentPart mainPart = doc.MainDocumentPart; //StockService.baseDS.stockCodeDataTable tbl = Gateway.PriceData.getPriceDataToDay(); SdtBlock ccWithTable = mainPart.Document.Body.Descendants <SdtBlock>().Where (r => r.SdtProperties.GetFirstChild <Tag>().Val == tblTag).Single(); // This should return only one table. Table theTable = ccWithTable.Descendants <Table>().Single(); // Get the last row in the table. TableRow theRow = theTable.Elements <TableRow>().Last(); foreach (var item in lstStockCodes) { try { StockService.baseDS.priceDataRow row = Gateway.PriceData.getPriceDataToDay(item); TableRow rowCopy = (TableRow)theRow.CloneNode(true); rowCopy.Descendants <TableCell>().ElementAt(0).Append(new Paragraph (new Run(new Text(row.stockCode.ToString())))); rowCopy.Descendants <TableCell>().ElementAt(1).Append(new Paragraph (new Run(new Text(row.openPrice.ToString())))); rowCopy.Descendants <TableCell>().ElementAt(2).Append(new Paragraph (new Run(new Text(row.closePrice.ToString())))); rowCopy.Descendants <TableCell>().ElementAt(3).Append(new Paragraph (new Run(new Text(row.volume.ToString())))); theTable.AppendChild(rowCopy); } catch (Exception ex) { continue; } } // Remove the empty placeholder row from the table. theTable.RemoveChild(theRow); // Save the changes to the table back into the document. mainPart.Document.Save(); } MessageBox.Show("Input->template_input.docx and Ouput->template_output.docx. Success!!! The program will be closed!!!"); this.Close(); }