4
使用http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html,因爲我寫了下面的指南:無法使用Ruby來保存Excel文件修改後與WIN32OLE
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
excel.visible = false #Setting this is 'true' doesn't reveal anything
workbook = excel.workbooks.open('C:\myspreadsheet.xlsx')
worksheet = workbook.worksheets('sheet1')
worksheet.Activate
data = worksheet.UsedRange.Value
p data.size #This works! - My spreadsheet has 3987 rows.
p data[3932] #This works, too! - I can "see" row 3932.
worksheet.Rows(3932).Insert #Insert a row above row 3932
data = worksheet.UsedRange.Value
p data.size #Returns 3988! This would seem to indicate that I've successfully added a row since it was just 3987.
workbook.saved = true #Save the workbook and quit.
excel.ActiveWorkbook.Close(0)
excel.Quit()
當我畢竟這打開Excel電子表格是不變的。有任何想法嗎?
成功!非常感謝你。 –
我在「workbook.saved = true」之後立即添加了「workbook.Save」,並且一切都很順利。 –
如果你不介意,出於好奇,我可以自己去哪裏找到這些信息?我很高興能夠在stackoverflow上得到一個答案,但是在這種情況下沒有文檔或引用的地方嗎?我看過,但唯一能找到的就是頂部的鏈接。再一次感謝你的幫助! –