Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Export sqlserver Data To Text and Excel
Forum Updated to NodeBB v4.3 + New Features

Export sqlserver Data To Text and Excel

Scheduled Pinned Locked Moved Solved General and Desktop
exceltextsqlserver
6 Posts 4 Posters 4.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M4RZB4NiM Offline
    M4RZB4NiM Offline
    M4RZB4Ni
    wrote on last edited by
    #1

    hello
    i have a sqlserver db and i want to write a query or code
    to export mydb data to a TEXT FILE AND A EXCEL FILE
    what should i do?

    Thanks
    M4RZB4Ni

    the_T 1 Reply Last reply
    0
    • sneubertS Offline
      sneubertS Offline
      sneubert
      wrote on last edited by sneubert
      #2

      Hi M4RZB4Ni,

      basically you have to get all tables with (http://doc.qt.io/qt-5/qsqldatabase.html#tables), and for each table fetch a QSqlRecord (http://doc.qt.io/qt-5/qsqldatabase.html#record) to get column names and types. Now you can iterate with maybe SELECT * FROM ...over each table entry an print the result to a file. On how to produce a valid EXCEL file you have to consult MSDN.

      Maybe it would be enough to call a managment console for your sqlserver. The Major sql products normally offer tools for export a database.

      M4RZB4NiM 1 Reply Last reply
      1
      • M4RZB4NiM M4RZB4Ni

        hello
        i have a sqlserver db and i want to write a query or code
        to export mydb data to a TEXT FILE AND A EXCEL FILE
        what should i do?

        the_T Offline
        the_T Offline
        the_
        wrote on last edited by
        #3

        @M4RZB4Ni
        To generate an Excel File (.xls, .xlsx) see https://wiki.qt.io/Handling_Microsoft_Excel_file_format and/or https://github.com/VSRonin/QtXlsxWriter (Thanks to @VRonin ;))

        -- No support in PM --

        VRoninV 1 Reply Last reply
        1
        • the_T the_

          @M4RZB4Ni
          To generate an Excel File (.xls, .xlsx) see https://wiki.qt.io/Handling_Microsoft_Excel_file_format and/or https://github.com/VSRonin/QtXlsxWriter (Thanks to @VRonin ;))

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @the_ said:

          (Thanks to @VRonin ;))

          I'm not the author of the library, I can take no credit for it, I'm just making sure the code compiles with recent Qt.

          I don't think the library is needed here anyway, something like this should do the job:

          QFile data("output.csv");
          if (data.open(QFile::WriteOnly)) {
              QTextStream outTxt(&data);
          	QSqlQuery query;
          	bool firstLine=true;
          	query.prepare("SELECT * FROM MyTable");
          	if(query.exec()){
          		while (query.next()) {
          			const QSqlRecord recrd= query.record();
          			if(firstLine){
          				for(int i=0;i<recrd.count();++i)
          					outTxt << recrd.field(i) << ','; //Headers
          			}
          			firstLine=false;
          			outTxt << "\r\n";
          			for(int i=0;i<recrd.count();++i)
          				outTxt << recrd.value(i).toString() << ',';
          		}
          	}
          	data.close();
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4
          • sneubertS sneubert

            Hi M4RZB4Ni,

            basically you have to get all tables with (http://doc.qt.io/qt-5/qsqldatabase.html#tables), and for each table fetch a QSqlRecord (http://doc.qt.io/qt-5/qsqldatabase.html#record) to get column names and types. Now you can iterate with maybe SELECT * FROM ...over each table entry an print the result to a file. On how to produce a valid EXCEL file you have to consult MSDN.

            Maybe it would be enough to call a managment console for your sqlserver. The Major sql products normally offer tools for export a database.

            M4RZB4NiM Offline
            M4RZB4NiM Offline
            M4RZB4Ni
            wrote on last edited by
            #5

            @sneubert
            can i use QsqlQuery and Write Direct to File?

            Thanks
            M4RZB4Ni

            1 Reply Last reply
            0
            • sneubertS Offline
              sneubertS Offline
              sneubert
              wrote on last edited by
              #6

              You can use QSqlQuery to run the select statements and loop the resultset with next() like in VRonin´s example. If you comma seperate the columns like in VRonin´s example you can open this file in excel.

              One more option, if your on windows is to use ODBC Excel Driver to generate the Excel file. With this you can use QSqlQuery to create tables and insert rows.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved