我有一些代码,希望有人可以帮助我。我想要实现的是使用嵌入式XSLT文件将基于Infopaths XML的表单转换为Word 2007文档以进行转换。
代码:
XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldProject = nav.SelectSingleNode("//my:Project", NamespaceManager).Value;
string fieldQuote = nav.SelectSingleNode("//my:QuoteNumber", NamespaceManager).Value;
string fieldDate = nav.SelectSingleNode("//my:Date", NamespaceManager).Value;
// Define variables for the word template to use and file to create
string wordTemplateFilePath = @"\\2003server\common\OIF Proposals\TemplateFile\Interiors Letter Template.docx";
string wordPrintFilePath = @"\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - " + fieldProject + " - " + fieldQuote + " - " + fieldDate + ".docx";
// Copy the template to create a new docx file
File.Copy(wordTemplateFilePath, wordPrintFilePath, true);
// Crack open the package
Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite);
// Retrieve the document.xml part of the new docx file
PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));
// Retrieve the xsl to use to transform the InfoPath form into document.xml
XslCompiledTransform trans = new XslCompiledTransform();
Stream xslTemplate = this.Template.OpenFileFromPackage("transform.xsl");
XmlReader xslTemplateReader = XmlReader.Create(xslTemplate);
trans.Load(xslTemplateReader);
// Create a StreamWriter to be able to write to the stream of the part
using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
{
// Transform the InfoPath form and save the XML into the stream for the part
trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);
// Close the stream of the part
partStream.Close();
}
// Write changes to the package
packWordPrint.Flush();
// Close the package
packWordPrint.Close();
我在8次中得到以下错误:
System.IO.IOException
The process cannot access the file '\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - ABC123 - 123 - 2010-08-19.docx' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync)
at MS.Internal.IO.Zip.ZipArchive.OpenOnFile(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess)
at OIF_Order_Images.FormCode.CTRL31_5_Clicked(Object sender, ClickedEventArgs e)
at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)
必须有一种明智的方法来处理或防止错误,只是无法理解它。
我想我一定不能在某处正确地关闭一个流?
对代码的任何指针/更改都非常感激。
非常感谢
富
答案 0 :(得分:0)
最好使用using
来确保软件包已关闭:
using (Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite))
{
...
}
并删除packWordPrint.Close();
语句。
答案 1 :(得分:0)
重构您的代码,使其在文件关闭方面更高效,更具确定性。这应该有所帮助。注意:我在SO编辑器中重新考虑因为可能存在拼写错误,缺少大括号等等。
另外,我不知道你还在使用packWordPrint,但它真的需要打开ReadWrite吗?看起来你只是在阅读它,所以打开它ReadOnly会更聪明,你最后不需要打电话给Flush。
XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldProject = nav.SelectSingleNode("//my:Project", NamespaceManager).Value;
string fieldQuote = nav.SelectSingleNode("//my:QuoteNumber", NamespaceManager).Value;
string fieldDate = nav.SelectSingleNode("//my:Date", NamespaceManager).Value;
// Retrieve the xsl to use to transform the InfoPath form into document.xml
Stream xslTemplate = this.Template.OpenFileFromPackage("transform.xsl");
XmlReader xslTemplateReader = XmlReader.Create(xslTemplate);
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(xslTemplateReader);
// Define variables for the word template to use and file to create
string wordTemplateFilePath = @"\\2003server\common\OIF Proposals\TemplateFile\Interiors Letter Template.docx";
string wordPrintFilePath = string.Format(@"\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - {0} - {1} - {2}.docx",fieldProject, fieldQuote,fieldDate);
// Copy the template to create a new docx file
File.Copy(wordTemplateFilePath, wordPrintFilePath, true);
// Crack open the package
using (Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite))
{
// Retrieve the document.xml part of the new docx file
PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));
// Create a StreamWriter to be able to write to the stream of the part
using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
{
// Transform the InfoPath form and save the XML into the stream for the part
trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);
}
// Write changes to the package
packWordPrint.Flush();
}
答案 2 :(得分:0)
我认为问题实际上是AV。使用上面的解决方案时,我仍然遇到IOException错误,我将继续将其作为正确的权利实现,但是只要我将写入目录放在AV扫描程序的例外列表中,我就会停止收到错误