QQ登录

只需一步,快速开始

快捷登录

登录 或者 注册 请先

UG爱好者

查看: 5112|回复: 4
打印 上一主题 下一主题

[原创] NX11.0二次开发新增Spreadsheet相关类的用法!

[复制链接]

贵宾

Rank: 9Rank: 9Rank: 9

14

主题

271

帖子

8495

积分

论坛技术员论坛贡献

跳转到指定楼层
楼主
发表于 2016-9-8 15:55:29 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 Jefft 于 2016-9-8 15:55 编辑

在 NX11中,新增了与电子表格操作的类;
Spreadsheet
SpreadsheetCellData
SpreadsheetExternal
SpreadsheetManager
对应的头文件为:
  1. #include <NXOpen/Spreadsheet.hxx>
  2. #include <NXOpen/SpreadsheetCellData.hxx>
  3. #include <NXOpen/SpreadsheetExternal.hxx>
  4. #include <NXOpen/SpreadsheetManager.hxx>
复制代码
利用这些类,可以操作内部与外部的电子表格。
我写了一个例子:
1、新建电子表格;
2、然后写入不同的数据;
3、再读取相关内容,打印在信息窗口;
注:个人感觉NX11虽然增加了这些类,但读取的效率比较低下,还没有用UFUN调KF的方法来读写电子表格效率高。
  1. void MyClass::do_it()
  2. {
  3.         UF_initialize();
  4.         //创建电子表格
  5. ************************

  6.         //打开
  7.         NXOpen::SpreadsheetExternal *openfile=
  8.         theSession->SpreadsheetManager()->OpenFile("c:\\tkl.xls", NXOpen::SpreadsheetManager::OpenModeWrite);
  9.         int worksheet = openfile->GetWorksheetIndex("tkl"); //得到电子表格工作页
  10.         
  11.         //在A1-c1中写入数字        
  12.         std::vector<NXOpen::SpreadsheetCellData *> addata;
  13.         for (size_t i = 0; i < 3; i++)
  14.         {
  15.                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
  16.                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesInt);
  17.                 celldata->SetIntValue((int)i+1);
  18.                 addata.push_back(celldata);
  19.         }
  20.         openfile->AppendRow(worksheet, addata);
  21.         addata.clear();

  22.         //在A2-D2中写入字符串
  23.         std::ostringstream tempnumberstring;        
  24.         for (size_t i = 0; i < 4; i++)
  25.         {
  26.                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
  27.                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesString);
  28.                 tempnumberstring << i+1;
  29.                 std::string covertvalue = tempnumberstring.str();
  30.                 celldata->SetStringValue("字符" + covertvalue);
  31.                 addata.push_back(celldata);
  32.                 tempnumberstring.str("");
  33.                 tempnumberstring.clear();
  34.         }
  35.         openfile->AppendRow(worksheet, addata);
  36.         addata.clear();

  37.         //在A3-C3中写入布尔型
  38.         for (size_t i = 0; i < 2; i++)
  39.         {
  40.                 NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
  41.                 celldata->SetType(NXOpen::SpreadsheetCellData::TypesLogical);
  42.                 if ((int)i==0)
  43.                 {
  44.                         celldata->SetLogicalValue(true);
  45.                 }
  46.                 else
  47.                 {
  48.                         celldata->SetLogicalValue(false);
  49.                 }
  50.                 addata.push_back(celldata);
  51.         }
  52.         openfile->AppendRow(worksheet, addata);
  53.         addata.clear();
  54.         
  55.         //读取        
  56.         std::vector<NXOpen::SpreadsheetCellData *> thedata;
  57.         openfile->ReadRange(worksheet, -1, -1, -1, -1, thedata); //读取范围,
  58.         //打印基本信息
  59.         theSession->ListingWindow()->Open();
  60.         std::ostringstream tempstring;
  61.         tempstring << "表序号:" << thedata[0]->IntValue() << ",起始行:" << thedata[1]->IntValue() << ",起始列:"
  62.                 << thedata[2]->IntValue() << ",结束行:" << thedata[3]->IntValue() << ",结束列:" << thedata[4]->IntValue();
  63.         std::string covertvalue = tempstring.str();
  64.         theSession->ListingWindow()->WriteFullline(covertvalue);
  65.         tempstring.str("");
  66.         tempstring.clear();

  67.         //打印每个单元格详细信息
  68.         for (size_t i = 5; i < (int)thedata.size(); i++)
  69.         {        
  70.                 NXOpen::SpreadsheetCellData::Types thetype = thedata[i]->Type();
  71.                 if (thetype == SpreadsheetCellData::TypesInt)
  72.                 {
  73.                         tempstring << thedata[i]->IntValue() << ",";
  74.                 }
  75.                 else if (thetype == SpreadsheetCellData::TypesDouble)
  76.                 {
  77.                         tempstring << thedata[i]->DoubleValue() << ",";
  78.                 }
  79.                 else if (thetype == SpreadsheetCellData::TypesString )
  80.                 {
  81.                         tempstring << thedata[i]->StringValue().GetLocaleText() << ",";
  82.                 }
  83.                 else if (thetype == SpreadsheetCellData::TypesLogical)
  84.                 {
  85.                         tempstring << thedata[i]->LogicalValue() << ",";
  86.                 }
  87.                 else
  88.                 {
  89.                         tempstring << thedata[i]->FormulaValue().GetLocaleText() << ",";
  90.                 }

  91.                 if ((i - 4) % (thedata[4]->IntValue() - thedata[2]->IntValue() + 1) == 0)
  92.                 {
  93.                         std::string covertvalue = tempstring.str();
  94.                         theSession->ListingWindow()->WriteFullline(covertvalue);
  95.                         tempstring.str("");
  96.                         tempstring.clear();
  97.                 }               
  98.         }

  99.         //关闭电子表格并保存
  100.         openfile->CloseFile(true);
  101.         
  102.         UF_terminate();
  103. }
复制代码



评分

参与人数 1G币 +50 元宝 +20 收起 理由
副总经理 + 50 + 20 纯支持老唐

查看全部评分

版权声明  
本人声明此帖为本人原创帖,未经允许,不得转载!

有奖推广贴子: 

回复

使用道具 举报

大校

Rank: 8Rank: 8

3

主题

920

帖子

1万

积分
沙发
发表于 2016-9-8 16:06:11 | 只看该作者
11111111111111111
回复 支持 反对

使用道具 举报

六级士官

Rank: 4

0

主题

17

帖子

1350

积分
板凳
发表于 2016-9-8 16:58:57 | 只看该作者
支持!学习学习。。。。。。
回复 支持 反对

使用道具 举报

四级士官

Rank: 3Rank: 3

4

主题

136

帖子

840

积分
地板
发表于 2016-9-8 18:31:15 | 只看该作者
前排  膜拜大神
回复 支持 反对

使用道具 举报

少校

Rank: 6Rank: 6

2

主题

813

帖子

7794

积分
5#
发表于 2016-9-26 11:02:46 | 只看该作者
这个扩展不错,感谢分享。。。。。。。。。。必须支持下啊。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

 
 
QQ:1359218528
工作时间:
9:00-17:00
 
微信公众号
手机APP
机械社区
微信小程序

手机版|UG爱好者论坛 ( 京ICP备10217105号-2 )    论坛管理员QQ:1359218528

本站信息均由会员发表,不代表本网站立场,如侵犯了您的权利请联系管理员,邮箱:1359218528@qq.com  

Powered by UG爱好者 X3.2  © 2001-2014 Comsenz Inc. GMT+8, 2024-9-21 07:01

返回顶部