Basic practice at create an excel file and set the format in the excel file.


require 'win32ole'

#Open excel application
excel = WIN32OLE.new("excel.application")
WIN32OLE.const_load(excel)
excel.visible = false

#Add worksheet.
workbook =excel.workbooks.add
worksheet = workbook.Worksheets(1)

#Define the format of the cell
worksheet.range("a1:z200").Interior"ColorIndex" = 2 #Define the interior line color in [a1:z200]
worksheet.range("a1:c1")'Value' = "Tests", "Number", "%"
#worksheet.range("a1:c1").value = "Tests", "Number", "%"

worksheet.range("b1:c1, a7").HorizontalAlignment = WIN32OLE::XlRight # Right horizontal Alignment of the text.
worksheet.range("a1:c1, a7:f7").Font"ColorIndex" = 2 #Define the font color
worksheet.range("a1:c1, a4:c4, a7:f7").Font.Bold = true # Define the font bold
worksheet.range("c2, c3, c4").NumberFormat = "0.00%" # Format the number.
worksheet.columns("a:f").AutoFit # Define the format of columns alignment.

#Define the border of the cell.
worksheet.range("a1:c4").Borders"ColorIndex" = WIN32OLE::XlAutomatic
worksheet.range("a1:c4").Borders"LineStyle" = WIN32OLE::XlEdgeRight
worksheet.range("a1:c4").Borders'LineStyle' = WIN32OLE::XlEdgeTop
worksheet.range("a1:c4").Borders'LineStyle' = WIN32OLE::XlEdgeBottom
worksheet.range("a1:c4").Borders'LineStyle' = WIN32OLE::XlEdgeLeft
worksheet.range("a1:c4").Borders'Weight' = WIN32OLE::XlMedium
worksheet.range("a1:c4").Borders'LineStyle' = WIN32OLE::XlContinuous
worksheet.range("a1:c4").Borders(WIN32OLE::XlInsideVertical).LineStyle = WIN32OLE::XlNone
worksheet.range("a1:c4").Borders(WIN32OLE::XlInsideHorizontal).LineStyle = WIN32OLE::XlNone

workbook.saveas(dir + Time.now.strftime("%d%m%Y%H%M")+".xls") # Save the file with an random name.

#Save the sheet and close the excel application.
workbook.Save()
workbook.close
excel.Quit

1. Open the excel file.

excel = WIN32OLE.new("excel.application")
excel.visible = false
workbook = excel.workbooks.open(filepath)
worksheet=workbook.worksheets(1)
#worksheet=workbook.worksheets("sheet name")

2. Get all the data in a line until the line is empty.

data=[]
row=10
while worksheet.range("a#{row}").value
   data << worksheet.range("a#{row}").value.to_s
   row+=1
end
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.