使用TCOM在excel中指定单元格

时间:2012-04-06 07:08:06

标签: excel tcl

命令$ cells项目2 A {SomeText}不会在单元格A2中设置文本,而是在单元格A1中指定文本。任何想法为什么它的表现如此???

              set application [::tcom::ref createobject "Excel.Application"]
              set XlFileFormat(xlCSV) [expr 6]
              set workbooks [$application Workbooks]
              set workbook [$workbooks Add]
              $application DisplayAlerts False
              set worksheets [$workbook Worksheets]
              set worksheet [$worksheets Item [expr 1]]
              set cells [$worksheet Cells]
            $cells Item 2 A {Range}


              $workbook SaveAs {c:\test.csv} $XlFileFormat(xlCSV)      
              $application Quit

1 个答案:

答案 0 :(得分:1)

好的,这里有一个解释:当保存到CSV时,Excel似乎只保存了(虚拟化的)工作表网格中形成“边界框”的数据。换句话说,您没有修改第一行单元格中的任何数据,因此不会保存。

这个简单的程序(JScript for WSH):

var app = new ActiveXObject("Excel.Application");
var wbs = app.Workbooks;
var wb  = wbs.Add;
var wss = wb.Worksheets;
var ws  = wss.Item(1);
var cells = ws.Cells;

app.DisplayAlerts = false;

cells.Item(2, "B") = "B2";
cells.Item(2, "C") = "C2";
cells.Item(3, "B") = "B3";
cells.Item(3, "C") = "C3";

wb.SaveAs("foo.csv", 6);
app.Quit();

设置四个单元格,这些单元格排列在一个矩形块中,从工作表的左上角偏移一列和一行,生成此CSV文件:

B2,C2
B3,C3