OLE方式保存Excel文件


#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure


    try {
        try {
            Ex=Variant::CreateObject("Excel.Application"); //启动Excel
        }
        catch(...)  {
            msg_Box("无法启动Excel,可能尚未安装或文件已经损坏!",MB_ICONERROR);
            return false;
        }
        Ex.PG("WorkBooks").PR("Open","模板.xls");
        Wb=Ex.PG("ActiveWorkBook");
        Wb.PG("WorkSheets",1).FN("Activate");
        Sh1=Wb.PG("ActiveSheet");

        s = FormatDateTime( "yyyy-mm-dd", Now() );
        Sh1.PG("Cells", 24, 3).PS("Value",s.c_str());             //制表日期
        Sh1.PG("Cells", 24, 26).PS("Value",g_UName.c_str());      //制表人

        ...
        
        AnsiString file1 = ExtractFilePath(Application->ExeName) + "reports\\xxxx.xls";
        if (needPrint) {
            //打印
            Ex.PS("Visible",true);
            //Ex.PG("ActiveWindow").PG("SelectedSheets").FN("PrintOut");
            Ex.PG("ActiveWindow").PG("SelectedSheets").FN("PrintPreview");
        } else {
            //保存报表
            try {
                Wb.PG("SaveAs",file1.c_str());
            } catch (...) {
                return false;
            }
        }
    }
    __finally {
        Wb.FN("Close",0);
        Ex.FN("Quit");
    }
 

猜你喜欢

转载自blog.csdn.net/huang714/article/details/89672608
今日推荐