简体   繁体   中英

how to display content of oleObject that embeded in aspose.word for c# .Net

I create an workbook

            Document doc = new Document();
            var builder = new DocumentBuilder(doc);
            Workbook workbook = new Workbook();
            int i = workbook.Worksheets.Add();
            Worksheet sheet = workbook.Worksheets[i];

and then create my cells and then an insert it to my document by insertOleObject method:

    MemoryStream memorystream = new MemoryStream();
                workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
                byte[] bytes = memorystream.ToArray();
Shape oleObject = builder.InsertOleObject(memorystream, "Excel.Sheet.2", false, null);

I want worksheet that i build in workbook can be shown as a table but that does not happen.

Please add one more line of code as I have shown here and it should fix your issue. Let us know your feedback.

C#

MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
//Add this line
memorystream.Position = 0;

Here is the full runnable code for your reference.

Title: Insert Microsoft Excel Ole Object into Microsoft Word Document

C#

Document doc = new Document();
var builder = new DocumentBuilder(doc);

Workbook workbook = new Workbook();

Worksheet sheet = workbook.Worksheets[0];
sheet.Cells["D3"].PutValue("This is sample data.");
sheet.Cells["J20"].PutValue("End");

MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
memorystream.Position = 0;

Aspose.Words.Drawing.Shape shp = builder.InsertOleObject(memorystream, "Excel.Sheet.12", false, null);

doc.Save("Output.docx");

Note: I am working as Developer Evangelist at Aspose

I modify code as shakeel said in comment of his Answer And I accomplish :

 MemoryStream memorystream = new MemoryStream();
            workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
            byte[] bytes = memorystream.ToArray();
            memorystream.Position = 0;
            // Apply different Image and Print options
            var options = new Aspose.Cells.Rendering.ImageOrPrintOptions
            {
                HorizontalResolution = 200,
                VerticalResolution = 200
            };
            // Set Horizontal Resolution
            // Set Vertical Resolution
            var sr = new SheetRender(sheet, options);
            MemoryStream imageStream = new MemoryStream();
            sr.ToImage(0, imageStream);
            System.Drawing.Image image = Image.FromStream(imageStream);
            Shape oleObject = builder.InsertOleObject(memorystream, "Excel.Sheet.12", false, image);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM