Use PHPExcel to operate xls files ,. PHPExcel is used to operate the xls file. if you have any problem reading the xls and csv files in Chinese, you can find the relevant information on the internet and find that the PHPExcel class library is easy to use. Official Website address: phpexcel. codeplex. com1. Read the xls file in PHPExcel,
Reading Chinese xls, csv files will have problems, find the information online, found PHPExcel class library easy to use, official website address: http://phpexcel.codeplex.com/
1. read the content of the xls file
SetReadDataOnly (true); $ xlsReader-> setLoadSheetsOnly (true); $ sheets = $ xlsReader-> load ($ file); $ content = $ sheets-> getSheet (0) -> toArray (); // read the first worksheet (note that the number starts from 0). If you read multiple worksheets, you can create a loop of 0, 1, 2, 3 .... // Obtain a two-dimensional array. each small array is the data returned $ content;} // $ type = 'excel2007 'in each column of the excel table content '; // Set the Excel type Excel5 (2003 or earlier) or Excel2007 $ type = 'excel5'; $ content = readxls('data.xls ', $ type); echo''; var_dump($content); echo '
';?>
2. write content to the xls file
GetProperties ()-> setCreator ("yun shu") //-> setLastModifiedBy ("yun shu") //-> setTitle ("product URL export ") //-> setSubject ("product URL export") //-> setDescription ("product URL export") //-> setKeywords ("product URL export "); $ objPHPExcel-> setActiveSheetIndex (0); $ cols = 'abcdefghijklmnopqrstuvwxyz '; // set the title for ($ I = 0, $ length = count ($ title ); $ I <$ length; $ I ++) {// echo $ cols {$ I }. '1'; $ objPHPExcel-> getActiveSheet ()-> setCellValue ($ cols {$ I }. '1', $ title [$ I]);} // set the title style $ titleCount = count ($ title); $ r = $ cols {0 }. '1'; $ c = $ cols {$ titleCount }. '1'; $ objPHPExcel-> getActiveSheet ()-> getStyle ("$ r: $ c ") -> applyFromArray (array ('font' => array ('bold '=> true), 'alignment' => array ('horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_RIGHT ,), 'borders' => array ('top' => array ('style' => PHPExcel_Style_Border: BORDER_THIN), 'fill' => array ('type' => PHPExcel_Style_Fill:: FILL_GRADIENT_LINEAR, 'rotation' => 90, 'startcolor' => array ('Arg' => 'ffa0a0 '), 'endcolor' => array ('Arg' => 'ffffff'); for ($ I = 0, $ length = count ($ data ); $ I <$ length; $ I ++) {$ j = 0; foreach ($ data [$ I] as $ v) {// use foreach here, supports associating arrays and numeric index arrays $ objPHPExcel-> getActiveSheet ()-> setCellValue ($ cols {$ j }. ($ I + 2), $ v); $ j ++ ;}/// generate the xls file header ('content-Type: application/vnd. ms-excel '); header ('content-Disposition: attachment; filename = "'.w.filename.'.xls"'); header ('cache-Control: max-age = 0 '); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save ('php: // output ');} $ array = array (1111, 'name', 'brand', 'commodity name ',' http://www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name ',' http://www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name ',' http://www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name ',' http://www.baidu.com '), Array (1111, 'name', 'brand', 'commodity name ',' http://www.baidu.com '),); Write_xls ($ array, array ('item ID', 'Supplier name', 'brand', 'item name', 'URL '), 'report');?>
3. obtain the content to be written by operating the database. for example, you can use mysqli preprocessing to obtain the content:
'Mysql', 'DB _ host' => 'localhost', 'DB _ name' => 'test', 'DB _ user' => 'root ', 'db _ pwd' => 'root', 'DB _ port' => '000000',); function getProductIdByName ($ name) {global $ config; $ id = false; $ mysqli = new mysqli ($ config ['DB _ host'], $ config ['DB _ user'], $ config ['DB _ pwd'], $ config ['DB _ name']); if (mysqli_connect_error () {// Compatible with <php5.2.9 OO way: $ mysqli-> connect_error die ("connection failed, error code: ". mysqli_connect_errno (). "error message :". m Ysqli_connect_error ();} // Set the encoding of the connected Database. do not forget to set $ mysqli-> set_charset ("gbk"); // The encoding of Chinese characters must be consistent with that of the database, if not set, the result is null $ name = iconv ("UTF-8", "gbk // IGNORE", $ name ); if ($ mysqli_stmt = $ mysqli-> prepare ("select id from 137_product where name like? ") {$ Mysqli_stmt-> bind_param (" s ", $ name); $ mysqli_stmt-> execute (); $ mysqli_stmt-> bind_result ($ id ); $ mysqli_stmt-> fetch (); $ mysqli_stmt-> close () ;}$ mysqli-> close (); return $ id ;} $ id = getProductIdByName ('% Chennai bathroom inner split toilet %'); var_dump ($ id);?>
OK...
References: http://blog.sina.com.cn/s/blog_44b3f96d0101cczo.htmlhttp://phpexcel.codeplex.com/
Reading, Reading Chinese xls, csv files will have problems, find the information online, found PHPExcel class library easy to use, official website address: http://phpexcel.codeplex.com/1, read...