如何选中表格并改变数据范围?
打开一个已存在的excel表格,选中其中的表格,然后改变它的数据范围,但是出错了,
弹出一个窗口说“未知名称”,不知道代码哪里有问题?
Variantexcel_app,work_book,work_sheet;
try
{
excel_app = Variant::CreateObject("Excel.Application");
}
catch(...)
{
return;
}
excel_app.OlePropertySet("visible",false);
//打开已经存在的excel文件
excel_app.OlePropertyGet("WorkBooks").OleProcedure("Open","template.xls");
work_book = excel_app.OlePropertyGet("ActiveWorkBook");
work_sheet = excel_app.OlePropertyGet("Sheets",1);
//选中表格,改变数据范围
Variant chart = work_sheet.OlePropertyGet("ChartObjects","Chart 1");
Variant range = work_sheet.OlePropertyGet("Range","A1:D100");
chart.OleProcedure("SetSourceData",range,2); //弹出窗口 "未知名称"
//保存为别的excel文件
work_book.OleProcedure("SaveAs","output.xls");
work_book.OleProcedure("Close"); //关闭表格
excel_app.OleFunction("Quit");
[解决办法]
OLE专业户来也!!!
Variant chart = work_sheet.OlePropertyGet("ChartObjects","Chart 1");
Variant range = work_sheet.OlePropertyGet("Range","A1:D100");
chart.OleProcedure("SetSourceData",range,2); //弹出窗口 "未知名称"
这三行改成:
work_sheet.OleFunction("ChartObjects", "Chart 1").OleProcedure("Select");
Variant range = work_sheet.OlePropertyGet("Range","A1:D100");
excel_app.OlePropertyGet("ActiveChart").OleProcedure("SetSourceData", range, 2);
即可。