PLM之家精品课程培训

PLM之家精品课程培训

联系电话:18301858168   |   QQ咨询:939801026
NX二次开发培训

NX二次开发培训

UFUN/NXOpen C++和实战案例

适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术。
公众号二维码

关注公众号

点击扫描二维码免费在线高清教程

课程详情
Catia二次开发培训

Catia二次开发培训

市场需求大,掌握核心技术前景广阔

Catia二次开发的市场需求大,人才稀缺。掌握开发技能潜力巨大,随着经验积累将在汽车、航空等领域有所作为。
B站二维码

在线原创B站视频

点击关注工业软件传道士主页

课程详情
Teamcenter培训

Teamcenter培训

全方位培训,从基础应用到高级开发全覆盖

涵盖用户应用基础培训、管理员基础培训、管理员高级培训及二次开发培训等全方位内容,由多年经验讲师打造。
QQ群二维码

加入同行交流

点击扫描二维码加入QQ群

课程详情
×

PLM之家plmhome公众号

课程涵盖: PLM之家所有原创视频

×

关注B站视频

所有高清视频一览无余,全部在线播放学习

×

加入PLM之家QQ群

同行交流,疑问解答,更多互助

PLM之家PLMHome-国产软件践行者

关于NX11.0二次开发支持Excel的操作之鸡肋和坑

2017-9-12 17:22| 发布者: admin| 查看: 6467| 评论: 1|原作者: admin

摘要: 相比较Catia 的CAA 开发对xml,excel相关的API函数,NX在这方面确实做的很不地道。要实现和excel的交互需要使用各种流行的方法去处理,个人常用的就是OLE的方式,当然如果你使用C# 或者 JAva 可能更好些,封装一些常 ...
相比较Catia 的CAA 开发对xml,excel相关的API函数,NX在这方面确实做的很不地道。要实现和excel的交互需要使用各种流行的方法去处理,个人常用的就是OLE的方式,当然如果你使用C# 或者 JAva 可能更好些,封装一些常用的功能去用。
NX11的帮助文档,明确的写了如下话语:

What is it?
Use the new NX Open methods to further interact with spreadsheets. You can control cell data and manage and control typical spreadsheet operations. The methods also provide capabilities for spreadsheets for NX Open similar to the current Knowledge Fusion spreadsheet functions.
The following new classes are provided:
  • SpreadsheetManager - Contains the spreadsheet interaction APIs that are similar to the Knowledge Fusion spreadsheet functions.
  • SpreadsheeTCellData - Contains data types and data for the cell.
  • Spreadsheet - This is a class for the spreadsheet object.


Why should I use it?
You can use these new methods to modify and manage spreadsheets in NX Open programs.


怀着无比兴奋的心情,想象一下以后再不用OLE的方式去做了,我要实现的功能很简单,树列表的内容导出!先把结果贴出来,再细说:
(1)开发的功能是获取所有PMI的信息,界面如下:
111.png
(2)导出结果如下:
222.png

现在我开始看这方面的类,一共四个,发现excel 分成了内部和外部,内部呢 实际上就是你从NX里面的工具打开的,外部就是我们自己用的那种。显然对于我来说只能是外部了,打开函数没问题,直接打开,导出只能通过appendrow的方法,这个很显然每次都是追加的方式。
问题是我如何覆盖以前的内容,外部方式的方法很少,实现不了,我觉得这是一个坑,官方是不是留到下一个版本再放出?
无法清除或者写入指定的range的结果就是使用appendRow不停的追加,很难看。另外也有些不稳定的报错,比如找不到sheet索引,无法读取excel,实际上是单进程的,如果已经打开了这个文件,就会报错,这个比较坑。
个人认为读取问题不大,要写入真靠不住!!
3333.png
我把代码贴出来,仅供参考!

void  MBD_PMIInformation::exportInforToSpreadSheet()
{
        NXString excelTemplateName = theSession->GetEnvironmentVariableValue("UGII_USER_DIR") + "\\data\\MBD_PMIInformation.xlsx";
        
        //open the excel as write mode
        NXOpen::SpreadsheetExternal *spreadSheet = theSession->SpreadsheetManager()->OpenFile(excelTemplateName,SpreadsheetManager::OpenModeWrite);
        if (spreadSheet==NULL)
        {
                MBD_PMIInformation::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError,"打开模板失败,可能模板文件已经打开:" + excelTemplateName);
                return;
        }

        // write data
        int workIndex = spreadSheet->GetWorksheetIndex("MBDInformation");
        
        std::vector< NXOpen::SpreadsheetCellData * > CellDatas;
        int numCol,numRow;
        vector<Node*> allNodes;        
        //inser title
        numCol= tree_controlInfor->NumberOfColumns();
        
        for (int i = 0; i < numCol; i++)
        {
                NXOpen::SpreadsheetCellData *CellData = theSession->SpreadsheetManager()->CreateCellData();
                CellData->SetType(SpreadsheetCellData::TypesString);
                CellData->SetStringValue(tree_controlInfor->GetColumnTitle(i).GetLocaleText());
                CellDatas.push_back(CellData);
               
        }
        
        
        spreadSheet->AppendRow(workIndex,CellDatas );
        CellDatas.clear();
        
        
        //insert value
        getAllNodes(tree_controlInfor,allNodes);
        if (allNodes.empty())
        {
                return;
        }
        numRow = allNodes.size();
        for (int ii = 0; ii < numRow; ii++)
        {
                        for (int jj = 0; jj < numCol; jj++)
                        {
                                NXOpen::SpreadsheetCellData *CellData = theSession->SpreadsheetManager()->CreateCellData();
                                CellData->SetType(SpreadsheetCellData::TypesString);
                                CellData->SetStringValue(allNodes.at(ii)->GetColumnDisplayText(jj).GetLocaleText());
                                CellDatas.push_back(CellData);
                                
                                
                        }
                  
                        spreadSheet->AppendRow(workIndex,CellDatas );
                        CellDatas.clear();
        }

        //from begining

        CellDatas.clear();
        
        
        //spreadSheet->ReadRange(workIndex,0,0,numRow,numCol,CellDatas);
        //for (int i = 0; i < CellDatas.size(); i++)
        //{
        //        lw->Open();
        //        if (CellDatas.at(i)->Type() == SpreadsheetCellData::TypesString )
        //        {
        //                lw->WriteLine(CellDatas.at(i)->StringValue().GetLocaleText());
        //        }
        //        
        //}


        spreadSheet->CloseFile(true);

        MBD_PMIInformation::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeInformation,"成功导出数据到:" + excelTemplateName);
        
}






1

鲜花

握手

雷人

路过

鸡蛋

刚表态过的朋友 (1 人)

发表评论

最新评论

引用 qiufengwu 2021-8-10 20:54
目前正在制作,调用EXCEL中的数据。

查看全部评论(1)

登录之后发表您得观点!
  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了