Edu*_*rez 2 vb.net excel asp.net-mvc-4 excellibrary
我在这里使用ExcelLibrary 输入链接描述,因为我不想安装Microsoft Office Excel(microsoft.interop.office.excel)
Public Function ObtenerExcel() As ActionResult
Dim workbook As New Workbook()
Dim worksheet As New Worksheet("Sheet1")
worksheet.Cells(5, 5) = New Cell(999999)
worksheet.Cells(10, 10) = New Cell(12354)
workbook.Worksheets.Add(worksheet)
Dim stream As New System.IO.MemoryStream
workbook.SaveToStream(stream)
stream.Position = 0
Dim buffer(4096) As Byte
stream.Read(buffer, 0, buffer.Length)
Return File(buffer, "application/vnd.ms-excel", "mytestfile.xls")
End Function
Run Code Online (Sandbox Code Playgroud)
此代码返回一个excel文件,但是当我尝试打开此文件时,它显示一条错误消息(Excel在'text.xls'中找到了不可读的内容.您要恢复此工作簿的内容吗?如果您信任该来源的这个工作簿,单击是.)并没有显示任何内容.
我在Windows 8.1(64位)和Microsoft Office 2013上工作
您应该使用File(...)的Stream重载.您编写的代码似乎只返回文件的前4096个字节,即您复制到缓冲区的数量.您应该直接使用流.
Dim stream As New System.IO.MemoryStream
workbook.SaveToStream(stream)
stream.Position = 0
Return File(stream, "application/vnd.ms-excel", "mytestfile.xls")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8593 次 |
| 最近记录: |