Maxscript和Excel:检索/更改单元格的值

时间:2013-06-12 19:54:07

标签: excel cell maxscript

我正试图通过maxscript访问单个单元格的值,而且我一直在努力弄清楚究竟是什么让我不这样做。这是我在maxscript中的内容:

-- Startup Ops
-- Generate a filename
excelFile = "<path string>\testbook.xlsx"

-- Start an Excel OLE Object
x = CreateOLEObject "Excel.Application"

-- Create a new workbook in the new excel document
x.application.Workbooks.open(excelFile)

-- This makes Excel Visible
x.visible = true



--Here's where I'm having trouble
-- Retrieve the contents of Cell A3
contents = x.ActiveSheet.Cells(3, 1).Value

-- Change the contents of Cell A3 to Hello
x.ActiveSheet.Cells(3, 1).Value = "Hello"



-- Cleanup Ops
-- Close the spreadsheet
x.application.ActiveWorkbook.Close

-- quit excel
x.quit()

-- Release the OLE Object
releaseOLEObject x

-- Release ALL OLE Objects, just in case
releaseAllOLEObjects()

这是我过去常用的msdn resource。据我所知,我把所有事情都跟进了T.如果有人能帮帮忙,我真的很感激。

2 个答案:

答案 0 :(得分:0)

我手头没有Excel,但我认为它应该是(x.ActiveSheet.Cells X Y).Value

或者您可以采用不同的路线,使用.NET和excellibrary

答案 1 :(得分:0)

我只是想在这里添加一个对我有用的解决方案。我知道这是7年前的事,我确定您已经解决了问题,但是您的代码确实对我有帮助,我在任何地方都找不到类似的东西,所以在这里:

--Here's where I'm having trouble
-- Retrieve the contents of Cell A3
contents = x.ActiveSheet.Cells(3, 1).Value

-- Change the contents of Cell A3 to Hello
x.ActiveSheet.Cells(3, 1).Value = "Hello"

我用过:

contents = x.application.cells 3 1  
contents.value = "Hello"