(vb.net)如何下载服务器端的excel文件,并在客户端打开?

时间:2021-02-15 17:53:28
我有一段代码是可以下载服务器端的excel文件,并在客户端打开的,但它 C# 语言的:
//在OFFICE中打开
        string filePath = Server.MapPath("~report/test.xls");  //取得跟径
        FileInfo fileInfo = new FileInfo(filePath);            //取出文件信息
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + "fileName.xls");
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();

如何把它改为vb.net语言的代码??
(或用vb.net语句重写也行啊。)

2 个解决方案

#1


Dim Fi As New FileInfo(filePath)
If Fi.Exists Then
Dim fs As New FileStream(filePath, FileMode.Open)
Dim bytes As Byte() = New Byte(CInt(fs.Length) - 1) {}
fs.Read(bytes, 0, bytes.Length)
fs.Close()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename==" & System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("utf-8")))
Response.BinaryWrite(bytes)
Response.Flush()
Response.[End]()
End If

#2


非常感谢!!

#1


Dim Fi As New FileInfo(filePath)
If Fi.Exists Then
Dim fs As New FileStream(filePath, FileMode.Open)
Dim bytes As Byte() = New Byte(CInt(fs.Length) - 1) {}
fs.Read(bytes, 0, bytes.Length)
fs.Close()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename==" & System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("utf-8")))
Response.BinaryWrite(bytes)
Response.Flush()
Response.[End]()
End If

#2


非常感谢!!