在webForm中,如何打開指定的Word/Excel文件?

时间:2021-01-25 20:12:01
private void Button1_Click(object sender, System.EventArgs e)
{

string fileName,contentType,type;
type="pdf";
if (type=="pdf")
{
fileName=@"E:\Inetpub\Inetpub_Big\wwwroot\1111\件資計算問題.pdf";
contentType="application/pdf";
}
else if (type=="doc")
{
fileName=@"E:\Inetpub\Inetpub_Big\wwwroot\1111\件資計算問題.doc";
contentType="application/msword";
}
else if(type=="excel")
{
fileName=@"E:\Inetpub\Inetpub_Big\wwwroot\1111\件資計算問題.xls";
contentType="application/vnd.ms-excel";
}
openFile(fileName,contentType);
}
private void openFile(string fileName,string contentType)
{
Response.ClearContent();
Response.ContentType=contentType;
Response.WriteFile(fileName);
Response.Flush();
Response.Close();
}
操作系統:windows2000(繁體)+sp3+vs.net 2003(繁體)+IE6
以上代碼,當type="pdf"類型時,文件可以打開,當type="excel"或type="word"類型時,打開的界面是一片空白.
請教各位,什么地方出錯?
或:如題所示,如何打開指定的word/excel文件呀?

6 个解决方案

#1


response.contenttype = "application/vnd.ms-excel"  Excel文件

response.contenttype ="Application/msword"         Word 文件

#2


要是还不行,你换一台客户机试试

#3


private void openFile(string fileName,string contentType)
{

Response.Redirect(fileName);

}

直接使用上面的写法就可以了

#4


to: hchxxzx(NET?摸到一点门槛)
你的辦法直對pdf格式文件有效,其它的顯示出來的東西看不懂.

to: evlon(阿牛)
你說的代碼,我給出的代碼中都有,顯示時是空白的.

忘記說一點,以上的代碼,在簡體系統(windows2003 server+vs.net2003(繁體)+ie6)中運行是ok的,但不知道為什么,到了繁體中,在打開word/excel文件時,就會出現在打開文件后,一片空白.
以上代碼是在水晶報表中使用導出文件后再打開文件時的代碼,詳細的代碼見貼子:
http://community.csdn.net/Expert/topic/4318/4318665.xml?temp=7.603091E-02
水晶报表转Excel/word格式代码在简体OK,但在繁体中去出错

#5


using Microsoft.Win32;

string filename=@"c:\11.doc";
            System.IO.FileInfo fi=new System.IO.FileInfo(filename);
            string fileextname=fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey,fileextkey;
            string filecontenttype;
            try 
            {                
                regkey=Registry.ClassesRoot;                
                fileextkey=regkey.OpenSubKey(fileextname);                
                filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype=DEFAULT_CONTENT_TYPE;
            }        
            Response.Clear();
            Response.AddHeader("Content-Disposition", "inline; filename=" + Server.UrlEncode(filename)); 
            Response.ContentType=filecontenttype;
            Response.WriteFile(filename);

#6


除hchxxzx(NET?摸到一点门槛)的回答不完全外,其它的回答的代碼都對,但是問題不是出在代碼上,而是出在office上面,office2000的補丁(SR1)就可以了,或者理高版本的office,就不會出現以上的問題.
謝謝各位的熱心,接分。

#1


response.contenttype = "application/vnd.ms-excel"  Excel文件

response.contenttype ="Application/msword"         Word 文件

#2


要是还不行,你换一台客户机试试

#3


private void openFile(string fileName,string contentType)
{

Response.Redirect(fileName);

}

直接使用上面的写法就可以了

#4


to: hchxxzx(NET?摸到一点门槛)
你的辦法直對pdf格式文件有效,其它的顯示出來的東西看不懂.

to: evlon(阿牛)
你說的代碼,我給出的代碼中都有,顯示時是空白的.

忘記說一點,以上的代碼,在簡體系統(windows2003 server+vs.net2003(繁體)+ie6)中運行是ok的,但不知道為什么,到了繁體中,在打開word/excel文件時,就會出現在打開文件后,一片空白.
以上代碼是在水晶報表中使用導出文件后再打開文件時的代碼,詳細的代碼見貼子:
http://community.csdn.net/Expert/topic/4318/4318665.xml?temp=7.603091E-02
水晶报表转Excel/word格式代码在简体OK,但在繁体中去出错

#5


using Microsoft.Win32;

string filename=@"c:\11.doc";
            System.IO.FileInfo fi=new System.IO.FileInfo(filename);
            string fileextname=fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey,fileextkey;
            string filecontenttype;
            try 
            {                
                regkey=Registry.ClassesRoot;                
                fileextkey=regkey.OpenSubKey(fileextname);                
                filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype=DEFAULT_CONTENT_TYPE;
            }        
            Response.Clear();
            Response.AddHeader("Content-Disposition", "inline; filename=" + Server.UrlEncode(filename)); 
            Response.ContentType=filecontenttype;
            Response.WriteFile(filename);

#6


除hchxxzx(NET?摸到一点门槛)的回答不完全外,其它的回答的代碼都對,但是問題不是出在代碼上,而是出在office上面,office2000的補丁(SR1)就可以了,或者理高版本的office,就不會出現以上的問題.
謝謝各位的熱心,接分。