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

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

 #include <NXOpen/Spreadsheet.hxx>
#include <NXOpen/SpreadsheetCellData.hxx>
#include <NXOpen/SpreadsheetExternal.hxx>
#include <NXOpen/SpreadsheetManager.hxx> ```
利用这些类,可以操作内部与外部的电子表格。
我写了一个例子:
、新建电子表格;
、然后写入不同的数据;
、再读取相关内容,打印在信息窗口;
注:个人感觉NX11虽然增加了这些类,但读取的效率比较低下,还没有用UFUN调KF的方法来读写电子表格效率高。 ```c
void MyClass::do_it()
{
UF_initialize();
//创建电子表格
************************ //打开
NXOpen::SpreadsheetExternal *openfile=
theSession->SpreadsheetManager()->OpenFile("c:\\tkl.xls", NXOpen::SpreadsheetManager::OpenModeWrite);
int worksheet = openfile->GetWorksheetIndex("tkl"); //得到电子表格工作页 //在A1-c1中写入数字
std::vector<NXOpen::SpreadsheetCellData *> addata;
for (size_t i = ; i < ; i++)
{
NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
celldata->SetType(NXOpen::SpreadsheetCellData::TypesInt);
celldata->SetIntValue((int)i+);
addata.push_back(celldata);
}
openfile->AppendRow(worksheet, addata);
addata.clear(); //在A2-D2中写入字符串
std::ostringstream tempnumberstring;
for (size_t i = ; i < ; i++)
{
NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
celldata->SetType(NXOpen::SpreadsheetCellData::TypesString);
tempnumberstring << i+;
std::string covertvalue = tempnumberstring.str();
celldata->SetStringValue("字符" + covertvalue);
addata.push_back(celldata);
tempnumberstring.str("");
tempnumberstring.clear();
}
openfile->AppendRow(worksheet, addata);
addata.clear(); //在A3-C3中写入布尔型
for (size_t i = ; i < ; i++)
{
NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
celldata->SetType(NXOpen::SpreadsheetCellData::TypesLogical);
if ((int)i==)
{
celldata->SetLogicalValue(true);
}
else
{
celldata->SetLogicalValue(false);
}
addata.push_back(celldata);
}
openfile->AppendRow(worksheet, addata);
addata.clear(); //读取
std::vector<NXOpen::SpreadsheetCellData *> thedata;
openfile->ReadRange(worksheet, -, -, -, -, thedata); //读取范围,
//打印基本信息
theSession->ListingWindow()->Open();
std::ostringstream tempstring;
tempstring << "表序号:" << thedata[]->IntValue() << ",起始行:" << thedata[]->IntValue() << ",起始列:"
<< thedata[]->IntValue() << ",结束行:" << thedata[]->IntValue() << ",结束列:" << thedata[]->IntValue();
std::string covertvalue = tempstring.str();
theSession->ListingWindow()->WriteFullline(covertvalue);
tempstring.str("");
tempstring.clear(); //打印每个单元格详细信息
for (size_t i = ; i < (int)thedata.size(); i++)
{
NXOpen::SpreadsheetCellData::Types thetype = thedata[i]->Type();
if (thetype == SpreadsheetCellData::TypesInt)
{
tempstring << thedata[i]->IntValue() << ",";
}
else if (thetype == SpreadsheetCellData::TypesDouble)
{
tempstring << thedata[i]->DoubleValue() << ",";
}
else if (thetype == SpreadsheetCellData::TypesString )
{
tempstring << thedata[i]->StringValue().GetLocaleText() << ",";
}
else if (thetype == SpreadsheetCellData::TypesLogical)
{
tempstring << thedata[i]->LogicalValue() << ",";
}
else
{
tempstring << thedata[i]->FormulaValue().GetLocaleText() << ",";
} if ((i - ) % (thedata[]->IntValue() - thedata[]->IntValue() + ) == )
{
std::string covertvalue = tempstring.str();
theSession->ListingWindow()->WriteFullline(covertvalue);
tempstring.str("");
tempstring.clear();
}
} //关闭电子表格并保存
openfile->CloseFile(true); UF_terminate();
}

最新文章

  1. 【转】Dubbo使用例子并且和Spring集成使用
  2. [Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.
  3. 小白请教几个关于Java虚拟机内存分配策略的问题
  4. Ubuntu 16.04 安装 .NET Core[转]
  5. JavaSE自学笔记
  6. mormot 数据集转换为JSON字串
  7. BZOJ1113 海报PLA
  8. struts2——通配符
  9. JDBC小结
  10. SpringCloud学习笔记(3)——Hystrix
  11. POJ1015 &amp;&amp; UVA - 323 ~Jury Compromise(dp路径)
  12. Java中存取权限和修饰符public、private、protected和default的区别和联系
  13. ES 06 - 通过Kibana插件增删改查ES中的索引文档
  14. 使用Maven插件构建Spring Boot应用程序镜像
  15. 高校表白APP-冲刺第一天
  16. GMA Round 1 YGGDRASIL
  17. Linux 安装vsftpd和ftp客户端
  18. h5新API之WebStorage解决页面数据通信问题
  19. 微信HTML5页面设计建议
  20. SQL Server -- 随笔

热门文章

  1. 【Flutter学习】基本组件之TabBar顶部导航
  2. Network基础(二):数制转换
  3. The Second Scrum Meeting!
  4. 数据库的显示、创建、使用 、用户授权管理及忘记root用户后重置密码
  5. C# winform 动态构建fastreport报表
  6. ML&amp;MLDS笔记:偏差 vs 方差
  7. Rust &lt;2&gt;:函数、方法与注释的格式
  8. Django框架(二十一)—— Django rest_framework-权限组件
  9. 如何在webpack开发中利用vue框架使用ES6中提供的新语法
  10. 硬RAID与软RAID的区别