How create XLSX file from LibXML::XML::Document in Ruby? - ruby-on-rails

My code prepares a string containing XML data in UTF-8 encoding. I use LibXML to create it and finally I call Rails send_data which creates some.xls file from the prepared string. MS Excel perfectly opens the some.xls file, but it's the only application which can open an XML file in table format.
Does anybody know how to create an XLSX file from LibXML::XML::Document? I need to create a spreadsheet at once,not cell by cell.
I checked some gems like XlsxWriter,etc. However, I found the only examples use methods writing into a cell or a row o a column, but I need to create a file at once.

Take a look the axlsx gem, I think this will help you.
https://github.com/randym/axlsx

Related

Issue with line breaks in CSV text files generated by rails

I generate a CSV text file in Rails like this:
CSV.generate(col_sep: ';') do |csv|
sheet.add_row ['1st line']
sheet.add_row ['2nd line']
end
When I open the text file the two lines are there as expected. Unfortunately this file now should be used by a program that reads the file and I get an error message, that the second line is missing. I have a sample file that looks exactly like the file I generated which works fine but my file can't be read properly. It also has the same encoding. Any suggestions where to look? Anything concerning line breaks?
I'm not sure this is a question that can be answered as asked. You said that a 3rd party program is having trouble reading a text file generated by Ruby, but provided no information on that error and how you think Ruby is related to this error.
Could you please update your original post with the plaintext version of your CSV file and what program you're trying to open it in?

How to create Trailer Record in Flat File using SSIS

I am looking for some help on how to create a trailer record in a flat file using SSIS, I have create a SSIS package that creates a custom header and loads other record from the database into the flat file, it is a fixed width flat file. Now at the end of the file I want to create a Trailer Record along with some static text and Record count. I tried looking on to google but could not get any good example. Any help is much appreciated.
Use a Script Task. Take the file path of the fixed width flat file that you have obtained as a input variable. Once withing the script task use the .Net coding to append the data that you need. I have written a post on it - https://karbimantras.wordpress.com/2016/08/12/adding-record-count-to-flat-file/

How to ignore hidden sheets when importing xlsx files with rails and roo

I am using Roo to import xlsx files into my rails app. The imports work fine, however, while trying to make a 'workbook' importer instead of just a 'worksheet' importer, I noticed that there are tons of hidden sheets on some of the files. For example:
In some of the files the SUB_LABOR sheet has important data that should be imported. These are not hidden. In other files, the SUB_LABOR was used as a scratch pad and then hidden so that people using the sheet will not use it.
I would like my importer to read in the workbook and parse the sheets that are not hidden and ignore the ones that are. I see that the 'hidden' value is stored in the excelx object under <Nokogiri::XML::Attr:[a hex value] name="state" value="hidden">
Is there a way to dig this information out of the object and act on it?
The whole object is way to big to post here.
You can pass in
only_visible_sheets: true
to the initializer, e.g.:
Roo::Excelx.new("my.xlsx", only_visible_sheets: true)

Write in an existing Excel .xls file which contains macros

I want to insert datas an existing Excel (.xls) file with Ruby under Linux. This file has already data, it has some format properties and it contains macros.
I tried to insert data into the file with the spreadsheet gem but when I save modifications, the format and all the macros of the file are lost.
Here's an example of a simple modification where I meet this problem :
book = Spreadsheet.open('myOriginalFile.xls')
sheet = book.worksheet 0
sheet.write('C12','hello')
book.write('myModifiedFile.xls')
I tried lots of things, did research on forums and the web but I didn't find a solution...
Does anyone has an idea?
I found a solution :
I use the POI library of Apache which is written in java with the rjb gem (Ruby Java Bridge, which allows to use java libraries with ruby). POI allows to keep macros and formulas of existing xls file and to modify it.
For those who need, here's how to set up rjb to use POI :
# JVM loading
apache_poi_path = File.dirname(__FILE__)+'/poi-3.8/poi-3.8-20120326.jar'
Rjb::load("#{apache_poi_path}", ['-Xmx512M'])
# Java classes import
#file_class = Rjb::import('java.io.FileOutputStream')
#workbook_class = Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook')
#poifs_class = Rjb::import('org.apache.poi.poifs.filesystem.POIFSFileSystem')
Rjb::import('org.apache.poi.hssf.usermodel.HSSFCreationHelper')
Rjb::import('org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator')
#cell_reference_class = Rjb::import('org.apache.poi.hssf.util.CellReference')
#cell_class = Rjb::import('org.apache.poi.hssf.usermodel.HSSFCell')
# You can import all java classes that you need
# Java classes utilisation :
#file_input_class = Rjb::import('java.io.FileInputStream')
#file_input = #file_input_class.new(model_file_path)
#fs = #poifs_class.new(#file_input)
#book = #workbook_class.new(#fs)
#worksheet = #book.getSheet('worksheet')
# ...
# You can use your objects like in Java but with a ruby syntax
You need to write the modified file to a new file name. Check out this
If you have more than one sheet, you need to rewrite other sheets
XLS with several sheets but only modify one of the sheets (and don't
touch the other data), there is no way, that spreadsheet "remembers"
what is in the other sheets. You will have to write the unmodified
sheets as well or otherwise unexpected things will happen.
Ergo: Write the modified sheet and also write the complete unmodified
sheets again, when modifying an XLS with spreadsheet with several
sheets.
Might want to check out Axlsx not sure if it will be able to edit a plain .xls, but I did some work with it a few weeks ago, it worked wonders for the xlsx I was working with.
You just need to open existing file, write your changes into file and save it with another name.
For example on server you have template.xls file.
Simple working example ( need to have template.xls near your .rb file ):
#edit_xls.rb
require 'rubygems'
require 'spreadsheet'
book = Spreadsheet.open 'template.xls'
sheet = book.worksheet 0
sheet[0,0] = 'qweqeqw'
book.write 'edited.xls'

Modify a csv file with a Ruby script

I have a .xlsx file converted to .csv.I need to write a script to modify this file(change/rename columns etc.) How can I open this .csv file and save it from within the script?
Thanks!
Open the csv file just like you would open any other file in ruby using the standard File api
csv_file = File.open('data.csv', 'r')
Parse it manually or use a library like FasterCSV. Make your modifications, writeback to the file and close. There is nothing inherently special about a csv file, work with it like you would with any file in ruby.
You should proably work with a CSV library (or in the ruby world a gem). So install the gem,
and your code will look something like this:
FasterCSV.foreach("path/to/file.csv") do |row|
# use row here...
end
http://fastercsv.rubyforge.org/
As far as I know, you cannot make inline modifications to the CSV file. You would have to output via another file.

Resources