//xy_LibXL_read_excel_test2
// Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h>
// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>
// Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>
// Std C++ Includes
#include <iostream>
#include <sstream>
//自定义
#include <uf.h>
#include <uf_ui.h>
#include <iostream>
#include <windows.h>
#include "libxl.h"
#pragma comment(lib,"D:\\XYmould\\xy_libxl\\lib64\\libxl.lib")
using namespace std;
using namespace libxl;
using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr;
//自定义
Sheet* getSheetByName(Book* book, const wchar_t* name);
//------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
// class members
public:
static Session *theSession;
static UI *theUI;
MyClass();
~MyClass();
void do_it();
void print(const NXString &);
void print(const string &);
void print(const char*);
private:
BasePart *workPart, *displayPart;
NXMessageBox *mb;
ListingWindow *lw;
LogFile *lf;
};
//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{
// Initialize the NX Open C++ API environment
MyClass::theSession = NXOpen::Session::GetSession();
MyClass::theUI = UI::GetUI();
mb = theUI->NXMessageBox();
lw = theSession->ListingWindow();
lf = theSession->LogFile();
workPart = theSession->Parts()->BaseWork();
displayPart = theSession->Parts()->BaseDisplay();
}
//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
}
//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{
// TODO: add your code here
UF_initialize();
Book * xy_book = xlCreateBook();
if (!xy_book) //判断是否创建实例成功
{
uc1601("温馨提醒:表格访问失败!", 1);
}
else
{
if (!(xy_book->load(L"D:\\XYmould\\configure\\xy_part_name.xls"))) //判断是加载文件至内存是否成功
{
uc1601("温馨提醒:未找到对应表格!", 1);
}
else
{
Sheet * xy_sheet = xy_book->getSheet(0); //getSheet:获取工作列表
//Sheet * xy_sheet = getSheetByName(xy_book,L"Sheet2"); //getSheet:获取工作列表,按名称访问工作表
if (!xy_sheet) //判断访问工作簿是否成功
{
uc1601("温馨提醒:未找到对应表格工作簿!", 1);
}
else
{
int xy_rowfirst = xy_sheet->firstRow(); //firstRow:数据开始行
int xy_rowlast = xy_sheet->lastRow(); //lastRow:数据结束行
int xy_colfirst = xy_sheet->firstCol(); //firstCol:数据开始列
int xy_collast = xy_sheet->lastCol(); //lastCol:数据结束列
UF_UI_open_listing_window();
char msg[256] ;
sprintf(msg, "开始行:%d\n结束行:%d\n开始列:%d\n结束列:%d\n", xy_rowfirst + 1, xy_rowlast + 1, xy_colfirst + 1, xy_collast + 1);
UF_UI_write_listing_window(msg);
for (int xy_row = xy_sheet->firstRow(); xy_row < xy_sheet->lastRow(); xy_row++) //循环行
{
for (int xy_col = xy_sheet->firstCol(); xy_col < xy_sheet->lastCol(); xy_col++) //循环列
{
CellType xy_cellType = xy_sheet->cellType(xy_row, xy_col); //返回单元格的类型
//sprintf(msg, "第%d行,第%d列:%ws\n", xy_row + 1, xy_col + 1, xy_sheet->readStr(xy_row, xy_col)); //读取所有内容
sprintf(msg, "第%d行,第%d列:", xy_row + 1, xy_col + 1);
UF_UI_write_listing_window(msg);
if ( xy_sheet->isFormula(xy_row,xy_col) ) //检查单元格是否包含公式
{
const wchar_t * s = xy_sheet->readFormula(xy_row, xy_col); //从单元格中读取公式字符串及其格式。如果指定的单元格不包含公式或发生错误,则返回NULL。
sprintf(msg, "%ws\t", (s ? s : L"null")); //公式数据转换
UF_UI_write_listing_window(msg);
UF_UI_write_listing_window("[formula]");
UF_UI_write_listing_window("\n");
}
else
{
switch (xy_cellType)
{
case libxl::CELLTYPE_EMPTY: //空,单元格不存在
{
UF_UI_write_listing_window("[empty]");
UF_UI_write_listing_window("\n");
break;
}
case libxl::CELLTYPE_NUMBER: //数值
{
double d = xy_sheet->readNum(xy_row, xy_col); //从单元格中读取数字或日期/时间及其格式
sprintf(msg, "%f\n", d);
UF_UI_write_listing_window(msg);
break;
}
case libxl::CELLTYPE_STRING: //字符串值
{
const wchar_t * s = xy_sheet->readStr(xy_row, xy_col); //从单元格中读取字符串及其格式
sprintf(msg, "%ws\t", (s ? s : L"null")); //公式数据转换
UF_UI_write_listing_window(msg);
UF_UI_write_listing_window("[string]");
UF_UI_write_listing_window("\n");
break;
}
case libxl::CELLTYPE_BOOLEAN: //布尔值
{
bool b = xy_sheet->readBool(xy_row, xy_col); //从单元格中读取bool值及其格式
sprintf(msg, "%s\t", (b ? "true" : "false")); //公式数据转换
UF_UI_write_listing_window(msg);
UF_UI_write_listing_window("[boolean]");
UF_UI_write_listing_window("\n");
break;
}
case libxl::CELLTYPE_BLANK: //空白,单元格只包含格式信息
{
UF_UI_write_listing_window("[blank]");
UF_UI_write_listing_window("\n");
break;
}
case libxl::CELLTYPE_ERROR: //错误
{
UF_UI_write_listing_window("[error]");
UF_UI_write_listing_window("\n");
break;
}
default:
break;
}
}
}
}
}
}
}
xy_book->release(); //删除该对象并释放资源
UF_terminate();
}
//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
// Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
try
{
// Create NXOpen C++ class instance
MyClass *theMyClass;
theMyClass = new MyClass();
theMyClass->do_it();
delete theMyClass;
}
catch (const NXException& e1)
{
UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
}
catch (const exception& e2)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
}
catch (...)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
}
}
//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}
/* 通过名称而不是通过索引访问工作表 */
Sheet* getSheetByName(Book* book, const wchar_t* name)
{
for (int i = 0; i < book->sheetCount(); ++i)
{
if (wcscmp(book->getSheet(i)->name(), name) == 0)
{
return book->getSheet(i);
}
}
return 0;
}