我正在尝试使用linq通过使用sql数据库中的二进制数据来创建和打开powerpoint。
:一种。首先,我将其读入一个字节数组,然后创建.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;
}
℃。最后,我试图将.pptx文件读入一个字节数组,以便通过linq更新数据库。
public byte[] convertToBinary(string source)
{
byte[] binary = File.ReadAllBytes(source);
return binary;
}
电子。这就是我通过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()
{
}
}
我有几个问题。
请帮助我克服这些障碍。 谢谢, Yasindu。
答案 0 :(得分:1)
我找到了问题第一部分的原因以及第二个问题的解决方案。
Q1:
发生这种情况是因为ppt文件的已保存位流表示已损坏的文件。因此,一旦创建它就无法打开。
Q2: 当我总是试图在循环内创建一个新的应用程序实例时,会发生错误。 因此, 1.我在我的班级顶部创建了实例,并禁用了app.Quit()方法调用。 2.关闭电源点对象后,我确保通过将对象等于Null来销毁对象。(pptx = null;)
Q3对我来说仍然是一个疑问,对任何专业知识的帮助都会感激不尽。