BCB中多个listview分SHEET导出到一个EXCEL中报错
报错为这里sysvari.h文件中的这个函数
Variant Variant::OlePropertyGet(const String& name, P1 p1)
{
TAutoArgs<1> args;
args[1] = p1;
return OlePropertyGet(name, static_cast<TAutoArgsBase*>(&args));//这里报错
}
报的错是“raised exception class EOleSysError with message'发生意外。',Process stopped,Use Step or Run to continue."
我的listview导出到EXCEL的函数内容如下
i_cols1 = lv1->Columns->Count;
i_rows1 = lv1->Items->Count;
i_cols2 = lv2->Columns->Count;
i_rows2 = lv2->Items->Count;
i_cols3 = lv3->Columns->Count;
i_rows3 = lv3->Items->Count;
i_cols4 = lv4->Columns->Count;
i_rows4 = lv4->Items->Count;
//路径
TSaveDialog* sd = new TSaveDialog(NULL);
sd->DefaultExt = "xls";
sd->Filter = "*.xls";
__try
{
if(s_path_to_save.Trim().Length())
sd->FileName = s_path_to_save;
if(sd->Execute())
{
s_path_to_save = sd->FileName;
//验证文件是否已存在
if(FileExists(s_path_to_save) && MessageBox(NULL, "文件已存在,覆盖原文件吗?", "提示", MB_ICONQUESTION|MB_YESNO)!=ID_YES)
return;
}
else
return;
}
__finally
{
delete sd;
}
//扫描文档的列和行
Variant Ex, wb, st1, st2, st3, st4, range;
try
{
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
return;
}
Ex.OlePropertySet("Visible",true);
Ex.OlePropertySet("DisplayAlerts", false);
Ex.OlePropertyGet("Workbooks").OleFunction("Add"); // 工作表
//选择工作表1
Ex.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1).OleProcedure("Select");
wb = Ex.OlePropertyGet("ActiveWorkBook");
st1 = wb.OlePropertyGet("ActiveSheet");
st1.OlePropertySet("Name","A");
//写标题 并加粗 锁定
for(int i=1; i<=i_cols1; i++)
{
st1.OlePropertyGet("Cells", 1, i).OlePropertySet("Value", lv1->Columns->Items[i-1]->Caption.c_str());
st1.OlePropertyGet("Cells", 1, i).OlePropertyGet("Font").OlePropertySet("Bold", true);
}
//选中
st1.OlePropertyGet("Rows", 2).OleProcedure("Select");
//冻结窗格
Ex.OlePropertyGet("ActiveWindow").OlePropertySet("FreezePanes", true);
//开始写数据内容
for(int i=0; i<i_rows1; i++)
{
st1.OlePropertyGet("Cells", i+2, 1).OlePropertySet("NumberFormatLocal", "@");
st1.OlePropertyGet("Cells", i+2, 1).OlePropertySet("Value", lv1->Items->Item[i]->Caption.c_str());
for(int j=0; j<i_cols1-1; j++)
{
st1.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("NumberFormatLocal", "@");
st1.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("Value", lv1->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
//选择工作表2
Ex.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 2).OleProcedure("Select");
wb = Ex.OlePropertyGet("ActiveWorkBook");
st2 = wb.OlePropertyGet("ActiveSheet");
st2.OlePropertySet("Name","B");
//写标题 并加粗 锁定
for(int i=1; i<=i_cols2; i++)
{
st2.OlePropertyGet("Cells", 1, i).OlePropertySet("Value", lv2->Columns->Items[i-1]->Caption.c_str());
st2.OlePropertyGet("Cells", 1, i).OlePropertyGet("Font").OlePropertySet("Bold", true);
}
//选中
st2.OlePropertyGet("Rows", 2).OleProcedure("Select");
//冻结窗格
Ex.OlePropertyGet("ActiveWindow").OlePropertySet("FreezePanes", true);
//开始写数据内容
for(int i=0; i<i_rows2; i++)
{
st2.OlePropertyGet("Cells", i+2, 1).OlePropertySet("NumberFormatLocal", "@");
st2.OlePropertyGet("Cells", i+2, 1).OlePropertySet("Value", lv2->Items->Item[i]->Caption.c_str());
for(int j=0; j<i_cols2-1; j++)
{
st2.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("NumberFormatLocal", "@");
st2.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("Value", lv2->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
//选择工作表3
Ex.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 3).OleProcedure("Select");
wb = Ex.OlePropertyGet("ActiveWorkBook");
st3 = wb.OlePropertyGet("ActiveSheet");
st3.OlePropertySet("Name","C");
//写标题 并加粗 锁定
for(int i=1; i<=i_cols3; i++)
{
st3.OlePropertyGet("Cells", 1, i).OlePropertySet("Value", lv3->Columns->Items[i-1]->Caption.c_str());
st3.OlePropertyGet("Cells", 1, i).OlePropertyGet("Font").OlePropertySet("Bold", true);
}
//选中
st3.OlePropertyGet("Rows", 2).OleProcedure("Select");
//冻结窗格
Ex.OlePropertyGet("ActiveWindow").OlePropertySet("FreezePanes", true);
//开始写数据内容
for(int i=0; i<i_rows3; i++)
{
st3.OlePropertyGet("Cells", i+2, 1).OlePropertySet("NumberFormatLocal", "@");
st3.OlePropertyGet("Cells", i+2, 1).OlePropertySet("Value", lv3->Items->Item[i]->Caption.c_str());
for(int j=0; j<i_cols3-1; j++)
{
st3.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("NumberFormatLocal", "@");
st3.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("Value", lv3->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
//选择工作表4
Ex.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 4).OleProcedure("Select");//单步调试到这里报错
wb = Ex.OlePropertyGet("ActiveWorkBook");
st4 = wb.OlePropertyGet("ActiveSheet");
st4.OlePropertySet("Name","D");
//写标题 并加粗 锁定
for(int i=1; i<=i_cols4; i++)
{
st4.OlePropertyGet("Cells", 1, i).OlePropertySet("Value", lv4->Columns->Items[i-1]->Caption.c_str());
st4.OlePropertyGet("Cells", 1, i).OlePropertyGet("Font").OlePropertySet("Bold", true);
}
//选中
st4.OlePropertyGet("Rows", 2).OleProcedure("Select");
//冻结窗格
Ex.OlePropertyGet("ActiveWindow").OlePropertySet("FreezePanes", true);
//开始写数据内容
for(int i=0; i<i_rows4; i++)
{
st4.OlePropertyGet("Cells", i+2, 1).OlePropertySet("NumberFormatLocal", "@");
st4.OlePropertyGet("Cells", i+2, 1).OlePropertySet("Value", lv4->Items->Item[i]->Caption.c_str());
for(int j=0; j<i_cols4-1; j++)
{
st4.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("NumberFormatLocal", "@");
st4.OlePropertyGet("Cells", i+2, j+2).OlePropertySet("Value", lv4->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
wb.OleFunction("SaveAs", s_path_to_save.c_str());
return;
各位前辈,这要如何解决呀?谢谢!