//在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
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
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
非常感谢!!