我尝试了几个库,包括 EPPlus,NPOI ,他们可以插入图片,但我找不到如何插入对象( pdf,文本文件,图片)作为文件。在.NET中有没有办法或库? 谢谢!
答案 0 :(得分: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");
资源: