public bool SaveExportInfo(Workbook workbook) { //clear old export item information xml foreach (CustomXMLPart curXmlPart in workbook.CustomXMLParts) { if (curXmlPart.BuiltIn) { continue; } XmlObject customObject = ObjectSerializeHelper.Deserialize <XmlObject>(curXmlPart.XML); if (customObject.ContentType == ContentType.PdeExportItem) { curXmlPart.Delete(); break; } } //add new information CustomXMLPart xmlPart = workbook.CustomXMLParts.Add(); string xmlContent = ObjectSerializeHelper.SerializeToString <PdeExports>(new PdeExports(exportItems)); XmlObject excelContent = new XmlObject(xmlContent, ContentType.PdeExportItem); xmlPart.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(excelContent)); return(true); }
public static void SaveExcelToDoc(docx.Document doc, string ExcelFile) { CustomXMLPart xmlPart = doc.CustomXMLParts.Add(); //XmlObject excelContent = new XmlObject(ExcelToBase64(ExcelFile)); string xmlContent = ExcelToBase64(ExcelFile); xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><content>" + xmlContent + "</content>"; xmlPart.LoadXML(xmlContent); }
public bool SaveMapInfo(Workbook workbook) { //clear old map information xml foreach (CustomXMLPart curXmlPart in workbook.CustomXMLParts) { if (curXmlPart.BuiltIn) { continue; } XmlObject customObject = ObjectSerializeHelper.Deserialize <XmlObject>(curXmlPart.XML); if (customObject.ContentType == ContentType.PdeMapInfo) { curXmlPart.Delete(); break; } } //add new map information CustomXMLPart xmlPart = workbook.CustomXMLParts.Add(); string xmlContent = ObjectSerializeHelper.SerializeToString <MapInfo>(mapInfo); XmlObject excelContent = new XmlObject(xmlContent, ContentType.PdeMapInfo); xmlPart.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(excelContent)); //Generate new xsd file and import it. GenXsd genXsd = new GenXsd(); prepareData(genXsd); XmlMap xmap = genXsd.ImportXsd(workbook); //add xpath for each mapped cell and table foreach (MapNode mn in mapInfo.Maps) { if (mn.type == MapType.SingleCell) { Range c = workbook.Application.get_Range(mn.target); c.XPath.SetValue(xmap, mn.xPath); } else { string[] target = mn.target.Split('!'); Worksheet worksheet = workbook.Sheets[target[0]]; ListObject lst = worksheet.ListObjects[target[1]]; foreach (TabCol tc in mn.columns) { lst.ListColumns[tc.columnName].XPath.SetValue(xmap, tc.xPath); } } } return(true); }
public void LoadXML_VadliXmlValue_LoadsIt() { // Arrange using (var workbook = this.ExcelApplication.Workbooks.Add()) { CustomXMLPart cxp1 = workbook.CustomXMLParts.Add(); // Act cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>"); // Assert Assert.AreEqual("<discounts><discount>0.10</discount></discounts>", cxp1.XML); } }
public void Delete_ValidXmlPart_RemovesIt() { // Arrange using (var workbook = this.ExcelApplication.Workbooks.Add()) { CustomXMLPart cxp1 = workbook.CustomXMLParts.Add(); cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>"); // Act cxp1.Delete(); // Assert Assert.Pass(); } }
private static CustomXMLPart InsertCustomXml(Document doc, string xml, string propertyName) { CustomXMLPart xmlpart = GetCustomXmlPart(doc, propertyName); bool result = xmlpart.LoadXML(xml); if (result) { DocumentProperties propertySet = doc.CustomDocumentProperties; propertySet.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, xmlpart.Id); } else { //TODO:FAILED PROCESS } return(xmlpart); }
/// <summary> /// Add custom xml part into active document /// </summary> /// <param name="customXmlPartContent"></param> /// <param name="partId"></param> /// <returns></returns> private string AddCustomXmlPart(string customXmlPartContent, string partId) { string id = string.Empty; try { ProtectLevel protectLevel = CurrentTemplateInfo.ProtectLevel; if (protectLevel == ProtectLevel.Bookmark) { UnprotectBookmark(); } if (protectLevel == ProtectLevel.Document) { UnprotectDocument(); } if (!string.IsNullOrEmpty(partId)) { CustomXMLPart oldXmlPart = CommonProfile.ActiveDoc.CustomXMLParts.SelectByID(partId); if (oldXmlPart != null) { oldXmlPart.Delete(); } } CustomXMLPart xmlPart = CommonProfile.ActiveDoc.CustomXMLParts.Add(); xmlPart.LoadXML(customXmlPartContent); id = xmlPart.Id; if (protectLevel == ProtectLevel.Bookmark) { ProtectBookmark(); } if (protectLevel == ProtectLevel.Document) { ProtectDocument(); } } catch { } return(id); }
private void button12_Click(object sender, EventArgs e) { word.Application app = createWord(); String wordFile = textBox10.Text; string xsltFile = textBox9.Text; word.Document doc = app.Documents.Open(wordFile); StreamReader sr = new StreamReader(xsltFile); string xsl = sr.ReadToEnd(); //delete old xsl file foreach (CustomXMLPart xmlPart in doc.CustomXMLParts) { if (xmlPart.BuiltIn) { continue; } XmlObject customObject = ObjectSerializeHelper.Deserialize <XmlObject>(xmlPart.XML); if (customObject.ContentType == ContentType.PdwrXsl) { xmlPart.Delete(); break; } } //add xsl file XmlObject xslObj = new XmlObject(); xslObj.ContentType = ContentType.PdwrXsl; xslObj.Content = xsl; CustomXMLPart xmlpart_xsl = doc.CustomXMLParts.Add(); xmlpart_xsl.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(xslObj)); sr.Close(); doc.Save(); enablePdw(app); app.Quit(); }
private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = true; Workbook workbook = app.Workbooks.Open(textBox1.Text); CustomXMLPart xmlPart = workbook.CustomXMLParts.Add(); //读取xml文件 XmlDocument data = new XmlDocument(); data.Load(textBox8.Text); XmlObject excelContent = new XmlObject(data.OuterXml, ContentType.PderXml); xmlPart.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(excelContent)); //string id = xmlPart.Id; //textBox1.Text = id; workbook.SaveAs(textBox1.Text + "r"); workbook.Close(); app.Quit(); MessageBox.Show("pder ok."); }
private void button8_Click(object sender, EventArgs e) { word.Application app = new word.Application(); app.Visible = true; Thread.Sleep(2000); //disable the pdw plugin COMAddIn pdwAddin = null; COMAddIns addins = app.COMAddIns; foreach (COMAddIn addin in addins) { if (addin.Description.Equals("Pdw")) { addin.Connect = false; pdwAddin = addin; break; } } Thread.Sleep(1000); string tempFile = textBox_pdw.Text; string wordFile = tempFile.Split('.')[0] + ".docx"; File.Copy(tempFile, wordFile, true); word.Document doc = app.Documents.Open(wordFile); //change properties of document //foreach (DocumentProperty docPro in doc.CustomDocumentProperties) //{ // if (docPro.Name.StartsWith("Pronto_ImportedExcelFileName")) // { // docPro.Value = textBox_pdwpder.Text; // } //} //remove pde template and add pder file. PdeContent pdeImported = null; string xsl = null; foreach (CustomXMLPart xmlPart in doc.CustomXMLParts) { if (xmlPart.BuiltIn) { continue; } XmlObject customObject = ObjectSerializeHelper.Deserialize <XmlObject>(xmlPart.XML); if (customObject.ContentType == ContentType.ImportedPde) { pdeImported = ObjectSerializeHelper.Deserialize <PdeContent>(customObject.Content); } else if (customObject.ContentType == ContentType.Xslt) { xsl = customObject.Content; } xmlPart.Delete(); } //add pder file CustomXMLPart xmlPart_pder = doc.CustomXMLParts.Add(); pdeImported.FileContent = FileHelper.ExcelToBase64(textBox_pdwpder.Text); //pdeImported.FullFileName = textBox_pdwpder.Text; XmlObject pderObj = new XmlObject(); pderObj.ContentType = ContentType.ImportedPde; pderObj.Content = ObjectSerializeHelper.SerializeToString <PdeContent>(pdeImported); xmlPart_pder.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(pderObj)); //add xml data file XmlDocument dataXml = new XmlDocument(); dataXml.Load(textBox_pdwd.Text); XmlObject pdwrXml = new XmlObject(dataXml.InnerXml, ContentType.PdwrXml); CustomXMLPart xmlPart_xml = doc.CustomXMLParts.Add(); xmlPart_xml.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(pdwrXml)); //add xsl file XmlObject xslObj = new XmlObject(); xslObj.ContentType = ContentType.PdwrXsl; xslObj.Content = xsl; CustomXMLPart xmlpart_xsl = doc.CustomXMLParts.Add(); xmlpart_xsl.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(xslObj)); //add pdwr setting XmlObject settObj = new XmlObject(); settObj.ContentType = ContentType.PdwrSettings; settObj.Content = "<?xml version=\"1.0\" encoding=\"utf-16\"?><RenderSettings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Media>Docx</Media><Channel>Display</Channel></RenderSettings>"; CustomXMLPart xmlpart_sett = doc.CustomXMLParts.Add(); xmlpart_sett.LoadXML(ObjectSerializeHelper.SerializeToString <XmlObject>(settObj)); doc.Save(); doc.Close(); pdwAddin.Connect = true; app.Quit(); File.Copy(wordFile, wordFile.Split('.')[0] + ".pdwr", true); MessageBox.Show("Create pdwr file success."); }