首页 > 解决方案 > xlsx中的文件加密密码

问题描述

目前,我正在使用 gem caxlsx 生成我的 excel'

有没有人在生成excel文件之前尝试提交密码?

这里的目标。是用户点击按钮然后会弹出一些文本来输入excel的密码。然后系统会在浏览器中自动生成excel下载。

标签: ruby-on-railsexcelrubyruby-on-rails-5axlsx

解决方案


查看文档,gem 支持密码保护表。

require 'axlsx'

p = Axlsx::Package.new
wb = p.workbook

s = wb.styles
unlocked = s.add_style locked: false

wb.add_worksheet(name: 'Sheet Protection') do |sheet|
  sheet.sheet_protection do |protection|
    protection.password = 'fish'
    protection.auto_filter = false
  end

  sheet.add_row [1, 2, 3], style: unlocked # These cells won't be locked
  sheet.add_row [4, 5, 6]
  sheet.add_row [7, 8, 9]

  # Set up auto filters
  sheet.auto_filter = 'A1:C3'
end

p.serialize 'sheet_protection_example.xlsx'

请参阅https://github.com/caxlsx/caxlsx/blob/master/examples/sheet_protection_example.md


推荐阅读