品牌咨询
联系方式
公司地址
苏州工业园区生物纳米园A4#216
联系电话
4000-520-616 / 18915418616
传真号码
0512-67156496
电子邮箱
info@ebiomall.com
公司网址
https://www.ebiomall.com

rails处理上传读取excell&生成excell - jamst - ITeye博客

作者: 时间:2024-09-20 点击量:

#文件格式要求为.xlsx格式。另外需要编辑excell的时候建议使用gem \\\'write_xlsx\\\'或gem \\\'axlsx\\\'上传导入excelldef 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 @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) rescue @import[:failed] row next end@message[:color] = \\\"#00DD00\\\" @message[:detail] = \\\"产品导入成功\\\" endrender \\\"import_product\\\" 导出excell:xls def export_product @report_name =\\\"#{@tag.name_cn}目录产品\\\" @columns = %w{产品id 分类英文名称 产品cas} @base_sql = \\\"SELECT chemicals.id,chemical_tags.name,chemicals.casFROM chemicals_tags as ca_tsleft join `chemical_tags` on chemical_tags.id = ca_ts.chemical_tag_idleft join chemicals on chemicals.id = ca_ts.chemical_idWHERE (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\\\" --------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.newfile.write xls_reportxls_report.set_encoding(\\\'UTF-8\\\') xls_report.string 写法二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 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 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 : \\\"\\\")

>>> 更多资讯详情请访问蚂蚁淘商城

Caltag商品列表
图片/货号 产品名/品牌 价格/货期 操作