Home c++ Entry in Excel C++

Entry in Excel C++

Author

Date

Category

How can I write data to the Excel file, first creating this file?
Is it possible to do this using the ofstream function?


Answer 1, Authority 100%

In Windows to write to Excel files, you can use ODBC , which allows you to work with Excel sheets as with conventional tables in the database.

# include & lt; stdio.h & gt;
#Include & lt; Tchar.h & gt;
#Include & lt; locale.h & gt;
#Include & lt; Windows.h & gt;
#Include & lt; sqlext.h & gt;
WCHAR SZDSN [] = L "DRIVER = {Microsoft Excel Driver (* .xls)}; dsn = ''; create_db = \" C: \\ test \\ newfile.xls \ "; dbq = c: \\ test \ \ newfile.xls; readonly = 0; ";
BOOL EXECUTESQL (HDBC HDBC, LPWSTR QUERY) {
  RETCODE RC;
  HSTMT HSTMT;
  WCHAR BUFSTATE [10] = L "";
  WCHAR BUFERR [1024] = L "";
  SQLINTEGER I;
  Sqlsmallint cch;
  BOOL RESULT;
  WPRINTF (L "& GT;% S \ N", Query);
  / * Prepare SQL Query * /
  rc = sqlallocstmt (HDBC, & amp; hstmt);
  if (! SQL_SUCCEEDED (RC)) {
    WPRINTF (L "SQLALLOCSTMT FAILED \ N");
    RETURN FALSE;
  }
  RC = SQLPREPARE (HSTMT, QUERY, SQL_NTS);
  If (! SQL_SUCCEEDED (RC)) {
    wprintf (L "SQLPREPARE FAILED \ N");
    Sqlfreehandle (sql_handle_stmt, hstmt);
    RETURN FALSE;
  }
  / * Excecute The Query * /
  RC = SQLEXECUTE (HSTMT);
  If (SQL_SUCCEEDED (RC)) {
    wprintf (L "SQL SUCCESS \ N");
    Result = True;
  }
  ELSE {
    SQLGETDIAGREC (SQL_HANDLE_STMT, HSTMT, 1, BUFSTATE, & AMP; I, BUFERR, SIZEOF (BUFERR) / SIZEOF (BUFERR [0]), & amp; CCH);
    WPRINTF (L "SQL ERROR. Code:% d; message:% s \ n", i, bufferr);
    result = false;
  }
  Sqlfreehandle (sql_handle_stmt, hstmt);
  RETURN RESULT;
}
INT _TMAIN (int argc, _tchar * argv [])
{
SETLOCALE (LC_ALL, "RUSSIAN");
Henv Henv;
HDBC HDBC;
/ * ODBC API RETURN STATUS * /
RETCODE RC;
int iconnstrlength2ptr;
Wchar szconnstrout [256];
/ * Allocate An Environment Handle * /
rc = sqlalloCenv (& amp; henv);
/ * Allocate A Connection Handle * /
RC = SQLALLOCCONNECT (HENV, & AMP; HDBC);
/ * Connect to the database * /
RC = SQLDRIVERCONNECT (HDBC, NULL, (WCHAR *) SZDSN,
  SQL_NTS, (WCHAR *) SzConnstrout,
  255, (sqlsmallint *) & amp; iconnstrlength2ptr, sql_driver_noprompt);
If (SQL_SUCCEEDED (RC))
{
  WPRINTF (L "SUCCESSFULLY Connected to Database. Data Source Name: \ N% S \ N"
    szconnstrout);
  ExecuteSQL (HDBC, L "CREATE TABLE [TEST] ([Name] text, [surname] text)");
  ExecuteSQL (HDBC, L "INSERT INTO [TEST] VALUES ('Ivan', 'Ivanov')");
}
ELSE.
{
  WPRINTF (L "CouldN't Connect to% s. \ n", szdsn);
}
/ * Disconnect and free up allocated handles * /
SQLDISCONNECT (HDBC);
SQLFreeHandle (SQL_Handle_DBC, HDBC);
SQLFreeHandle (SQL_Handle_env, Henv);
getchar ();
Return 0;
}
Previous articleFunction Ofstream C++
Next articleLinks to PHP

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions