在编写 qt activex excel read/write 代码时防止未安装 excel 时应用程序崩溃
Prevent application crash when excel is not installed while writing qt activex excel read/write codes
我正在使用 Qt activex (QAxObject) 以便 read/write excel 文件。
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workBook = workbooks->querySubObject("Open(const QString&)", path);
QAxObject* sheets = workBook->querySubObject( "Worksheets" );
QAxObject* sheet1 = sheets->querySubObject( "Item( int )", 1 );
QAxObject* sheet2 = sheets->querySubObject( "Item( int )", 2 );
//....
我们知道此机制使用 excel 本身,并且在未安装 excel 的计算机上不起作用。
如果代码在此类计算机上运行(未安装 excel),则程序会崩溃。如何在代码中检测到 excel 未安装在 PC 上?
您检查工作簿是否为 NULL 指针。根据经验,您可能需要在使用前检查 excel 是否为空指针。
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
if ( excel )
{
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
if ( workbooks )
{
QAxObject* workBook = workbooks->querySubObject("Open(const QString&)", path);
QAxObject* sheets = workBook->querySubObject( "Worksheets" );
QAxObject* sheet1 = sheets->querySubObject( "Item( int )", 1 );
QAxObject* sheet2 = sheets->querySubObject( "Item( int )", 2 );
...
我正在使用 Qt activex (QAxObject) 以便 read/write excel 文件。
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workBook = workbooks->querySubObject("Open(const QString&)", path);
QAxObject* sheets = workBook->querySubObject( "Worksheets" );
QAxObject* sheet1 = sheets->querySubObject( "Item( int )", 1 );
QAxObject* sheet2 = sheets->querySubObject( "Item( int )", 2 );
//....
我们知道此机制使用 excel 本身,并且在未安装 excel 的计算机上不起作用。
如果代码在此类计算机上运行(未安装 excel),则程序会崩溃。如何在代码中检测到 excel 未安装在 PC 上?
您检查工作簿是否为 NULL 指针。根据经验,您可能需要在使用前检查 excel 是否为空指针。
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
if ( excel )
{
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
if ( workbooks )
{
QAxObject* workBook = workbooks->querySubObject("Open(const QString&)", path);
QAxObject* sheets = workBook->querySubObject( "Worksheets" );
QAxObject* sheet1 = sheets->querySubObject( "Item( int )", 1 );
QAxObject* sheet2 = sheets->querySubObject( "Item( int )", 2 );
...