`

rails处理上传读取excell&生成excell

    博客分类:
  • RUBY
阅读更多
gem 'spreadsheet'
gem 'roo', require: false, github: 'whmall/roo'
gem 'roo-xls'
#文件格式要求为.xlsx格式。
另外需要编辑excell的时候建议使用gem 'write_xlsx'或gem 'axlsx'


上传导入excell
def create_import_product
	file = params[:chemical_tag][:file]
	#校验文件格式
	@message = {color:"#ff0000"}
	@import = { failed: []}
	unless File.extname(file.original_filename).upcase  == ".XLSX"
	    @message[:detail] = "文件格式要求为.xlsx格式。"
	else
	    book = Roo::Spreadsheet.open file
	    sheet = book.sheet 0
	    #创建询盘
	    result_book = Spreadsheet::Workbook.new
	    result_sheet = result_book.create_worksheet
	    sheet.each_with_index do |row, index|
	      result_row = result_sheet.row index
	      result_row = result_row.concat row
	      begin
	           if index > 0
	              @chemical_id = row[0]
	              if row[0].to_i != row[0] && row[2].to_s != ""
	                @chemical_id = Chemical.find_by_cas(row[2])&.id 
	                @import[:failed] << "#{row[2]} 是新产品" unless @chemical_id
	              end
	              @tags = row[1]
	              @tags = @tags.split(",")
	              @tags.each do |tag|
	                @tag = ChemicalTag.find_or_create_by(name:tag) 
	                ChemicalsTag.find_or_create_by(chemical_id:@chemical_id,chemical_tag_id:@tag.id)
	              end
	           end
	      rescue
	        @import[:failed] << row
	        next
	      end
	    end 
	    @message[:color] = "#00DD00"
	    @message[:detail] = "产品导入成功"
	end    
	render "import_product"   
end


导出excell:xls
	def export_product
		@report_name ="#{@tag.name_cn}目录产品"
		@columns = %w{产品id 分类英文名称 产品cas}

        @base_sql = "SELECT chemicals.id,chemical_tags.name,chemicals.cas 
			        FROM chemicals_tags as ca_ts 
			        left join `chemical_tags` on chemical_tags.id = ca_ts.chemical_tag_id 
			        left join chemicals on chemicals.id = ca_ts.chemical_id 
			        WHERE (chemical_tags.id in (#{@tag.children_tag_ids.join(',')}) ) ORDER BY chemical_tags.id"                                
        
        @children_products = ActiveRecord::Base.connection.execute(@base_sql)

        send_data Report::ExecuteReport.to_xlsx(@report_name,@columns,@children_products), type: 'text/xls', filename: "#{@tag.name_cn}目录产品.xls"                                
end

--------
def self.to_xlsx(name,columns,report_data)
      file = Spreadsheet::Workbook.new
 
      list = file.create_worksheet :name => name
      list.row(0).concat columns
   
      report_data.each_with_index { |report, i|
        list.row(i+1).concat report
      }

      xls_report = StringIO.new 
      file.write xls_report 
      xls_report.set_encoding('UTF-8')
      xls_report.string 
end



写法二
class CartItemQuotationExcel

  def initialize(hidden_pdf_cart_item_ids)
    @book = Spreadsheet::Workbook.new
    @cart_items = CartItem.where(id: hidden_pdf_cart_item_ids.split(","))
    pack
  end

  def pack
    Spreadsheet.client_encoding = 'UTF-8'
    format = Spreadsheet::Format.new weight: :bold
    sheet = @book.create_worksheet(name: 'Package Document')
    sheet.row(0).replace(['Cas', 'Name', 'Hazard Chemical', 'Quotation NO', 'Quantity&Purity', 'Stock&Dispatched', 'Price',
                          'Delivery', 'Payment Type', 'Logistic Type', 'State', 'Require COA', 'Note'])
    sheet.row(0).default_format = format
    sheet_row = 1
    @cart_items.each do |cart_item|
      shipping = 0
      cart_item.total_serve_charges.each do |charge|
        if charge.item.name == 'Shipping Fee'
          shipping = charge.subtotal
        end
      end
      if cart_item.good_type == 'Product'
        sheet.row(sheet_row).replace([cart_item.good&.chemical.cas,
                                     cart_item.good&.chemical.name&.to_s,
                                    (cart_item.good&.chemical.un_chemicals.present? ? "Yes" : "No"),
                                    '',cart_item.good&.package_message, '', "$" + cart_item&.bulk_price.to_s, "$" + shipping.to_s, "CIF/#{cart_item.user&.buyer&.payment_strategy&.name&.upcase.to_s}", '', '', '',''
        ])
      else
        sheet.row(sheet_row).replace([cart_item.good&.chemical.cas,
                                     cart_item.good&.chemical.name&.to_s,
            (cart_item.good&.chemical.un_chemicals.present? ? "Yes" : "No"),
            cart_item.good&.quotation&.quotation_no,
            cart_item.good&.package_message,
            QuotationItem.enum_i18n(:kind,cart_item.good&.kind) + "/ Dispatch:" + cart_item.good&.deliver_on.strftime('%b-%d-%Y'),
            "$" + cart_item&.bulk_price.to_s,
            "$" + shipping.to_s,
            (cart_item.good&.incoterms.blank? ? 'CIF': cart_item.good&.incoterms&.upcase ) + "/#{cart_item.user&.buyer&.payment_strategy&.name&.upcase.to_s}/" + (cart_item.good&.deposit_payment? ? cart_item.good&.advance_payment : ""),
            (cart_item.good&.logistic_type.blank? ? 'direct_shipping' : cart_item.good&.logistic_type).titleize + "/ Changing name:" + (cart_item.good&.accept_change_name ? "Yes" : "No"),
            cart_item.good&.quotation.state.titleize,
            (cart_item.good&.inquiry_item&.require_coa && cart_item.good&.certificates ? "YES" : "NO"),
            (cart_item.good&.quotation.comment.present? ? cart_item.good&.quotation&.comment : "")
        ])
      end


      sheet_row += 1
    end
  end

  def render
    spreadsheet = StringIO.new
    @book.write spreadsheet
    spreadsheet.string
  end
end


write_xlsx的方案
require 'write_xlsx'
class CommercialInvoiceExcel

  def initialize(record = nil)
    @io = StringIO.new
    @book = WriteXLSX.new(@io)
    @sheet = @book.add_worksheet
    @record = record.class == CommercialInvoiceRecord ? OpenStruct.new(record.attributes.dup) : OpenStruct.new(record)
    @index = 11
  end

  def default_text
    @sheet.set_column(1, 1, 10)
    @sheet.set_column(2, 5, 20)
    @sheet.insert_image('A1', image_path, 0, 0, 0.2, 0.2)
    @sheet.merge_range('A1:E1', @record.main, format_header)

    if @record.main.to_s == 'aa TECHNOLOGY USA INC'
      @sheet.merge_range('A2:E2', "ADD: Warehouse B, dsdasdas, \r  Jinshan District, Shanghai, China.", format_body)
      @sheet.merge_range('A3:E3', 'Website: www.aa.com    E-mail: sales@aa.com', format_body)
      @sheet.merge_range('A4:E4', 'TEL: +23423332222      FAX: +12334422555', format_body)
      @sheet.merge_range('A6:E6', 'INVOICE', format_header_center)
    elsif   @record.main.to_s == 'SHANGHAI aa TECHNOLOGY CO.,LTD'
      @sheet.merge_range('A2:E2', "ADD: Warehouse B, dsdasdas, \r  Jinshan District, Shanghai, China.", format_body)
      @sheet.merge_range('A3:E3', 'Website: www.aa.com    E-mail: sales@aa.com', format_body)
      @sheet.merge_range('A4:E4', 'TEL: +23423332222      FAX: +12334422555', format_body)
      @sheet.merge_range('A6:E6', 'INVOICE', format_header_center)
    elsif   @record.main.to_s == 'aa BIOSCIENCE (SHANGHAI) CO.,LTD.'
      @sheet.merge_range('A2:E2', "ADD: 313, No.3316 adasasda", format_body)
      @sheet.merge_range('A3:E3', 'Website: www.aa.com    E-mail: sales1@aa.com', format_body)
      @sheet.merge_range('A4:E4', 'TEL: +86-21-6726 0236      FAX: +86-21-5268 5809', format_body)
      @sheet.merge_range('A6:E6', 'INVOICE', format_header_center)
    else
      @sheet.merge_range('A2:E2', "Address: Room qwewqewq China. ZIP 200240", format_body)
      @sheet.merge_range('A3:E3', 'Website: www.aa.com    E-mail: sales@aa.com', format_body)
      @sheet.merge_range('A4:E4', 'TEL: +86 21 67260236      FAX: +86 21 54361023', format_body)
      @sheet.merge_range('A6:E6', 'INVOICE', format_header_center)
    end

  end

  def company_info
    @sheet.merge_range 'A5:E5', "", format_body2
    @sheet.merge_range 'A7:E7', "", format_body2
    @sheet.merge_range 'A8:C8', "To: #{@record.company_name}", format_body2
    @sheet.merge_range 'D8:E8', "Invoice No.: #{@record.invoice}", format_body2
    @sheet.merge_range 'A9:C9', "Phone: #{@record.phone}", format_body2
    @sheet.merge_range 'D9:E9', "Date: #{@record.created_date}", format_body2
    @sheet.merge_range 'A10:C10', repacle_address("Address: #{@record.address}"), format_body2
    @sheet.merge_range 'D10:E10', "PO#: #{@record.po}", format_body2
    @sheet.merge_range 'A11:C11', "Attention: #{@record.to}", format_body2
    @sheet.merge_range 'D11:E11', "Order ID: #{@record.order_uuid}", format_body2
  end

  def table_info
    @sheet.write_string(@index, 0, 'NO.',format_header_table)
    @sheet.write_string(@index, 1, 'Chemical Name',format_header_table)
    @sheet.write_string(@index, 2, 'Catalogue No.',format_header_table)
    @sheet.write_string(@index, 3, 'Qty',format_header_table)
    @sheet.write_string(@index, 4, 'Price (USD)',format_header_table)
    @index = @index+1
    @record.body.each_with_index do |item, index|
      @sheet.write_string(@index, 0, (index + 1).to_s, format_table_body)
      if item[:chemical_name].length < 25
          @sheet.set_row(@index, 20)
      elsif  item[:chemical_name].length < 40 && item[:chemical_name].length > 25
        @sheet.set_row(@index, 45)
      else
        @sheet.set_row(@index, 60)
      end
      @sheet.write_string(@index, 1, item[:chemical_name],format_table_body)
      @sheet.write_string(@index, 2, item[:calalogue_no], format_table_body)
      @sheet.write_string(@index, 3, item[:qty], format_table_body)
      @sheet.write_string(@index, 4, "US$" + item[:price].to_s, format_table_body)
      @index = @index+ 1
    end
    @index = @index+ 1
    @sheet.write_string(@index-1, 4, "Shipping Fee: US$#{@record.shipping_fee}", format_body)
    @sheet.merge_range "A#{@index+1}:D#{@index+1}", "", format_body2
    @sheet.merge_range "A#{@index}:D#{@index}", "", format_body2
    @sheet.write_string(@index, 4, "Handling Fee: US$#{@record.handling_fee}", format_body) if @record.handling_fee.to_f > 0
    @sheet.write_string(@index, 4, "Bank Charge: US$#{@record.bank_fee}", format_body) if @record.bank_fee.to_f > 0
    @sheet.merge_range "A#{@index+2}:D#{@index+2}", '', format_body2
    @sheet.write_string(@index+1, 4, "Total: US$#{@record.total_price}", format_body)
    @sheet.merge_range "A#{@index+3}:E#{@index+3}", "Payment Terms: #{@record.payment_method}", format_body2
    @sheet.merge_range "A#{@index+4}:E#{@index+4}", "Overdue payment may defer new quotation and order by aa.", format_body2
    @sheet.merge_range "A#{@index+5}:E#{@index+5}", "NOTE:Please reference aa catalog number or account email address on all Bank Transfers so we may properly credit your account.Client is responsible for banking charges.
", format_body2
    @sheet.set_row(@index+4, 40)
  end

  def bank_info
    @sheet.merge_range("A#{@index + 6}:E#{@index + 6}", 'Payment Methods:', format_body2)
    @sheet.merge_range("A#{@index + 7}:E#{@index + 7}", 'BANK TRANSFER', format_body2)
    @sheet.merge_range("A#{@index + 8}:E#{@index + 8}", 'Beneficiary Bank: CHINA CONSTRUCTION BANK, SHANGHAI CHEMICAL DISTRICT BRANCH', format_body2)
    @sheet.merge_range("A#{@index + 9}:E#{@index + 9}", 'Bank Address: 201 MUHUA ROAD, FENGXIAN DISTRICT, SHANGHAI, CHINA', format_body2)
    @sheet.merge_range("A#{@index + 10}:E#{@index + 10}", 'Beneficiary Name/ Account Name: SHANGHAI aa CO., LTD.', format_body2)
    @sheet.merge_range("A#{@index + 11}:E#{@index + 11}", 'Beneficiary Address/Account Address: 3316 TINGWEI ROAD, JINSHAN, SHANGHAI, CHINA', format_body2)
    @sheet.merge_range("A#{@index + 12}:E#{@index + 12}", 'ACCT#: 3105 0169 5200 0000 0652', format_body2)
    @sheet.merge_range("A#{@index + 13}:E#{@index + 13}", 'Swift Code: PCBCCNBJSHX', format_body2)
  end

  def set_rows
    ( (1..50).to_a ).each do |i|
      @sheet.set_row(i, 20, nil, 0, 4, 0)
    end
    @sheet.set_row(0, 30)
    @sheet.set_row(9, 40)
    @sheet.set_row(1, 30)
  end

  def repacle_address(address)
    address.index("\r\n").nil? ? address : address.gsub!("\r\n", "  ")
  end


  def image_path
    Rails.root + 'public/coa_logo1.png'
  end

  def format_header_table
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'vcenter',
      size: 10,
      bold: 1,
      bg_color: 'silver',
      font: 'Times New Roman'
    )
  end

  def format_header
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'bottom',
      size: 16,
      bold: 1,
      bottom: 0,
      border: 1,
      border_color: 'white',
      font: 'Times New Roman'
    )
  end

  def format_header_center
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'bottom',
      size: 16,
      bold: 1,
      border: 1,
      border_color: 'white',
      underline: 3,
      font: 'Times New Roman'
    )
  end

  def format_body
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'vcenter',
      size: 10,
      bold: 1,
      border: 1,
      border_color: 'white',
      font: 'Times New Roman'
    )
  end

  def format_table_body
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'vcenter',
      size: 10,
      bold: 1,
      border: 1,
      border_color: 'gray',
      font: 'Times New Roman'
    )
  end

  def format_blank
    @book.add_format(
      text_wrap: 1,
      align: 'center',
      valign: 'vcenter',
      size: 10,
      bold: 1,
      top: 1,
      top_color: 'gray',
      left: 1,
      left_color: 'white',
      right: 1,
      right_color: 'white',
      font: 'Times New Roman'
    )
  end

  def format_body2
    @book.add_format(
      text_wrap: 1,
      align: 'left',
      valign: 'vcenter',
      size: 10,
      bold: 1,
      border: 1,
      border_color: 'white',
      font: 'Times New Roman'
    )
  end

  def render
    set_rows
    default_text
    company_info
    table_info
    bank_info
    @sheet.paper = 9
    @book.close
    @io.string
  end

  def file_name
    "aa Invoice-#{@record.order_uuid}-#{rand(99999)}.xlsx"
  end

end

0
0
分享到:
评论

相关推荐

    应用Rails进行敏捷Web开发&#40;中文版&#41;第三版.part1

    应用Rails进行敏捷Web开发&#40;中文版&#41;第三版.part1.ra

    Ruby-GoOnRails使用Rails生成器来生成一个Golang应用

    这个工具简化了在Rails项目中使用Go进行后端处理的流程,使开发者可以利用Go的性能优势处理高并发请求,同时保留Rails的便捷开发体验。 ### 2. 安装`GoOnRails` 首先,确保你的系统已经安装了Rails和Go。接下来,...

    rails2.3.8 && ruby1.8.7

    标题中的"rails2.3.8 && ruby1.8.7"指的是Ruby on Rails框架的2.3.8版本和Ruby编程语言的1.8.7版本。Ruby on Rails(通常简称为Rails)是一个基于Ruby语言的开源Web应用程序框架,它遵循模型-视图-控制器(MVC)架构...

    Ruby on Rails环境搭建(基于WinXP & RadRails)

    此外,了解ActiveRecord(用于数据库交互)、Scaffold(快速生成基本CRUD操作)以及Rails的路由配置也是必要的。 总之,搭建Ruby on Rails环境需要安装Ruby、Rails、数据库驱动以及一个合适的IDE。在这个案例中,...

    rails-learning, 学习 Rails & ruby的最佳方法.zip

    rails-learning, 学习 Rails & ruby的最佳方法 Ruby on Rails从头开始学习东西几乎总是一个艰苦的事情,你根本不知道从哪里开始,或者不从开始开始,把东西踢到什么地方。 你不必担心找到最佳的资源,排序坏的资源,...

    rails上传文件_paperclip

    在Ruby on Rails框架中,Paperclip是一个非常流行的用于处理文件上传的库。它提供了一种简单而优雅的方式来管理和处理模型中的附件,如图片、文档等。Paperclip与ActiveRecord紧密集成,使得在Rails应用中添加文件...

    rails 文件上传

    它提供了一个简单的接口来处理文件上传,包括存储位置、版本管理和缩略图生成等。 6. **Paperclip**: Paperclip是另一个流行的文件上传gem,但已不再维护,推荐使用ActiveStorage。如果你正在使用Paperclip,可能...

    Rails中上传文件保存中文文件名乱码

    在Rails框架中处理文件上传时,经常会遇到一个问题,那就是当用户尝试上传包含中文名称的文件时,文件名可能会出现乱码。这个问题主要是由于字符编码不兼容导致的。Rails默认使用UTF-8编码,但文件系统或者某些外部...

    rails学习教程

    Rails的scaffolding功能能快速生成一个基本的CRUD(创建、读取、更新、删除)应用界面,是快速原型开发的好帮手。 九、Rails的安全性 Rails提供了许多安全特性,如CSRF防护、XSS防护和参数过滤。了解这些安全措施并...

    在rails中 使用RSpec生产CHM文档

    标题 "在Rails中使用RSpec生产CHM文档" 暗示了这个话题是关于如何在Ruby on Rails(简称Rails)框架中使用RSpec测试工具来创建帮助文档,特别是以CHM(Microsoft Compiled HTML Help)格式。CHM文件是一种常见的...

    unity插件 Tracks and Rails.unitypackage 铁路轨道生成

    网格是自动生成的。包括四个跟踪模板。用这些或者自己做。使用任何你想要的模型,或只是把相机放在它和去兜风! 是基于物理的相互作用。这辆车有一个刚体,可以像其他刚体一样被推、拉、撞、跳和加速。轨道本身可以被...

    Rails101_by_rails4.0

    作者特别提到了“CRUD懶人大法Scaffold”,它是一种通过Rails自动生成代码的方式来快速搭建基本的CRUD操作,这大大简化了开发流程,使得开发者可以将更多的精力放在业务逻辑的实现上。 此外,书中还介绍了一些Rails...

    rails指南 中文版

    6. **Scaffolding**:Rails提供了快速生成基本CRUD(Create, Read, Update, Delete)操作的命令行工具,可以自动生成控制器、视图、样式表和测试文件,方便快速搭建原型。 7. **Testing**:Rails强调测试驱动开发,...

    Ruby-简单的CLI利用Docker生成和运行Rails的环境

    简单的CLI利用Docker生成和运行Rails的环境

    使用sitemap_generator来为rails网站生成Sitemap

    此外,`sitemap_generator` gem 还支持动态Sitemap,这意味着它可以处理动态内容,如用户生成的内容或基于时间的博客文章。只需在Sitemap配置中添加相应的URL生成规则即可。 总之,`sitemap_generator` 是Rails...

    Rails相关电子书汇总

    6. **Scaffold**:快速开发工具,可以自动生成用于CRUD(创建、读取、更新、删除)操作的基本控制器、模型和视图。 7. **Gemfile和Bundler**:Rails项目通过Gemfile管理依赖,Bundler工具确保在开发环境中安装正确...

    rails-1.0.0

    8. **脚手架**:Rails的scaffold命令可以自动生成基本的CRUD(创建、读取、更新、删除)功能,方便快速搭建原型。 9. **测试支持**:Rails内置了测试框架,如Test::Unit和RSpec,提供模型、控制器和功能测试的支持...

    go-on-rails:使用Rails开发或生成Golang应用程序

    关于go-on-rails生成器的基本用法的简单 展示了如何将Go API集成到Rails项目中显示了如何处理Rails会话以在go-on-rails生成的Go API中获取用户的信息先决条件Rails 4.2+(不支持Rails 6,需要您的帮助) Golang ...

    Ruby on Rails Tutorial Learn Rails by Example 的源代码

    10. **Scaffolding**:在`sample_app`中,你可能会看到一些由`rails generate scaffold`命令自动生成的代码,这是一种快速构建基本CRUD(创建、读取、更新、删除)功能的方法。 11. **安全**:`sample_app`可能包含...

Global site tag (gtag.js) - Google Analytics