Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Feb 21, 2018 10:46 am

Hi Team,

I have word document where I have embedded an excel sheet into it for further calculation. How do I access that excel sheet through this dll.

Khandu4
 
Posts: 1
Joined: Wed Feb 21, 2018 10:24 am

Thu Feb 22, 2018 6:15 am

Hello,

Thanks for your inquiry. it can't achieve this only with Spire.Xls, it also needs the Spire.Doc dll, so you need to download the Spire.Office package and reference the corresponding dlls. Here are some codes for your reference.
Code: Select all
      public void UpdateOle(string fileName)
        {
            Document doc = new Document();
            doc.LoadFromFile(fileName);
            Section sec = doc.Sections[0];
            MemoryStream xls = new MemoryStream();
            //Traverse through all Child Objects in the body of each section
            foreach (DocumentObject obj in sec.Body.ChildObjects)
            {
                if (obj is Paragraph)
                {
                    Paragraph par = obj as Paragraph;
                    //Traverse through all Child Objects in Paragraph
                    foreach (DocumentObject o in par.ChildObjects)
                    {
                        //Find the Ole Objects and Change the link
                        if (o.DocumentObjectType == DocumentObjectType.OleObject)
                        {
                            DocOleObject Ole = o as DocOleObject;
                            GetObjectStream(Ole, xls);
                            DocPicture pic = new DocPicture(doc);
                            pic.LoadImage(SaveTXlsImage(xls));
                            pic.Width = Ole.OlePicture.Width;
                            pic.Height = Ole.OlePicture.Height;
                            Ole.SetOlePicture(pic);
                            Ole.SetNativeData(xls.ToArray());

                        }
                    }
                }
            }
            doc.SaveToFile("OleObject2.docx", Spire.Doc.FileFormat.Docx);
            xls.Close();
        }
        public static void GetObjectStream(DocOleObject obj, MemoryStream xls)
        {
            using (MemoryStream ms = new MemoryStream(obj.NativeData))
            {
                Workbook workbook1 = new Workbook();
                workbook1.LoadFromStream(ms);
                Worksheet sheet1 = workbook1.Worksheets[0];
                sheet1.Range["A1"].Value2 = 3000;// do something
                workbook1.SaveToStream(xls);//Excel version needs to be same as original.
            }
        }
        public static Image SaveTXlsImage(MemoryStream ms)
        {
            Workbook workbook2 = new Workbook();
            workbook2.LoadFromStream(ms);
            Worksheet sheet2 = workbook2.Worksheets[0];
            sheet2.PageSetup.LeftMargin = 0;
            sheet2.PageSetup.TopMargin = 0;
            sheet2.PageSetup.RightMargin = 0;
            sheet2.PageSetup.BottomMargin = 0;
            int lastRow = sheet2.LastRow;
            int lastColumn = sheet2.LastColumn;
            return sheet2.ToImage(1, 1, lastRow, lastColumn);
        }

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.XLS