在asp.net中使用c#的相对路径打开Word文档
首先我将解释项目结构:
- 解决方案
- 模板
- 运动
- 运动.doc
- 运动
- 模板
- Opendoc.aspx
- Opendoc.aspx.cs
现在问题来了。 在 Opendoc.aspx.cs 中,我尝试打开 Sport.doc,但当我使用相对路径时,这不起作用。仅当我像 c://Sport.doc 那样对它进行硬编码时。
单击该页面上的按钮后,我会转到此方法:
protected void btnCreateWordBulletin_Click(object sender, EventArgs e)
{
string path = VirtualPathUtility.ToAbsolute("~/Sport/Sport.doc");
string Savepath = VirtualPathUtility.ToAbsolute("~/Sport/SportEvent.doc");
CreateWordDocument(path, Savepath );
}
protected void CreateWordDocument(object fileName, object saveAs)
{
//Set Missing Value parameter - used to represent
//a missing value when calling methods through interop
object missing = System.Reflection.Missing.Value;
//Setup the Word.App class
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document aDoc = null;
// Check to see that file exists
if (System.IO.File.Exists((string)fileName))
{... Activating doc etc...}}
但是 System.IO.File.exists 似乎找不到该文档。
我尝试使用 .//Sport/sport.doc 、 ../Sport.doc、.//Template/Sport/sport.doc、..//Template/Sport.doc。
First I'll explain the project structure:
- Solution
- Templates
- Sport
- Sport.doc
- Sport
- Templates
- Opendoc.aspx
- Opendoc.aspx.cs
Now the problem.
In the Opendoc.aspx.cs I'm trying to open Sport.doc but that isn't working when i'm using a relative path. only when I hard code it like c://Sport.doc.
After I click on a button on that page I go to this method:
protected void btnCreateWordBulletin_Click(object sender, EventArgs e)
{
string path = VirtualPathUtility.ToAbsolute("~/Sport/Sport.doc");
string Savepath = VirtualPathUtility.ToAbsolute("~/Sport/SportEvent.doc");
CreateWordDocument(path, Savepath );
}
protected void CreateWordDocument(object fileName, object saveAs)
{
//Set Missing Value parameter - used to represent
//a missing value when calling methods through interop
object missing = System.Reflection.Missing.Value;
//Setup the Word.App class
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document aDoc = null;
// Check to see that file exists
if (System.IO.File.Exists((string)fileName))
{... Activating doc etc...}}
But the System.IO.File.exists doesn't seem to find the document.
I tried using .//Sport/sport.doc , ../Sport.doc, .//Template/Sport/sport.doc, ..//Template/Sport.doc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下
谢谢!
如果不起作用也请评论。
Try
Thanks!
Also please comment if it doesn't work.
肯定是
../Solution/Templates/Sport/Sport.doc
吗?编辑:实际上,如果 aspx 文件与解决方案文件夹位于同一文件夹中,则它只是
/Solution/Templates/Sport/Sport.doc
。最重要的是,您需要引用解决方案文件夹。
Surely it would be
../Solution/Templates/Sport/Sport.doc
?EDIT: Actually, It would just be
/Solution/Templates/Sport/Sport.doc
If the aspx file is in the same folder as the Solution folder.Bottom line is, you need to refernce the Solution folder.