使用安全电子表格在 xlsx 中添加密码 [英] Add password in xlsx using secure-spreadsheet

查看:98
本文介绍了使用安全电子表格在 xlsx 中添加密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的excel文件中输入密码

def excel_filetest = Axlsx::Package.new do |p|p.workbook.add_worksheet(:name => "Pie Chart") do |sheet|sheet.add_row [简单饼图"]%w(第一第二第三).每个{ |标签|sheet.add_row [标签,兰特(24)+1]}sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title =>示例 3:饼图") 做 |chart|chart.add_series :data =>sheet[B2:B4"], :labels =>sheet[A2:A4"], :colors =>['FF0000', '00FF00', '0000FF']结尾结尾p.serialize('simple.xlsx')结尾IO.popen("secure-spreadsheet --password secret", "r+") do |io|io.write(测试)io.close_write读取结尾结尾

在上面的这段代码中,它将在我的项目文件中生成名为 simple.xlsx 的 excel 文件.然后我想使用安全电子表格输入密码.

IO.popen"部分代码是.我不确定我正在做的那个正确的代码.目标是获取创建的 alxsx 文件,然后添加密码.

def download_excelresponse_to do |格式|format.xlsx { send_data excel_file, type: 'application/xlsx;header=present',处置:附件",文件名:output.xlsx";}结尾结尾

上面的代码将在浏览器中下载.

这里是问题的起源Rails http 响应 Donwload excel 文件

这是我使用的仓库

https://github.com/randym/axlsx

https://github.com/ankane/secure-spreadsheet

我没有使用电子表格保护.我不需要那个,因为它只锁定电子表格.我需要的是整个excel文件的密码

解决方案

深入图书馆和 文档我找到了答案.

<块引用>

保护现有的 XLSX

cat input.xlsx |安全电子表格 --password secret --input-format xlsx >输出.xlsx

你提供的代码有什么问题,它没有考虑 input-format 选项和 File.write(test) 实际编写 Axlsx::Package 到一个文件,这不是你想要的.您需要该 Axlsx::Package 的序列化内容.

解决办法

excel_filename = 'simple.xlsx'test = Axlsx::Package.new do |p|p.workbook.add_worksheet(:name => "Pie Chart") do |sheet|sheet.add_row [简单饼图"]%w(第一第二第三).每个{ |标签|sheet.add_row [标签,兰特(24)+1]}sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title =>示例 3:饼图") 做 |chart|chart.add_series :data =>sheet[B2:B4"], :labels =>sheet[A2:A4"], :colors =>['FF0000', '00FF00', '0000FF']结尾结尾p.serialize(excel_filename)结尾secure = IO.popen("secure-spreadsheet --password secret --input-format xlsx", "r+") do |io|io.write(File.read(excel_filename))io.close_write读取结尾my_new_secured_file = File.open('secured_sheet.xlsx', 'w') { |f|f.写(安全)}

secured 包含您未加密的文件(simple.xlsx)和一些元数据,以告诉 excel 这个新文件(my_new_secured_file 变量)实际上是加密的,需要密码才能打开

因此您需要将加密数据存储在 secured 变量中(因为它实际上是由您的内部 shell 调用和修改的),然后将其写入一个新文件,该文件将受到保护的 xlsx 电子表格

尝试在终端中打开

打开secure_sheet.xlsx

你会被提示输入密码secret

im try to put password in my excel file

def excel_file
  

    test = Axlsx::Package.new do |p|

      p.workbook.add_worksheet(:name => "Pie Chart") do |sheet|
        sheet.add_row ["Simple Pie Chart"]
        %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
        sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
          chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"],  :colors => ['FF0000', '00FF00', '0000FF']
        end
      end
      p.serialize('simple.xlsx')
    end


   IO.popen("secure-spreadsheet --password secret", "r+") do |io|
    io.write(test)
    io.close_write
    io.read
  end

end

in this code above it will generate excel file with name simple.xlsx in my project file. then i want to put password using secure-spreadsheet.

The "IO.popen" part of the code is. Im not sure is that right code that Im doing. the goal is get the created alxsx file then add a password.

def download_excel
  respond_to do |format|
    format.xlsx { send_data excel_file, type: 'application/xlsx; header=present', disposition: "attachment", filename: "output.xlsx"  }
  end
end

this code above will download in the browser.

here's origin of the question rails http response to Donwload excel file

here's the repo im using

https://github.com/randym/axlsx

https://github.com/ankane/secure-spreadsheet

im not using spreadsheet protection. i dont need that because it only lock the spreadsheet. what i need is a password for the entire excel file

解决方案

Digging into library and documentation I found an answer.

Protect an existing XLSX

cat input.xlsx | secure-spreadsheet --password secret --input-format xlsx > output.xlsx

What's wrong with the code you provided, it is doesn't take into consideration input-format option and File.write(test) actually writing instance of Axlsx::Package to a file, that's not what you want. You want serialized contents of that Axlsx::Package.

Here is solution

excel_filename = 'simple.xlsx'
test = Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => "Pie Chart") do |sheet|
    sheet.add_row ["Simple Pie Chart"]
    %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
    sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
      chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"],  :colors => ['FF0000', '00FF00', '0000FF']
    end
  end

  p.serialize(excel_filename)
end

secured = IO.popen("secure-spreadsheet --password secret --input-format xlsx", "r+") do |io|
  io.write(File.read(excel_filename))
  io.close_write
  io.read
end

my_new_secured_file = File.open('secured_sheet.xlsx', 'w') { |f| f.write(secured) }

secured contains your unsecured contains of file(simple.xlsx) and some metadata to tell excel that this new file (my_new_secured_file variable) is actually encrypted and need passphrase in order to open it

so you need to store encrypted data in secured variable (cause it is actually being called and modified by your internal shell) and then write it to a new file which will be secured xlsx spreadsheet

try to open in your terminal

open secured_sheet.xlsx

and you will be prompted to write a password secret

这篇关于使用安全电子表格在 xlsx 中添加密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆