使用Mata xl()合并Excel中的单元格

时间:2018-08-15 20:41:43

标签: stata

我有许多使用Stata命令putexcelexport excel创建的Excel表。

保存后,我需要格式化许多表格。由于文件大小,我尝试使用mata进行格式化,而不是使用putexcel

这很好用,除了当我尝试将两个单元合并为一个单元时:

*Build sample matrix
set obs 10
foreach i of numlist 1/10{
    gen var`i' = _n + `i'
}

mkmat var1-var10, matrix(m)

*To Excel
quietly putexcel set "Test_Merge.xlsx", sheet("Sheet1") modify
quietly putexcel A1=matrix(m) 

*Prep for mata
mata: b = xl()
mata: b.load_book("Test_Merge.xlsx")

*Left-align some cells
mata: cols = (1,2)
mata: b.set_horizontal_align(1, cols, "left")

*Center some cells
mata: cols = (4,5)
mata: b.set_horizontal_align(1, cols, "center")

*Merge some cells
mata: cols = (7,8)
mata: b.set_horizontal_align(1, cols, "merge")

使用putexcel命令,我将执行以下操作:

quietly putexcel (G1:H1), merge

但是,在mata版本中,“合并”选项似乎只是将指定的单元格居中,而不是将它们合并在一起。

我误解了b.set_horizontal_align()中的“合并”选项吗?

1 个答案:

答案 0 :(得分:1)

您需要改用mata函数b.set.sheet.merge()

clear

*Build sample matrix
set obs 10
foreach i of numlist 1/10{
    gen var`i' = _n + `i'
}

mkmat var1-var10, matrix(m)

*To Excel
quietly putexcel set "Test_Merge.xlsx", sheet("Sheet1") modify
quietly putexcel A1=matrix(m) 

*Prep for mata
mata: b = xl()
mata: b.load_book("Test_Merge.xlsx")

*Left-align some cells
mata: cols = (1,2)
mata: b.set_horizontal_align(1, cols, "left")

*Center some cells
mata: cols = (4,5)
mata: b.set_horizontal_align(1, cols, "center")

*Merge some cells
mata: rows = (1,1)
mata: cols = (7,8)
mata: b.set_sheet_merge("Sheet1", rows, cols)

请注意以粗体显示的更改,这些更改将功能应用于两个单元格。