Ask PHP to import an Excel file to MySQL. at last, the post was edited by ShunYea from 2013-02-0316: 23: 05 and read similar examples on the Internet. My code: & lt ;? Phprequire_once & nbsp; 'reader. php'; ask php about importing Excel files to MySQL.
At the end of this post, ShunYea edited it at 16:23:05 on and read similar online examples. it seems that all of them were changed.
My code:
Require_once 'reader. php ';
$ Data = new Spreadsheet_Excel_Reader ();
$ Data-> setOutputEncoding ('gbk ');
$ Data-> read('in.xls ');
@ $ Db = mysql_connect ('localhost', 'root', '123') or
Die ("cocould not connect to database .");
Mysql_query ("set names 'gbk '");
Mysql_select_db ('test1 ');
Error_reporting (E_ALL ^ E_NOTICE );
For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
$ A = $ data-> sheets [0] ['cells '] [$ I] [1]; // obtain the data in the first column
$ B = $ data-> sheets [0] ['cells '] [$ I] [2]; // Obtain data in the second column
$ C = $ data-> sheets [0] ['cells '] [$ I] [3]; // Obtain data in the third column
$ D = $ data-> sheets [0] ['cells '] [$ I] [4]; // Obtain data in the fourth column
$ E = $ data-> sheets [0] ['cells '] [$ I] [5]; // Obtain data in the fifth column
$ F = $ data-> sheets [0] ['cells '] [$ I] [6]; // Obtain data in column 6
$ G = $ data-> sheets [0] ['cells '] [$ I] [7]; // obtain the data in column 7
$ SQL = "insert into appexp (account, appinfo, price, result, income, pay, date) VALUES ('$ A',' $ B ',' $ C ', '$ d',' $ E', '$ f',' $ G ')";
Echo $ SQL .'
';
$ Res = mysql_query ($ SQL) or die ("Error: $ SQL ");
}
?>
I will. The table is also uploaded. The in. php page is blank. what is the problem?
Thank you for your answers.
------ Solution --------------------
I am normal here, and I paste my code for your reference (my web client uploads an excel file by myself ):
Require_once 'Excel/reader. php ';
$ Data = new Spreadsheet_Excel_Reader ();
If ($ _ FILES ){
$ Check = substr ($ _ FILES ["file"] ["name"], strrpos ($ _ FILES ["file"] ["name"], ". ") + 1 );
If ($ check = "xls"
------ Solution --------------------
$ Check = "xlsx ")
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/". $ _ FILES ["file"] ["name"]);
}
Else
{
Exit ('no ');
}
$ Data-> setOutputEncoding ('utf-8 ');
$ Data-> read ("upload/". $ _ FILES ["file"] ["name"]);
Error_reporting (E_ALL ^ E_NOTICE );
$ Db_host = 'localhost'; // edit_config
$ Db_user = 'root ';
$ Db_password = 'root ';
$ Db_port = 3306;
$ Db_name = 'test ';
$ Con = mysql_connect ($ db_host, $ db_user, $ db_password );
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
$ Db_selected = mysql_select_db ($ db_name, $ con );
If (! $ Db_selected)
{
Die ("Can \'t use test_db:". mysql_error ());
}
// Delete an existing table