使用http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/classes/WIN32OLE.html作为指南,我编写了以下内容:
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 电子表格时,它没有改变。有任何想法吗?