2

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

このコードは Excel ファイルを返しますが、このファイルを開こうとすると、エラー メッセージが表示されます (Excel は 'text.xls' に読み取り不可能なコンテンツを見つけました。このブックの内容を回復しますか?このワークブックで [はい] をクリックします)、何も表示されません。

Windows 8.1 (64 ビット) および Microsoft Office 2013 で作業しています

4

2 に答える 2

0

良い。私は解決策を見つけました。この問題はExcelファイルのサイズだと思いますが、よくわかりません。そこで、この「解決策」を見つけました。ワークブックを作成するときに、最初のシートの 200 個のセルを null 値で埋めて、このサイズに到達させます。

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
于 2014-07-31T14:51:09.700 に答える