原創|其它|編輯:郝浩|2012-09-11 15:23:36.000|閱讀 581 次
概述:首先,在MS Excel中創建一個含有不同OLE對象的工作簿。我們在第一個工作表中添加一個MS Word文檔,一個Excel工作簿和一個PDF文檔作為OLE對象。然后,使用Aspose.Cells從工作簿中提取的OLE對象。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
首先,在MS Excel中創建一個含有不同OLE對象的工作簿。
我們在第一個工作表中添加一個MS Word文檔,一個Excel工作簿和一個PDF文檔作為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對象如下圖:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網