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

Export sqlserver Data To Text and Excel

Scheduled Pinned Locked Moved Solved General and Desktop
exceltextsqlserver
6 Posts 4 Posters 4.8k Views
  • 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.
  • M Offline
    M Offline
    M4RZB4Ni
    wrote on 13 Jul 2016, 07:41 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

    T 1 Reply Last reply 13 Jul 2016, 09:28
    0
    • S Offline
      S Offline
      sneubert
      wrote on 13 Jul 2016, 08:50 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.

      M 1 Reply Last reply 13 Jul 2016, 20:08
      1
      • M M4RZB4Ni
        13 Jul 2016, 07:41

        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?

        T Offline
        T Offline
        the_
        wrote on 13 Jul 2016, 09:28 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 --

        V 1 Reply Last reply 13 Jul 2016, 09:55
        1
        • T the_
          13 Jul 2016, 09:28

          @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 ;))

          V Offline
          V Offline
          VRonin
          wrote on 13 Jul 2016, 09:55 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
          • S sneubert
            13 Jul 2016, 08:50

            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.

            M Offline
            M Offline
            M4RZB4Ni
            wrote on 13 Jul 2016, 20:08 last edited by
            #5

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

            Thanks
            M4RZB4Ni

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sneubert
              wrote on 14 Jul 2016, 11:04 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

              1/6

              13 Jul 2016, 07:41

              • Login

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