Biscoff Frappe
Elite
Generate a code where a form contains input for first name, last name and middle name. Also a save button where when a user click it, the data entered will transfer to excel ? Using visual c++
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <msclr\marshal_cppstd.h>
#include <Excel.h>
using namespace std;
using namespace System;
using namespace Excel;
private: System::Void btnSave_Click(System::Object^ sender, System::EventArgs^ e) {
// Get the values entered by the user
string firstName = msclr::interop::marshal_as<string>(txtFirstName->Text);
string lastName = msclr::interop::marshal_as<string>(txtLastName->Text);
string middleName = msclr::interop::marshal_as<string>(txtMiddleName->Text);
// Create an instance of the Excel Application
Application^ excelApp = gcnew Application();
// Open the workbook
Workbook^ workbook = excelApp->Workbooks->Open("C:\\Users\\Username\\Documents\\Test.xlsx");
// Select the first worksheet
Worksheet^ worksheet = (Worksheet^)workbook->Worksheets[1];
// Get the next empty row
int row = worksheet->Cells->Find("*", System::Reflection::Missing::Value, System::Reflection::Missing::Value,
System::Reflection::Missing::Value, XlSearchOrder::xlByRows, XlSearchDirection::xlPrevious,
false, System::Reflection::Missing::Value, System::Reflection::Missing::Value)->Row + 1;
// Save the values to the worksheet
worksheet->Cells[row, 1] = firstName.c_str();
worksheet->Cells[row, 2] = lastName.c_str();
worksheet->Cells[row, 3] = middleName.c_str();
// Save and close the workbook
workbook->Save();
workbook->Close();
// Release the resources
System::Runtime::InteropServices::Marshal::ReleaseComObject(worksheet);
System::Runtime::InteropServices::Marshal::ReleaseComObject(workbook);
System::Runtime::InteropServices::Marshal::ReleaseComObject(excelApp);
MessageBox::Show("Data saved successfully.");
}