将 Ruby on Rails 应用程序中的数据导入 Excel 电子表格的最佳方法是什么?

发布于 2024-07-16 03:02:53 字数 279 浏览 9 评论 0原文

我知道您在想什么,只需将数据下载为 CSV 并导入 Excel 即可。

不幸的是,我们需要的是用户从 Rails 应用程序中选择一些数据,然后轻松地(即尽可能少的用户干预 - 最好按一个按钮)将其导出,并在包含各种公式和数据的现有 Excel 电子表格中打开它。宏等,将应用于数据。

我理想的解决方案将涉及 Rails 端的大部分繁重工作,因为这就是我的技能所在,但我想可能有某种 Excel Web 集成内容可能会有所帮助。

无论如何,我可以提供一些关于解决此问题的好方法的建议吗?

I know what you're thinking, just download the data as a CSV and import into Excel.

Unfortunately what we require is for a user to choose some data from a Rails app then easily (i.e. with as little user intervention as possible - ideally a single button press) export it, and open it in an existing Excel spreadsheet that contains various formulas and macros, etc, which will be applied to the data.

My ideal solution would involve most of the heavy lifting at the Rails end, as that's where my skills lie, but I guess there could be some sort of Excel web integration stuff that might help.

Anyway, can I have some recommendations on good approaches to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独闯女儿国 2024-07-23 03:02:53

axlsx 可能是深度 Excel 工作最完整的实现。
https://github.com/randym/axlsx

这是构建微软 skydrive 模板之一的示例:
在此处输入图像描述

require 'axlsx'

Axlsx::Package.new do |p|
  p.workbook do |wb|
    styles = wb.styles
    header = styles.add_style :bg_color => "DD", :sz => 16, :b => true, :alignment => {:horizontal => :center}
    tbl_header = styles.add_style :b => true, :alignment => { :horizontal => :center }
    ind_header = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => {:indent => 1}
    col_header  = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => { :horizontal => :center }
    label       = styles.add_style :alignment => { :indent => 1 }
    money       = styles.add_style :num_fmt => 5
    t_label       = styles.add_style :b => true, :bg_color => "FFDFDEDF"
    t_money = styles.add_style :b => true, :num_fmt => 5, :bg_color => "FFDFDEDF"

    wb.add_worksheet do |sheet|
      sheet.add_row
      sheet.add_row [nil, "College Budget"], :style => [nil, header]
      sheet.add_row
      sheet.add_row [nil, "What's coming in this month.", nil, nil, "How am I doing"], :style => tbl_header
      sheet.add_row [nil, "Item", "Amount", nil, "Item", "Amount"], :style => [nil, ind_header, col_header, nil, ind_header, col_header]
      sheet.add_row [nil, "Estimated monthly net income", 500, nil, "Monthly income", "=C9"], :style => [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Financial aid", 100, nil, "Monthly expenses", "=C27"], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Allowance from mom & dad", 20000, nil, "Semester expenses", "=F19"], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Total", "=SUM(C6:C8)", nil, "Difference", "=F6 - SUM(F7:F8)"], :style => [nil, t_label, t_money, nil, t_label, t_money]
      sheet.add_row
      sheet.add_row [nil, "What's going out this month.", nil, nil, "Semester Costs"], :style => tbl_header
      sheet.add_row [nil, "Item", "Amount", nil, "Item", "Amount"], :style => [nil, ind_header, col_header, nil, ind_header, col_header]
      sheet.add_row [nil, "Rent", 650, nil, "Tuition", 200], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Utilities", 120, nil, "Lab fees", 50], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Cell phone", 100, nil, "Other fees", 10], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Groceries", 75, nil, "Books", 150], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Auto expenses", 0, nil, "Deposits", 0], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Student loans", 0, nil, "Transportation", 30], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Other loans", 350, nil, "Total", "=SUM(F13:F18)"], :style => [nil, label, money, nil, t_label, t_money]
      sheet.add_row [nil, "Credit cards", 450], :style => [nil, label, money]
      sheet.add_row [nil, "Insurance", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Laundry", 10], :style => [nil, label, money]
      sheet.add_row [nil, "Haircuts", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Medical expenses", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Entertainment", 500], :style => [nil, label, money]
      sheet.add_row [nil, "Miscellaneous", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Total", "=SUM(C13:C26)"], :style => [nil, t_label, t_money]
      sheet.add_chart(Axlsx::Pie3DChart) do |chart|
        chart.title = sheet["B11"]
        chart.add_series :data => sheet["C13:C26"], :labels => sheet["B13:B26"]
        chart.start_at 7, 2
        chart.end_at 12, 15
      end
      sheet.add_chart(Axlsx::Bar3DChart, :barDir => :col) do |chart|
        chart.title = sheet["E11"]
        chart.add_series :labels => sheet["E13:E18"], :data => sheet["F13:F18"]
        chart.start_at 7, 16
        chart.end_at 12, 31
      end
      sheet.merged_cells.concat ["B4:C4","E4:F4","B11:C11","E11:F11","B2:F2"]
      sheet.column_widths 2, nil, nil, 2, nil, nil, 2
    end
 end
 p.use_shared_strings = true
 p.serialize 'axlsx.xlsx'
end

axlsx is probably the most complete implementation for deep excel work.
https://github.com/randym/axlsx

Here is an example that builds one of microsoft's skydrive templates:
enter image description here

require 'axlsx'

Axlsx::Package.new do |p|
  p.workbook do |wb|
    styles = wb.styles
    header = styles.add_style :bg_color => "DD", :sz => 16, :b => true, :alignment => {:horizontal => :center}
    tbl_header = styles.add_style :b => true, :alignment => { :horizontal => :center }
    ind_header = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => {:indent => 1}
    col_header  = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => { :horizontal => :center }
    label       = styles.add_style :alignment => { :indent => 1 }
    money       = styles.add_style :num_fmt => 5
    t_label       = styles.add_style :b => true, :bg_color => "FFDFDEDF"
    t_money = styles.add_style :b => true, :num_fmt => 5, :bg_color => "FFDFDEDF"

    wb.add_worksheet do |sheet|
      sheet.add_row
      sheet.add_row [nil, "College Budget"], :style => [nil, header]
      sheet.add_row
      sheet.add_row [nil, "What's coming in this month.", nil, nil, "How am I doing"], :style => tbl_header
      sheet.add_row [nil, "Item", "Amount", nil, "Item", "Amount"], :style => [nil, ind_header, col_header, nil, ind_header, col_header]
      sheet.add_row [nil, "Estimated monthly net income", 500, nil, "Monthly income", "=C9"], :style => [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Financial aid", 100, nil, "Monthly expenses", "=C27"], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Allowance from mom & dad", 20000, nil, "Semester expenses", "=F19"], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Total", "=SUM(C6:C8)", nil, "Difference", "=F6 - SUM(F7:F8)"], :style => [nil, t_label, t_money, nil, t_label, t_money]
      sheet.add_row
      sheet.add_row [nil, "What's going out this month.", nil, nil, "Semester Costs"], :style => tbl_header
      sheet.add_row [nil, "Item", "Amount", nil, "Item", "Amount"], :style => [nil, ind_header, col_header, nil, ind_header, col_header]
      sheet.add_row [nil, "Rent", 650, nil, "Tuition", 200], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Utilities", 120, nil, "Lab fees", 50], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Cell phone", 100, nil, "Other fees", 10], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Groceries", 75, nil, "Books", 150], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Auto expenses", 0, nil, "Deposits", 0], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Student loans", 0, nil, "Transportation", 30], :style =>  [nil, label, money, nil, label, money]
      sheet.add_row [nil, "Other loans", 350, nil, "Total", "=SUM(F13:F18)"], :style => [nil, label, money, nil, t_label, t_money]
      sheet.add_row [nil, "Credit cards", 450], :style => [nil, label, money]
      sheet.add_row [nil, "Insurance", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Laundry", 10], :style => [nil, label, money]
      sheet.add_row [nil, "Haircuts", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Medical expenses", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Entertainment", 500], :style => [nil, label, money]
      sheet.add_row [nil, "Miscellaneous", 0], :style => [nil, label, money]
      sheet.add_row [nil, "Total", "=SUM(C13:C26)"], :style => [nil, t_label, t_money]
      sheet.add_chart(Axlsx::Pie3DChart) do |chart|
        chart.title = sheet["B11"]
        chart.add_series :data => sheet["C13:C26"], :labels => sheet["B13:B26"]
        chart.start_at 7, 2
        chart.end_at 12, 15
      end
      sheet.add_chart(Axlsx::Bar3DChart, :barDir => :col) do |chart|
        chart.title = sheet["E11"]
        chart.add_series :labels => sheet["E13:E18"], :data => sheet["F13:F18"]
        chart.start_at 7, 16
        chart.end_at 12, 31
      end
      sheet.merged_cells.concat ["B4:C4","E4:F4","B11:C11","E11:F11","B2:F2"]
      sheet.column_widths 2, nil, nil, 2, nil, nil, 2
    end
 end
 p.use_shared_strings = true
 p.serialize 'axlsx.xlsx'
end
傲性难收 2024-07-23 03:02:53

至少导出部分相当不错 - 这里有几个候选宝石:

我怀疑(希望!)一旦你有了 XLS 形式的数据,其余的应该在客户端进行处理。

查看 Excel 的 Web 查询工具是否有任何价值? 如果用户可以定义他们的选择标准,以便不经常更改,那么自定义页面(可能只是提供一个表格)可以将数据直接放入他们的 Excel 会话中。 只是一个想法...

The export part is reasonably well covered at least - here's a couple of candidate gems:

I suspect (hope!) that once you have your data in XLS form, the rest should be plumbing on the client side.

Is there any value in looking at Excel's Web Query facility? If the user can define their selection criteria such that change is infrequent, then a custom page (perhaps just delivering a table) could drop the data straight into their Excel session. Just a thought...

悍妇囚夫 2024-07-23 03:02:53

Ryan Bates 刚刚在railscasts.com 上发布了免费的简短截屏视频
导出 CSV 和 Excel

Ryan Bates just released a free short screencast at railscasts.com on
Exporting CSV and Excel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文