|
本帖最后由 Jefft 于 2016-9-8 15:55 编辑
在 NX11中,新增了与电子表格操作的类;
Spreadsheet
SpreadsheetCellData
SpreadsheetExternal
SpreadsheetManager
对应的头文件为:
- #include <NXOpen/Spreadsheet.hxx>
- #include <NXOpen/SpreadsheetCellData.hxx>
- #include <NXOpen/SpreadsheetExternal.hxx>
- #include <NXOpen/SpreadsheetManager.hxx>
复制代码
利用这些类,可以操作内部与外部的电子表格。
我写了一个例子:
1、新建电子表格;
2、然后写入不同的数据;
3、再读取相关内容,打印在信息窗口;
注:个人感觉NX11虽然增加了这些类,但读取的效率比较低下,还没有用UFUN调KF的方法来读写电子表格效率高。
- 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 = 0; i < 3; i++)
- {
- NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
- celldata->SetType(NXOpen::SpreadsheetCellData::TypesInt);
- celldata->SetIntValue((int)i+1);
- addata.push_back(celldata);
- }
- openfile->AppendRow(worksheet, addata);
- addata.clear();
- //在A2-D2中写入字符串
- std::ostringstream tempnumberstring;
- for (size_t i = 0; i < 4; i++)
- {
- NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
- celldata->SetType(NXOpen::SpreadsheetCellData::TypesString);
- tempnumberstring << i+1;
- 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 = 0; i < 2; i++)
- {
- NXOpen::SpreadsheetCellData *celldata = theSession->SpreadsheetManager()->CreateCellData();
- celldata->SetType(NXOpen::SpreadsheetCellData::TypesLogical);
- if ((int)i==0)
- {
- 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, -1, -1, -1, -1, thedata); //读取范围,
- //打印基本信息
- theSession->ListingWindow()->Open();
- std::ostringstream tempstring;
- tempstring << "表序号:" << thedata[0]->IntValue() << ",起始行:" << thedata[1]->IntValue() << ",起始列:"
- << thedata[2]->IntValue() << ",结束行:" << thedata[3]->IntValue() << ",结束列:" << thedata[4]->IntValue();
- std::string covertvalue = tempstring.str();
- theSession->ListingWindow()->WriteFullline(covertvalue);
- tempstring.str("");
- tempstring.clear();
- //打印每个单元格详细信息
- for (size_t i = 5; 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 - 4) % (thedata[4]->IntValue() - thedata[2]->IntValue() + 1) == 0)
- {
- std::string covertvalue = tempstring.str();
- theSession->ListingWindow()->WriteFullline(covertvalue);
- tempstring.str("");
- tempstring.clear();
- }
- }
- //关闭电子表格并保存
- openfile->CloseFile(true);
-
- UF_terminate();
- }
复制代码
|
评分
-
查看全部评分
|