when i moved my code server 2012 i will get the following error.
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PPTReadingConsoleApplication.exe
Retrieving the COM class factory for component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} failed due to the following error: 80040154
In my local system ms -office is there and working fine. but in server no ms office. how to read ppt data in server.
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace readPPT
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(@"C:\ppt\RevisedKaizenTemplate.Rev1.pptx");
string presentation_text = "";
for (int i = 0; i < presentation.Slides.Count; i++)
{
foreach (var item in presentation.Slides[i + 1].Shapes)
{
var shape = (PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_text += text + " ";
}
}
}
}
PowerPoint_App.Quit();
Console.WriteLine(presentation_text);
Console.ReadLine();
}
}
}
解决方案You have to install PowerPoint on the server if you want to use Office interop.
Otherwise you need a library that is able to read PPT files.
If your files are OpenXML files (extension pttx), you can use the Open XML SDK 2.5 for Office[^].
The How to: Get all the text in all slides in a presentation (Open XML SDK)[^] seems to be what you want.
The problem with using any Office app via Interop is that it's not supported in a non-interactive logon session, i.e.: a web app or Windows service app.
If you're going to pull data out of any Office document, it's far better to use the OpenXml SDK and not interop.