
Author: Pixelot12345
How to use the Excel.Application in C++Builder. Target device is Win64.
Use the ComObj, call in CreateOleObject (“Excel.Application”).
Including ComObj is required.
In C++Builder it is below.
1 2 |
//// #include "System.Win.ComObj.hpp" |
Import from CSV file to Excel file.
1 2 3 |
//// constexpr wchar_t const def_filename[] = L"C:EmbarcaderoExcel_testBook1.xlsx"; constexpr wchar_t const csv_file[] = L"C:EmbarcaderoExcel_testcsv.csv"; |
We will implement this process.
Prepare the TButton.
TButton is one in a simple form.
Next is the TButton OnClick event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
///// constexpr wchar_t const def_excel_name[] = L"Excel.Application"; void __fastcall TForm1::Button1Click(TObject *Sender) { Variant ExcelApp = CreateOleObject(def_excel_name); try { Variant excel_book; Variant excel_sheet; UnicodeString excel_filename{def_filename}; try { ExcelApp.OlePropertySet("Visible", true); //setup Show Excel. excel_book = ExcelApp.OlePropertyGet("Workbooks"). OleFunction("Open",static_cast<WideString>(excel_filename)); //Open Excel File. excel_sheet = excel_book.OlePropertyGet("WorkSheets"). OlePropertyGet("Item", static_cast<WideString>("Sheet1")); //Select Sheet Name or Number. /* excel_sheet.OlePropertyGet("Cells"). OlePropertyGet("Item",1,2).OlePropertySet("Value",(WideString)L"文字"); //Write String Cell. cell_set(excel_sheet.OlePropertyGet("Cells"), 1, 1, "a"); //One Cell SetValue. */ load_csv_toCell(csv_file,excel_sheet.OlePropertyGet("Cells")); excel_book.OleProcedure("Save"); } __finally { excel_book = Unassigned(); excel_sheet = Unassigned(); } ExcelApp.OleProcedure("Quit"); //Quit Excel. } __finally { ExcelApp = Unassigned(); } } |
Sheet name is “Sheet 1”.
Next we will create two methods.
1 2 3 4 5 6 7 8 9 10 11 |
//// void __fastcall TForm1::cell_set(System::Variant cells, int row_, int col, String value_) { try { //Write to one of the cell. cells.OlePropertyGet("Item",row_,col) .OlePropertySet("Value",static_cast<WideString>(value_)); } catch(...){} } |
Read the file with TStringList.And, you write to the specified cell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//// void __fastcall TForm1::load_csv_toCell(String csv_filename, System::Variant cells) { auto csv_ = {std::make_unique<TStringList>()}; std::wstringstream cols_; std::wstring value_ = L""; int column_count_{0}; try { (*csv_.begin())->LoadFromFile(csv_filename); //Read file. for (int i=0; i < (*csv_.begin())->Count; i++) //Retrieve one row. { //Separate one line. column_count_ = 0; //Column Count = 0. cols_.clear(); //Clear Rows. cols_.str(StringReplace((*csv_.begin())->Strings[i],L""", "",TReplaceFlags()<<rfReplaceAll).w_str()); while (std::getline(cols_, value_, L',')){ column_count_++; cell_set(cells,i + 1,column_count_,value_.c_str()); }; } } catch(...){} } |
Write the imported CSV file to Excel.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Where do you close the excel object crated with CreateOleObject?
The CreateOleObject doesn’t actually create an object, as such, it returns an interface to the object and this is reference-counted.
The line;
ExcelApp = Unassigned();
sets the OLE object to nil (unassigned) which correctly releases the interface.