editing chart with c# inside word with excel embeddedusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO.Packaging;
using System.IO;
using System.Xml;
using Word = Microsoft.Office.Interop.Word;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Create the Word application and declare a document
            Word.Application word = new Word.Application();
            Word.Document doc = new Word.Document();
            
            // Define an object to pass to the API for missing parameters
            object oMissing = System.Type.Missing;
            try
            {
          
                object oTemplate = @"G:\document.docx";
                Package docPackage = Package.Open(oTemplate.ToString(), FileMode.Open, FileAccess.ReadWrite);
                PackagePart xlsPackagePart = docPackage.GetPart(new Uri("/word/embeddings/Hoja_de_c_lculo_de_Microsoft_Office_Excel1.xlsx", UriKind.Relative));
                Package xlsPackage = Package.Open(xlsPackagePart.GetStream(), FileMode.Open, FileAccess.ReadWrite);
                PackagePart hoja1 = xlsPackage.GetPart(new Uri("/xl/worksheets/sheet1.xml", UriKind.Relative));
                XmlDocument docxml = new XmlDocument();
                docxml.Load(hoja1.GetStream());
                XmlNamespaceManager nsManager = new XmlNamespaceManager(docxml.NameTable);
                nsManager.AddNamespace("d", docxml.DocumentElement.NamespaceURI);
                XmlNode cell = docxml.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c[@r='{0}']/d:v", "D9"),nsManager);
                cell.ChildNodes[0].Value = "4";
                
                cell = docxml.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c[@r='{0}']/d:v", "D10"), nsManager);
                cell.ChildNodes[0].Value = "5";
                
                
                docxml.Save(hoja1.GetStream(FileMode.Create, FileAccess.ReadWrite));
                PackagePart chart1 = docPackage.GetPart(new Uri("/word/charts/chart1.xml", UriKind.Relative));
                docxml.Load(chart1.GetStream());
                XmlNodeList nodos = docxml.GetElementsByTagName("c:numCache");
                nodos[0].ChildNodes[2].ChildNodes[0].ChildNodes[0].Value= "4";
                nodos[0].ChildNodes[3].ChildNodes[0].ChildNodes[0].Value = "5";
                docxml.Save(chart1.GetStream(FileMode.Create, FileAccess.ReadWrite));
                    
                xlsPackage.Close();
                docPackage.Close();
                
                
                
                doc = word.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                
                
                
                
                
                
               
                word.Visible = true;
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //doc.Close(ref missing, ref missing, ref missing);
                word.Application.Quit(ref oMissing, ref oMissing, ref oMissing);
               
            }
        }
    }
}
domingo, 19 de abril de 2009
Editar gráficas con c# dentro de word con excel embebido
Suscribirse a:
Comentarios (Atom)
 
