NX二次开发-使用libxl读写EXCEL

最近这一段身体不舒服,所以一直没写博客,晚上都在躺床上看斗鱼直播。看看电视剧大宅门,看看女主播跳舞,偶尔还看看七龙珠。

之前写过OLE/COM组件方式读写EXCEL,前几天做了一个C++ QT的应用程序,当时发现在QT项目里不能用OLE,网上搜别人都是拿QT的EXCEL库去弄的,

我没研究那么深,还不会。后来搜到了LIBXL,这个也可以做EXCEL读写,而且优点是本地不需要安装OFFICE都可以读写EXCEL,这一点是OLE所不具备的,

而且库是人家封装好的了,直接调用封装现成的方法就行了。配置开发环境也非常简单,读写速度特别快。但是也有缺点,缺点是这个是付费的,当然CSDN上面也有破解的库。还有就是可能不支持多线程。

注意事项:OLE行列是从1开始的,LIBXL行列是从0开始的。

  1 //test1
  2 
  3 // Mandatory UF Includes
  4 #include <uf.h>
  5 #include <uf_object_types.h>
  6 
  7 // Internal Includes
  8 #include <NXOpen/ListingWindow.hxx>
  9 #include <NXOpen/NXMessageBox.hxx>
 10 #include <NXOpen/UI.hxx>
 11 
 12 // Internal+External Includes
 13 #include <NXOpen/Annotations.hxx>
 14 #include <NXOpen/Assemblies_Component.hxx>
 15 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
 16 #include <NXOpen/Body.hxx>
 17 #include <NXOpen/BodyCollection.hxx>
 18 #include <NXOpen/Face.hxx>
 19 #include <NXOpen/Line.hxx>
 20 #include <NXOpen/NXException.hxx>
 21 #include <NXOpen/NXObject.hxx>
 22 #include <NXOpen/Part.hxx>
 23 #include <NXOpen/PartCollection.hxx>
 24 #include <NXOpen/Session.hxx>
 25 
 26 #include "libxl.h"
 27 #pragma comment(lib,"D:\test1\test1\libxl.lib")
 28 
 29 // Std C++ Includes
 30 #include <iostream>
 31 #include <sstream>
 32 
 33 using namespace libxl;
 34 using namespace std;
 35 using namespace NXOpen;
 36 using std::string;
 37 using std::exception;
 38 using std::stringstream;
 39 using std::endl;
 40 using std::cout;
 41 using std::cerr;
 42 
 43 
 44 //------------------------------------------------------------------------------
 45 // NXOpen c++ test class 
 46 //------------------------------------------------------------------------------
 47 class MyClass
 48 {
 49     // class members
 50 public:
 51     static Session *theSession;
 52     static UI *theUI;
 53 
 54     MyClass();
 55     ~MyClass();
 56 
 57     void do_it();
 58     void print(const NXString &);
 59     void print(const string &);
 60     void print(const char*);
 61 
 62 private:
 63     Part *workPart, *displayPart;
 64     NXMessageBox *mb;
 65     ListingWindow *lw;
 66     LogFile *lf;
 67 };
 68 
 69 //------------------------------------------------------------------------------
 70 // Initialize static variables
 71 //------------------------------------------------------------------------------
 72 Session *(MyClass::theSession) = NULL;
 73 UI *(MyClass::theUI) = NULL;
 74 
 75 //------------------------------------------------------------------------------
 76 // Constructor 
 77 //------------------------------------------------------------------------------
 78 MyClass::MyClass()
 79 {
 80 
 81     // Initialize the NX Open C++ API environment
 82     MyClass::theSession = NXOpen::Session::GetSession();
 83     MyClass::theUI = UI::GetUI();
 84     mb = theUI->NXMessageBox();
 85     lw = theSession->ListingWindow();
 86     lf = theSession->LogFile();
 87 
 88     workPart = theSession->Parts()->Work();
 89     displayPart = theSession->Parts()->Display();
 90     
 91 }
 92 
 93 //------------------------------------------------------------------------------
 94 // Destructor
 95 //------------------------------------------------------------------------------
 96 MyClass::~MyClass()
 97 {
 98 }
 99 
100 //------------------------------------------------------------------------------
101 // Print string to listing window or stdout
102 //------------------------------------------------------------------------------
103 void MyClass::print(const NXString &msg)
104 {
105     if(! lw->IsOpen() ) lw->Open();
106     lw->WriteLine(msg);
107 }
108 void MyClass::print(const string &msg)
109 {
110     if(! lw->IsOpen() ) lw->Open();
111     lw->WriteLine(msg);
112 }
113 void MyClass::print(const char * msg)
114 {
115     if(! lw->IsOpen() ) lw->Open();
116     lw->WriteLine(msg);
117 }
118 
119 
120 
121 
122 //------------------------------------------------------------------------------
123 // Do something
124 //------------------------------------------------------------------------------
125 void MyClass::do_it()
126 {
127 
128     // TODO: add your code here
129     
130     //获得book
131     Book* book = xlCreateXMLBookA();
132     book->setKey("Halil Kural", "windows-2723210a07c4e90162b26966a8jcdboe");//注册
133     if (book)
134     {
135         //打开类型对照表EXCEL
136         if (book->load("D:\123.xlsx"))
137         {
138             //获取sheet
139             Sheet* sheet = book->getSheet(0);
140             if (sheet)
141             {
142                 //写入内容
143                 sheet->writeStr(0,0,"大傻逼!");
144                 sheet->writeNum(0,1,123);
145             }
146             //保存
147             book->save("D:\123.xlsx");
148         }
149         book->release();//释放对象!!!!!
150     }
151 
152 }
153 
154 //------------------------------------------------------------------------------
155 // Entry point(s) for unmanaged internal NXOpen C/C++ programs
156 //------------------------------------------------------------------------------
157 //  Explicit Execution
158 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
159 {
160     try
161     {
162         // Create NXOpen C++ class instance
163         MyClass *theMyClass;
164         theMyClass = new MyClass();
165         theMyClass->do_it();
166         delete theMyClass;
167     }
168     catch (const NXException& e1)
169     {
170         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
171     }
172     catch (const exception& e2)
173     {
174         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
175     }
176     catch (...)
177     {
178         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
179     }
180 }
181 
182 
183 //------------------------------------------------------------------------------
184 // Unload Handler
185 //------------------------------------------------------------------------------
186 extern "C" DllExport int ufusr_ask_unload()
187 {
188     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
189 }
190 
191 
192 Caesar卢尚宇
193 2019年12月29日

LIBXL官网

https://www.libxl.com/

https://www.libxl.com/workbook.html#addSheet

相关资料链接:感谢各位博主大神!

libxl库读取excel文件,遍历excel中的所有表和表中所有元素https://blog.csdn.net/haijunsm/article/details/86165419

LibXL库使用详解(篇一)https://blog.csdn.net/zt_xcyk/article/details/72846042

LibXL库使用详解---增删查改(篇二)https://blog.csdn.net/zt_xcyk/article/details/73247548

libxl 学习之 excel 读写操作https://blog.csdn.net/u010477528/article/details/53857396

libxl 库使用https://wenku.baidu.com/view/8ff2d43a0912a2161479299f.html?from=search

使用libxl库读取excel文件https://blog.csdn.net/iamqianrenzhan/article/details/81008662

xlslib生成excel文件https://blog.csdn.net/byxdaz/article/details/83505475

原文地址:https://www.cnblogs.com/nxopen2018/p/12116701.html