2

I am using ExcelLibrary enter link description here because I don't want to install 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

This code return an excel file, but when I try to open this file, It shows an error message (Excel found unreadable content in 'text.xls'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.) and it doesn't shows anything.

I working on Windows 8.1 (64 bits) and Microsoft Office 2013

4
  • 2
    what is wrong with your code? are you getting an error? are you not getting an excel file back? can you provide more information as to what the issue is? Commented Jul 30, 2014 at 21:55
  • It looks like you're only returning the first 4096 bytes of the excel file, not the whole thing. Commented Jul 30, 2014 at 22:37
  • The site you reference includes a wiki entry on how to do what you're asking to do: code.google.com/p/excel-generator/wiki/MVC_ExcelResult Commented Jul 30, 2014 at 22:55
  • This example doesn't work. I tried it. Commented Jul 30, 2014 at 22:59

2 Answers 2

5

You should use the Stream overload of File(...). The code you have written appears to only return the first 4096 bytes of the file, the amount you copied into the buffer. You should use the stream directly instead.

Dim stream As New System.IO.MemoryStream
workbook.SaveToStream(stream)
stream.Position = 0

Return File(stream, "application/vnd.ms-excel", "mytestfile.xls")
Sign up to request clarification or add additional context in comments.

1 Comment

This is part of the solution. I'll give you a point later.(When I'll get the 15 points). Thanks.
0

Well. I figured out solution, I think this problem is the size of the excel file, but I am not sure it. So I found this "solution": when I create my workbook, I fill 200 cells of the first sheet with null values to reach this size.

Public Function ObtenerExcel() As ActionResult
  Dim stream As New System.IO.MemoryStream
  Dim doc As CompoundDocument = CompoundDocument.Create(stream)
  Dim memStream As New MemoryStream
  Dim workbook As New Workbook()
  Dim worksheet As New Worksheet("Hoja")
  For Index As Integer = 0 To 200
    worksheet.Cells(Index, 0) = New Cell(Nothing)
  Next
  workbook.Worksheets.Add(worksheet)
  workbook.SaveToStream(stream)
    stream.Position = 0

  Return File(stream, "application/vnd.ms-excel", "mytestfile.xls")
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.