使用mysql查询数据库时，you have the ability to view results from that query on your screen or send them to a text file.If you insert tab characters into the output data stream,you can generate a file that Excel can import.You don't have to write code to add tab characters to this file;MySQL inserts them automatically when you learn to embed them in your query.
Create Tab-delimited File
Launch the tool you use to manage MySQL and open one of your databases.Use the tool to write a simple query such as this:
选择CulnN1，Column2 INTO OUTFILE"myFile"字段以“t”行终止，行以“n”从mytable终止
Replace"Column1"and"Column2"with the names of two fields in one of your database's tables.
Replace"myFile"with the name of the output file you wish to generate.For example,if you'd like to store a file named"Results.txt"in a folder named"销售“on your C drive,替换“myFile"with the following text:
Note the forward slash characters.You must use a forward slash instead of the regular backward slash as a separator when specifying the directory path to a file.
Replace the word"myTable"in the query with the name of the table from which you wish to retrieve your data.Run your query as you usually do.Results will not appear on-screen this time.因为你用了INTO OUTFILE"查询中的修饰符，the results go to the file you specified in the query.
View in Excel
Launch Excel and press"Ctrl-O"to view the Open window.
Navigate to the folder that contains the file you created.Double-click that file to launch Excel's"“文本导入”wizard window.
Click"Next"and then click the"Tab"check box to place a checkmark in that check box if none exists.文件中的数据将显示在窗口的“Data Preview"section.
Click"Finish"to load the data into a new spreadsheet.
- This example uses a simple MySQL query to illustrate the use of the INTO OUTFILE,FIELDS TERMINATED BY and LINES TERMINATED BY query modifiers.Use those modifiers with any MySQL query to create tab-delimited files that Excel can open.
- After loading the data into Excel,you are free to save your new spreadsheet as an XLS or XLSX file as you normally do.You can then double-click that file in Windows Explorer to launch it and treat it as you would any other Excel spreadsheet.
- Michael Blann/Digital Vision/Getty Images