简体   繁体   中英

Rails Gem “Axlsx” / “Rails-Axlsx” - Adding Image

I try to add one image to my workbook/worksheet.

The Code :

    wb.add_worksheet(:name => "Doc1", :page_setup => setup, :print_options => options) do |sheet|

        img = File.expand_path('../logo.jpg', __FILE__)
        sheet.add_image(:image_src => img, :noMove => true) do |image|
            image.width = 7
            image.height = 6
            image.start_at 2, 2
        end

    ...

    end

But if i open the document there is no image on the worksheet, how can i fix this ?

And how is the correct path to the image? For this test i copy the jpg in my view folder, but usually all images in "app/assets/images/logo.jpg" . I try it with img = File.expand_path('../assets/images/logo.jpg', __FILE__) , but it fails with "No file found!"

https://pramodbshinde.wordpress.com/2013/12/29/design-spreadsheets-using-axlsx-in-rails/上的示例显示以下代码行

img = File.expand_path(Rails.root+'app/assets/images/result.png')

Here is another way you can achieve this:

Code:

img = File.open(Dir.glob("#{Rails.root}/app/assets/images/result.png, "r")

In the lines where you set the image width and height you have them very small and you could be overlooking the image as this number is in pixels. Try setting them to higher number.

the following worked for me:

img = File.expand_path(Rails.root+'app/assets/images/result.png')
        sheet.add_image(:image_src => img, :noMove => true) do |image|
            image.width = 100
            image.height = 150
            image.start_at 2, 2
        end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM