-NX11.0二次开发新增Spreadsheet相关类的用法!

这几天搜NX对EXCAL读取写入相关的开发内容,发现唐工写了一篇关于NX11对EXCAL操作的文章。让我知道NX11新增了对EXCAL操作相关的类,以前NX里是没有的。我以前都是用OLE方式去做,没用过其他的,今天学习一下。感谢唐工的分享。
原文出处:http://www.ugsnx.com/thread-167024-1-1.html
ps:经过多次测试,这种方式有一个缺点,就是写入的时候不能覆盖写入,新内容只能接原来的内容继续写。

在 NX11中,新增了与电子表格操作的类;
Spreadsheet
SpreadsheetCellData
SpreadsheetExternal
SpreadsheetManager
对应的头文件为:

  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 
  6 ```
  7 利用这些类,可以操作内部与外部的电子表格。
  8 我写了一个例子:
  9 1、新建电子表格;
 10 2、然后写入不同的数据;
 11 3、再读取相关内容,打印在信息窗口;
 12 注:个人感觉NX11虽然增加了这些类,但读取的效率比较低下,还没有用UFUN调KF的方法来读写电子表格效率高。
 13 
 14 ```c
 15 void MyClass::do_it()
 16 {
 17         UF_initialize();
 18         //创建电子表格
 19  ************************
 20 
 21         //打开
 22         NXOpen::SpreadsheetExternal *openfile=
 23         theSession->SpreadsheetManager()->OpenFile("c:\\tkl.xls", NXOpen::SpreadsheetManager::OpenModeWrite);
 24         int worksheet = openfile->GetWorksheetIndex("tkl"); //得到电子表格工作页
 25         
 26         //在A1-c1中写入数字        
 27         std::vector addata;
 28         for (size_t i = 0; i < 3; i++)
 29         {
 30                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
 31                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesInt);
 32                 celldata->SetIntValue((int)i+1);
 33                 addata.push_back(celldata);
 34         }
 35         openfile->AppendRow(worksheet, addata);
 36         addata.clear();
 37 
 38         //在A2-D2中写入字符串
 39         std::ostringstream tempnumberstring;        
 40         for (size_t i = 0; i < 4; i++)
 41         {
 42                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
 43                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesString);
 44                 tempnumberstring << i+1;
 45                 std::string covertvalue = tempnumberstring.str();
 46                 celldata->SetStringValue("字符" + covertvalue);
 47                 addata.push_back(celldata);
 48                 tempnumberstring.str("");
 49                 tempnumberstring.clear();
 50         }
 51         openfile->AppendRow(worksheet, addata);
 52         addata.clear();
 53 
 54         //在A3-C3中写入布尔型
 55         for (size_t i = 0; i < 2; i++)
 56         {
 57                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
 58                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesLogical);
 59                 if ((int)i==0)
 60                 {
 61                         celldata->SetLogicalValue(true);
 62                 }
 63                 else
 64                 {
 65                         celldata->SetLogicalValue(false);
 66                 }
 67                 addata.push_back(celldata);
 68         }
 69         openfile->AppendRow(worksheet, addata);
 70         addata.clear();
 71         
 72         //读取        
 73         std::vector thedata;
 74         openfile->ReadRange(worksheet, -1, -1, -1, -1, thedata); //读取范围,
 75         //打印基本信息
 76         theSession->ListingWindow()->Open();
 77         std::ostringstream tempstring;
 78         tempstring << "表序号:" << thedata[0]->IntValue() << ",起始行:" << thedata[1]->IntValue() << ",起始列:" 
 79                 << thedata[2]->IntValue() << ",结束行:" << thedata[3]->IntValue() << ",结束列:" << thedata[4]->IntValue();
 80         std::string covertvalue = tempstring.str();
 81         theSession->ListingWindow()->WriteFullline(covertvalue);
 82         tempstring.str("");
 83         tempstring.clear();
 84 
 85         //打印每个单元格详细信息
 86         for (size_t i = 5; i < (int)thedata.size(); i++)
 87         {        
 88                 NXOpen::SpreadsheetCellData::Types thetype = thedata[i]->Type();
 89                 if (thetype == SpreadsheetCellData::TypesInt)
 90                 {
 91                         tempstring << thedata[i]->IntValue() << "";
 92                 }
 93                 else if (thetype == SpreadsheetCellData::TypesDouble)
 94                 {
 95                         tempstring << thedata[i]->DoubleValue() << "";
 96                 }
 97                 else if (thetype == SpreadsheetCellData::TypesString )
 98                 {
 99                         tempstring << thedata[i]->StringValue().GetLocaleText() << "";
100                 }
101                 else if (thetype == SpreadsheetCellData::TypesLogical)
102                 {
103                         tempstring << thedata[i]->LogicalValue() << "";
104                 }
105                 else
106                 {
107                         tempstring << thedata[i]->FormulaValue().GetLocaleText() << "";
108                 }
109 
110                 if ((i - 4) % (thedata[4]->IntValue() - thedata[2]->IntValue() + 1) == 0)
111                 {
112                         std::string covertvalue = tempstring.str();
113                         theSession->ListingWindow()->WriteFullline(covertvalue);
114                         tempstring.str("");
115                         tempstring.clear();
116                 }                
117         }
118 
119         //关闭电子表格并保存
120         openfile->CloseFile(true); 
121         
122         UF_terminate();
123 }

-NX11.0二次开发新增Spreadsheet相关类的用法!_第1张图片

你可能感兴趣的:(-NX11.0二次开发新增Spreadsheet相关类的用法!)