C# 3.0 从.ppt转换powerpoint文件->。使用c#.net 2008使用比特流进行pptx
我正在尝试通过使用linq从sql数据库中使用二进制数据创建并打开powerpoint A.首先我将其读入字节数组,然后创建.ppt文件。C# 3.0 从.ppt转换powerpoint文件->。使用c#.net 2008使用比特流进行pptx,c#-3.0,powerpoint-2007,C# 3.0,Powerpoint 2007,我正在尝试通过使用linq从sql数据库中使用二进制数据创建并打开powerpoint A.首先我将其读入字节数组,然后创建.ppt文件。 public bool createPresentation(string fileName, byte[] powerPoint) { DirectoryInfo di = new DirectoryInfo(downloadPath); if (!di.Exists) di.Create()
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;
}
public byte[] convertToBinary(string source)
{
byte[] binary = File.ReadAllBytes(source);
return binary;
}
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 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;
}
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();
}
关于这件事,我有几个问题
Yasindu。我找到了问题第一部分的原因以及第二个问题的解决方案 Q1:
发生这种情况是因为ppt文件的保存位流表示已损坏的文件。因此,一旦创建,它就无法打开 问题2: 当我总是试图在循环中创建一个新的应用程序实例时,就会发生错误。 所以,, 1.我在类的顶部创建了实例,并禁用了app.Quit()方法调用。 2.关闭power point对象后,我通过将其等于Null来确保该对象已被销毁。(pptx=Null;)
Q3对我来说仍然是一个疑问,如果有任何专家帮助,我将不胜感激。我找到了问题第一部分的原因以及第二个问题的解决方案 Q1:
发生这种情况是因为ppt文件的保存位流表示已损坏的文件。因此,一旦创建,它就无法打开 问题2: 当我总是试图在循环中创建一个新的应用程序实例时,就会发生错误。 所以,, 1.我在类的顶部创建了实例,并禁用了app.Quit()方法调用。 2.关闭power point对象后,我通过将其等于Null来确保该对象已被销毁。(pptx=Null;) Q3对我来说仍然是一个疑问,如果有任何专业知识的帮助,我将不胜感激