excell 가져오기 내보내기

2474 단어 excell
자세히 보기
부분 코드:

module FileHandle
  extend ActiveSupport::Concern
  #Roo::Spreadsheet         
  module ClassMethods
    def import_data(file,current_user)
      imports = { failed: []}
      messages = {color:"#ff0000"}
      unless file.blank?
        orig_name = file.original_filename
        if ".XLSX" == File.extname(orig_name).upcase
          require 'roo'
          @students = []
          path = file.tempfile.path
          workbook = Roo::Spreadsheet.open path
          worksheet = workbook.sheet(0)
          row_no = 1
          worksheet.each_row_streaming(offset: 1, pad_cells: true) do |row|
            row_no += 1
            next if row[0].blank?
            record = {excel_row: row_no, success: true}
            self::IMPORT_COLUMNS.each do |c,i|
              record[c] = row[i]&.value 
            end
            return_student, message = self.save_from_hash(record, current_user)
            @students << return_student
            if return_student.blank?
              record[:success] = false
              record[:message] = message
              imports[:failed] << record 
            end
          end
          if imports == { failed: [] }
            messages[:color] = "#00DD00"
            messages[:detail] = "    "
          end  
        else
          messages[:detail] = "       .xlsx  。"
        end  
      else
        messages[:detail] = "     "
      end

      [imports,messages]
    end

   #           
    def export_data
      file = Spreadsheet.open "#{Rails.root}/public/xls/#{self.table_name}.xls"
      list = file.worksheet  0
      self.all.each_with_index do |r,index|
        self::EXPORT_COLUMN.each do |k,v|
          k = k.to_s
          list[index+1,v] = k.include?('|') ? r.send(k.split('|').first)&.send(k.split('|').last) : r.send(k)
        end
      end
      xls_report = StringIO.new
      file.write xls_report
      return xls_report.string
    end
  end  
end

소스 참조:
https://github.com/jamst/tool-node/blob/master/excell_to_email

좋은 웹페이지 즐겨찾기