admin 发表于 2017-9-12 17:11:19

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

相比较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的信息,界面如下:

(2)导出结果如下:


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

我把代码贴出来,仅供参考!

voidMBD_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);
      
}






qiufengwu 发表于 2021-8-10 20:54:50

目前正在制作,调用EXCEL中的数据。
页: [1]
查看完整版本: 关于NX11.0二次开发支持Excel的操作之鸡肋和坑