What's new

Visual c++

Crypto Pitt

Forum Guru
Elite
Joined
Nov 19, 2020
Posts
2,683
Reaction
4,446
Points
1,546
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++
 
Here is a sample code that you can use as a reference to create a form using Visual C++ that collects first name, last name, and middle name and saves it to an Excel file.

First, you need to create a form with three textboxes for first name, last name, and middle name, and a save button. You can do this by opening the Visual Studio and creating a new Windows Forms Application project.

Next, add the necessary headers for Excel automation and namespace at the top of your code file:

Code:
#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;

In the save button click event, you need to get the input values from the textboxes and save them to the Excel file. You can use the following code for that:

Code:
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.");
}

In this code, we first get the values entered by the user in the textboxes, create an instance of the Excel Application, and open the workbook to which we want to save the data.

Next, we select the first worksheet in the workbook and get the next empty row to save the data. We save the values entered by the user to the worksheet and close the workbook.

Finally, we release the resources used by Excel and display a message to the user that the data has been saved successfully.

Note: You need to change the path of the workbook to which you want to save the data.
 

Similar threads

Back
Top