zoukankan      html  css  js  c++  java
  • MFC 简单输出EXCEL

    三图胜千言

    就是酱紫

    //打印领料表
    void CKnifeDlgDlg::PrintCurUsedTabel(int order)
    {
        // TODO: Add your command handler code here
        char szFilters[] = "hgy Files (*.xls)|*.xls|All Files (*.*)|*.*||";
    
        CFileDialog fileDlg (FALSE, "xls", ReadName + ".xls",
            OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, NULL);
    
        fileDlg.m_ofn.lpstrTitle = "保存"; 
    
        
    
        if( fileDlg.DoModal() == IDOK )
        {
            CString pathName = fileDlg.GetPathName();
    
    
            std::vector<std::map<std::string,std::string>> resultArr = YHdialog::GetOpResultListTool(_knifeclasses);
            std::vector<std::string> title;
            title.push_back("用料材料");
            title.push_back("厚度");
            title.push_back("数量");
            title.push_back("面积");
            std::vector<std::string> dataCol;
            dataCol.push_back("PanelMatrial");
            dataCol.push_back("Thickness");
            dataCol.push_back("PanelNum");
            dataCol.push_back("Allsize");
    
            if(order == 0)
            {
                //反序
                std::reverse(resultArr.begin(),resultArr.end());
            }
            OnWriteDataExcel(pathName, title, dataCol, resultArr);
    
            MessageBox("保存完成");
        }
    }
    
    
    
    
    //写EXCEL
    void CKnifeDlgDlg::OnWriteDataExcel(CString savePath, std::vector<std::string> title,std::vector<std::string> dataMap, std::vector<std::map<std::string,std::string>> dataSrc)
    {
        CString strFile = savePath;  
    
        COleVariant   
            covTrue((short)TRUE),   
            covFalse((short)FALSE),   
            covOptional((long)DISP_E_PARAMNOTFOUND,   VT_ERROR);   
    
        CApplication app;  
        CWorkbook book;  
        CWorkbooks books;  
        CWorksheet sheet;  
        CWorksheets sheets;  
        CRange range;  
        CFont font;  
    
        if (!app.CreateDispatch(_T("Excel.Application")))  
        {  
            MessageBox(_T("Error!Creat Excel Application Server Faile!"));  
        }  
    
        books = app.get_Workbooks();  
        book = books.Add(covOptional);  
        sheets = book.get_Worksheets();  
        sheet = sheets.get_Item(COleVariant((short)1));  
    
        //得到全部Cells   
        range.AttachDispatch(sheet.get_Cells());   
    
        for( long title_col = 0 ; title_col < title.size() ; title_col++ )  
        {
            range.put_Item(
                _variant_t((long)1),    //
                _variant_t((long)title_col+1),    //
                _variant_t(title[title_col].c_str()));  //数据
        }
            
    
    
        //下面是向第二行的前十个单元格中输入1到10,十个数字  
        for( long data_row = 0 ; data_row < dataSrc.size() ; data_row++ )  
        {
            for( long data_col = 0 ; data_col < title.size() ; data_col++ )  
            {
                long row = data_row+2;
                long col = data_col+1;
                CString info;
                info.Format("%s",dataSrc[data_row][dataMap[data_col]].c_str());
    
                range.put_Item(
                    _variant_t(row),    //
                    _variant_t(col),    //
                    _variant_t(info));  //数据
            }
        }
    
        //保存  
        book.SaveCopyAs(COleVariant(strFile));   
        book.put_Saved(true);  
        app.put_Visible(false); 
    
        //释放对象   
        range.ReleaseDispatch();   
        sheet.ReleaseDispatch();   
        sheets.ReleaseDispatch();   
        book.ReleaseDispatch();   
        books.ReleaseDispatch();  
        app.ReleaseDispatch();  
    
        //结尾,释放  
        book.ReleaseDispatch();     
        books.ReleaseDispatch();     
        app.ReleaseDispatch();  
        app.Quit();   
    }
    MFC OLE EXCEL
  • 相关阅读:
    USB子系统gadget analyse
    关于嵌入式的学习和职业发展
    编译JNI动态库so
    Linux串口编程
    i2c_get_clientdata和to_i2c_client 的使用获取平台设备
    Kbuild 编译系统分析
    解决QPST 出现的 "服务器正在运行" 的 下载错误.
    Msm 高通平台配置记录之一
    USB Skeleton driver 分析记录
    网站应用数据库相关 过滤类
  • 原文地址:https://www.cnblogs.com/Again/p/5699027.html
Copyright © 2011-2022 走看看