这几天搜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();
}

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

05-10 18:18