In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to export and import excel files from PHP web pages". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to export and import excel files from PHP web pages.
Export and import form code:
Export
Excel form:
Excel export:
$query = mysql_query ("select * from user limit 50"); $I = 0; $list = array (); while ($row=mysql_fetch_array ($query)) {$list [$I] ['id'] = $row [' id']; $list [$I] ['username'] = $row [' username']; $list [$I] ['password'] = $row [' password']; $title = array ('ID',' mailbox', 'password') / / set the header exportExcel of excel to be exported ($list, 'material Fire user Table', $title)
ExportExcel method code:
Function exportExcel ($data, $savefile = null, $title = null, $sheetname = 'sheet1') {require_once' PHPExcel.class.php'; / / current timestamp if (is_null ($savefile) if no file name is specified) {$savefile = time () } / / if the excel header is specified, append the form to the body content to if (is_array ($title)) {array_unshift ($data, $title);} $objPHPExcel = new PHPExcel (); / / Excel content $head_num = count ($data); foreach ($data as $k = > $v) {$obj = $objPHPExcel- > setActiveSheetIndex (0); $row = $k + 1 / / Line $nn = 0; foreach ($v as $vv) {$col = chr (65 + $nn); / / column $obj- > setCellValue ($col. $row, $vv); / / column, row, value $nn++;}} / / set column header for ($I = 0; $I
< $head_num - 1; $i++) { $alpha = chr(65 + $i); $objPHPExcel->GetActiveSheet ()-> getColumnDimension ($alpha)-> setAutoSize (true); / / Cell width adaptive $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getFont ()-> setName ("Candara"); / / set font $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getFont ()-> setSize (12); / / set the size $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color::COLOR_BLACK); / / set color $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER); / / horizontal center $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment::VERTICAL_CENTER); / / Vertical center $objPHPExcel- > getActiveSheet ()-> getStyle ($alpha. '1')-> getFont ()-> setBold (true); / / bold} $objPHPExcel- > getActiveSheet ()-> setTitle ($sheetname); / / title $objPHPExcel- > setActiveSheetIndex (0); / / set current sheet header (' Content-Type: application/vnd.ms-excel'); header ('Content-Disposition: attachment;filename='. $savefile. '.xls "); / / File name header (' Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel5'); / / Excel5 $objWriter- > save (' php://output');}
Excel Import:
$tmp = $_ FILES ['file'] [' tmp_name']; if (empty ($tmp)) {echo 'Please select the Excel file to import!' ; exit;} $save_path = "uploads/"; $filename = $save_path. Date ('Ymdhis'). ".xls"; / / the saved path and name of the uploaded file if (copy ($tmp, $filename)) {require_once 'PHPExcel.class.php'; require_once' PHPExcel/Reader/Excel5.php'; $PHPReader = new PHPExcel_Reader_Excel5 (); / / PHPExcel_Reader_Excel2007 PHPExcel_Reader_Excel5 / / load file $PHPExcel = $PHPReader- > load ($filename) / / get the first worksheet in the table. If you want to get the second, change 0 to 1, and so on $currentSheet = $PHPExcel- > getSheet (0); / / get the total number of columns $allColumn = $currentSheet- > getHighestColumn (); / / get the total number of rows $allRow = $currentSheet- > getHighestRow () / / cycle to get the data in the table. $currentRow represents the current row. From which row to read the data, the index value starts at 0 for ($currentRow = 1; $currentRow getValue ();}} $add_time = date ('Y-m-d add_time, time () Foreach ($data as $k = > $v) {if ($k > 1) {$sql = "insert into user (username,password) values ('". $v ['B']. "','". $v ['C']. "')"; mysql_query ($sql);}} $sql = "SELECT * FROM user"; $result = mysql_query ($sql); $tip = "user imported successfully". Oh, now. Mysql_num_rows ($result). 'There 's a piece of data!' ; echo "alert ('". $tip. "'); history.go (- 1);"; exit;} at this point, I believe you have a better understanding of "how to export and import excel files from PHP web pages". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.