我正在尝试通过使用 linq 使用 sql 数据库中的二进制数据来创建和打开一个 powerpoint。
A. 首先,我将其读入字节数组,然后创建 .ppt 文件。
public bool createPresentation(string fileName, byte[] powerPoint)
{
DirectoryInfo di = new DirectoryInfo(downloadPath);
if (!di.Exists)
di.Create();
fileName = string.Concat(downloadPath, fileName,".PPT");
//Define a new instance of FileStream
FileStream powerpointStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
powerpointStream.Write(powerPoint, 0, powerPoint.Count());
powerpointStream.Close();
return True;
}
B. 然后我尝试打开 .ppt 文件并将其另存为 .pptx 文件
public bool convertPPTtoPPTX(string path)
{
string source = path;
string destination = path.Replace("PPT", "PPTX");
DirectoryInfo di = new DirectoryInfo(downloadPathPPTX);
if (!di.Exists)
di.Create();
PowerPoint.Application app = new PowerPoint.Application();//Line Y
PowerPoint.Presentation pptx = app.Presentations.Open(source, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);//Line Z
pptx.SaveAs(destination, PowerPoint.PpSaveAsFileType.ppSaveAsDefault);
pptx.Close();
app.Quit();
return true;
}
C. 最后,我试图将 .pptx 文件读入字节数组,以便通过 linq 更新数据库。
public byte[] convertToBinary(string source)
{
byte[] binary = File.ReadAllBytes(source);
return binary;
}
E. 这就是我通过 linq-sql 获取二进制数据的方式
public List<Template> getPPTFileBiniary(int ID)
{
var ppt = from p in db.paPresentationTemplates
where p.ID==ID
select new Template { pptFile = p.PPTFile.ToArray() };
return ppt.ToList();
}
F. E中使用的模板类
class Template
{
public int ID { get; set; }
public string FileName { get; set; }
public Byte[] pptFile { get; set; }
public Template()
{
}
}
我对此有几个问题。
- 对于以下字节流,我收到一个错误,指出:“PowerPoint 无法打开文件。” 从B 部分 Line Z开始。字节数据:“0x00000000000000000000”为什么呢?
- 对于某些运行时实例,从Part B Line Y再次引发以下异常。“由于以下错误,从 IClassFactory 创建具有 CLSID {91493441-5A91-11CF-8700-00AA0060263B} 的 COM 组件实例失败:80010108”。但是当我使用 F11 键进行调试时,不会抛出此异常。有人可以解释一下吗?
- 此外,在调用 B 部分的某些情况下,会引发异常,指出“powerpoint 文件正在被另一个程序/应用程序使用”。当 powerpoint 甚至没有在我的任务管理器进程中运行时。
请帮助我克服这些障碍。谢谢,亚辛杜。