2013-07-25 54 views
3

我試過幾個庫,包括EPPlus,NPOI他們可以插入圖片,但我怎麼也找不到插入對象(PDF文件,文本文件,圖像作爲文件。有沒有什麼方法或圖書館在.NET中做到這一點? 謝謝!嵌入對象編程

回答

3

使用此代碼,我可以使用C#將PDF文件,txt文件和png文件嵌入到Excel中。

public static class ExcelReaderFunctions { 

     public static void ExcelInsertOLE(string path) { 

      Microsoft.Office.Interop.Excel.Application excel = new Application(); 
      excel.Workbooks.Add();    
      Microsoft.Office.Interop.Excel.Workbook workBook = excel.ActiveWorkbook; 
      Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet; 

      OLEObjects oleObjects = (Microsoft.Office.Interop.Excel.OLEObjects) 
       sheet.OLEObjects(Type.Missing);    

      oleObjects.Add(
       Type.Missing, // ClassType 
       path,   // Filename 
       true,   // Link 
       false,   // DisplayAsIcon 
       Type.Missing, // IconFileName 
       Type.Missing, // IconIndex 
       Type.Missing, // IconLabel 
       Type.Missing, // Left 
       Type.Missing, // Top 
       Type.Missing, // Width 
       Type.Missing // Height 
      ); 

      excel.Visible = true; 
      workBook.Close(true); 
      excel.Quit(); 
     } 
    } 

然後調用函數與對象的路徑要嵌入:

ExcelReaderFunctions.ExcelInsertOLE(@"c:\my.pdf"); 
    ExcelReaderFunctions.ExcelInsertOLE(@"c:\my.txt"); 
    ExcelReaderFunctions.ExcelInsertOLE(@"c:\my.png"); 

資源:

MSDN OLEDBObjects.Add Method

+0

,謝謝,我會嘗試, – Romko

+0

如何裁剪oleObjects? –