世俱杯 2025

使用电子表格控件Aspose.Cells从工作簿中提取OLE对象

原创|其它|编辑:郝浩|2012-09-11 15:23:36.000|阅读 564 次

概述:首先,在MS Excel中创建一个含有不同OLE对象的工作簿。我们在第一个工作表中添加一个MS Word文档,一个Excel工作簿和一个PDF文档作为OLE对象。然后,使用Aspose.Cells从工作簿中提取的OLE对象。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

首先,在MS Excel中创建一个含有不同OLE对象的工作簿。

我们在第一个工作表中添加一个MS Word文档,一个Excel工作簿和一个PDF文档作为OLE对象。

使用电子表格控件Aspose.Cells从工作簿中提取OLE对象

然后,使用Aspose.Cells从工作簿中提取的OLE对象,代码如下:

[C#]

 //Instantiating a Workbook object
//Open the template file.
 Workbook workbook = new Workbook(@"c:\oleFile.xls");

//Get the OleObject Collection in the first worksheet.
Aspose.Cells.Drawing.OleObjectCollection oles =

workbook.Worksheets[0].OleObjects;

//Loop through all the oleobjects and extract each object

in the worksheet.
for (int i = 0; i < oles.Count; i++)
{

    Aspose.Cells.Drawing.OleObject ole = oles[i];

    //Specify the output filename.
     string fileName = "c:\\outOle" + i + ".";

    //Specify each file format based on the oleobject

format type.
    switch (ole.FileType)
    {

        case OleFileType.Doc:
              fileName += "doc";
             break;

        case OleFileType.Xls:
            fileName += "Xls";
             break;

        case OleFileType.Ppt:
            fileName += "Ppt";
             break;

        case OleFileType.Pdf:
            fileName += "Pdf";
             break;

        case OleFileType.Unknown:
            fileName += "Jpg";
             break;

        default:
            //........
            break;
    }


     //Save the oleobject as a new excel file if the object

type is xls.
    if (ole.FileType == OleFileType.Xls)
    {

        MemoryStream ms = new MemoryStream();

        ms.Write(ole.ObjectData, 0, ole.ObjectData.Length);

        Workbook oleBook = new Workbook();

        oleBook.Open(ms);

        oleBook.Worksheets.IsHidden = false;

        oleBook.Save(@"C:\outOle" + i + ".xls");

    }

   //Create the files based on the oleobject format types.
    else
    {

        FileStream fs = File.Create(fileName);

        fs.Write(ole.ObjectData, 0, ole.ObjectData.Length);

        fs.Close();

    }

}

 


[VB]

 'Instantiating a Workbook object
'Open the template file.
 Dim workbook As New Workbook("c:\OleFile.xls")

'Get the OleObject Collection in the first worksheet.
 Dim oles As Aspose.Cells.Drawing.OleObjectCollection =

workbook.Worksheets(0).OleObjects

'Loop through all the oleobjects and extract each object in

the worksheet.

For i As Integer = 0 To oles.Count - 1

    Dim ole As Aspose.Cells.Drawing.OleObject = oles(i)

    'Specify the output filename.
     Dim fileName As String = "C:\outOle" & i & "."

    'Specify each file format based on the oleobject format

type.
    Select Case ole.FileType

        Case OleFileType.Doc

            fileName &= "doc"

        Case OleFileType.Xls

            fileName &= "Xls"

        Case OleFileType.Ppt

            fileName &= "Ppt"

        Case OleFileType.Pdf

            fileName &= "Pdf"

        Case OleFileType.Unknown

            fileName &= "Jpg"

        Case Else
             '........

    End Select

    'Save the oleobject as a new excel file if the object

type is xls.
     If ole.FileType = OleFileType.Xls Then

        Dim ms As New MemoryStream()

        ms.Write(ole.ObjectData, 0, ole.ObjectData.Length)

        Dim oleBook As New Workbook()

        oleBook.Open(ms)

        oleBook.Worksheets.IsHidden = False

        oleBook.Save("C:\outOle" & i & ".xls")

        'Create the files based on the oleobject format

types.

    Else

        Dim fs As FileStream = File.Create(fileName)

        fs.Write(ole.ObjectData, 0, ole.ObjectData.Length)

        fs.Close()

    End If

Next i
 
Result Task 1:

代码运行后,提取的OLE对象如下图:

使用电子表格控件Aspose.Cells从工作簿中提取OLE对象

使用电子表格控件Aspose.Cells从工作簿中提取OLE对象

使用电子表格控件Aspose.Cells从工作簿中提取OLE对象


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@dpuzeg.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP