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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM