How to: Export MySQL table to Excel


1. Read MySQL Table into DataReader.


publicMySqlDataReader MySQLConnect()


  string strConn = ConfigurationManager.ConnectionStrings[“dbConnectionString”].ConnectionString;

  MySqlConnection conn = newMySqlConnection(strConn);



  string strSQL = “SELECT * FROM om_org_structure_ostr”;

  MySqlCommand cmd = newMySqlCommand(strSQL, conn);


  MySqlDataReader dr = cmd.ExecuteReader();

  return dr;



2. Open Excel file, write to it then save.


publicvoid MySQLToExcel(MySqlDataReader dr,  string strFileName)


  int row = 1;

  Object Opt = Missing.Value;

  Microsoft.Office.Interop.Excel.Application app = newApplication();

  //Declare Workbook

  Microsoft.Office.Interop.Excel.Workbook book;


  //Excel.Worksheet sheet;

  Microsoft.Office.Interop.Excel.Worksheet xlsSheet;

  Microsoft.Office.Interop.Excel.Range oRng;


  //Open Spreadsheet

  book = app.Workbooks.Open(strFileName, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt);


  //Take the first sheet

  xlsSheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets[1];


  while (dr.Read())


    for (int col = 0; col < dr.FieldCount; col++)


      xlsSheet.Cells[row + 1, col + 1] = dr.GetValue(col);






  book.Close(false, false, Missing.Value);



Download the complete code from The sample file name is ConAppMySQLtoExcel.rar. My MSN ID is


About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

14 Responses to How to: Export MySQL table to Excel

  1. says:

    Woaw…. this is fenomenal :D!!!!!!!!! Thank’s…..

  2. Avinash Reddy says:


    I didn’t get the complete code can you please help me to get it ?

  3. Hen Path says:

    Hi how can I get the code? I’ve tried it but I couldn’t

  4. Great web site you have here.. It’s hard to find good quality writing like yours nowadays. I really appreciate individuals like you! Take care!!

  5. Anqi says:

    Can you give me the complete code plizz…thanks

  6. momo says:

    I can’t get your sample code from skydrive. Would U like to send me your sample code please?

  7. imarielp says:

    Good day! Can you please send it to my email –> <– the complete code/project of this topic? Thanks in advance.🙂

  8. nodatafound says:

    hi… Can you please send it to my email is for my first job about programing engineer

  9. syed Abuthahir says:

    Can you give me the complete code plz..

  10. Opeyemi says:

    I can’t get the file on the link you provided

  11. mohitverma09 says:

    Hi Can you send your code files? Also are you using an DLL? Thank you for your help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s