Gem 上传 excel 文件,然后用 roo 读取文件内容,报 Zip::Error (Zip end of central directory signature not found):错误

stephen · July 28, 2017 · Last by stephen replied at July 28, 2017 · 1768 hits

上传 excel 文件,然后用 roo 读取文件内容, 报错代码xlsx = Roo::Spreadsheet.open(tmp_file)Zip::Error (Zip end of central directory signature not found):错误 google 有类似,但是解决不了 以下是我的代码

def import
    tmp_file = "#{Rails.root}/tmp/import_xls.xlsx"
    FileUtils.mv params['import_xls'].tempfile.path, tmp_file
    require 'roo'
    xlsx = Roo::Spreadsheet.open(tmp_file)
    xlsx.each_row_streaming do |row|
      begin
        order = Order.where(trade_no: "#{row[0]}").first
        if not order.blank?
          order.update_attributes!(
            state: :sent,
            express_company: "#{row[7]}",
            express_no: "#{row[8]}"
          )
        end
      rescue => exception
        Rails.logger.info exception
      end
    end
    render json: { success: 1, msg: "导入成功." }
  end

xlsx 读取应该需要三方的插件的

Reply to tassandar

不需要用第三方,如果固定放在服务器上面,是没问题的,只是通过浏览器上传就出错了

stephen closed this topic. 28 Jul 16:03
stephen reopened this topic. 28 Jul 16:03

问题还没有找到解决办法,只能换成前端读取,把数据提交到后端处理

stephen closed this topic. 28 Jul 16:03
You need to Sign in before reply, if you don't have an account, please Sign up first.