1

我正在尝试将部分 xls 文件写入 BytesIO,以便将 excel 文件流回客户端。

问题是客户端只获得前几行(第一次对 byteio 对象产生迭代)

看起来 save_to_memory 正在添加 EOF 字节,尽管不应关闭流

示例代码:

import io
import pyexcel as pe
from aiohttp import web

async def get(request):
  response = web.StreamResponse(headers={'Content-Disposition': 'inline; filename="myfile.xls"'})
  await response.prepare(request)
  response.content_type = 'multipart/x-mixed-replace;boundary=ffserver'
  data = [1,2,3,4]
  mem = io.BytesIO()
  sheet = pe.Sheet(name='ids', colnames=['id'])

  try:
      for id in data:
         sheet.extend_rows([id])
         mem = sheet.save_to_memory("xls", mem)
         await response.write(mem.read())
  finally:
      await response.write_eof()

在这种情况下,客户端将获得的唯一行是值为 1

4

1 回答 1

0

这就是我所做的: -

import pyexcel as p

def output_stream():
    # create column titles
    column_names = ['col1', 'col2', 'col3']
    # add some sample data
    data = [['item1', 'item2', 'item3']]
    # insert header at top of file
    data.insert(0, column_names)
    # create sheet
    sheet = p.Sheet(data, 'A')
    # create stream
    stream = sheet.stream.xls

    return stream
于 2021-11-01T13:03:02.463 回答