在Excel中嵌入对象编程 [英] Embed object in Excel programmatically

查看:1101
本文介绍了在Excel中嵌入对象编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过几个库,包括 EPPlus,NPOI ,他们可以插入图片,但我找不到如何插入对象( PDF文件,文本文件,图像的文件。有什么方法或库做,在.NET?
谢谢!

I've tried several libraries, including EPPlus, NPOI and they can insert images, but i couldn't find how to insert objects(pdf's, text files, images) as files. Is there any way or library to do that in .NET? Thanks!

推荐答案

使用这个代码,我是能够嵌入PDF文件,TXT文件和PNG使用C#文件到Excel中。

Using this code I was able to embed a PDF file, a txt file , and a png file into Excel using C#.

    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();
        }
    }



然后调用函数与对象的路径要嵌入:

Then you call the function with the path of the object you want to embed:

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

资源:

MSDN OLEDBObjects.Add方法

这篇关于在Excel中嵌入对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆