dedecms后台增加批量上传和导出报表的功能

2024年04月07日 dedecms 后台 增加 批量 上传 导出 报表 懒猪技术

1、导出csv文件

01 <?php

02 require_once (dirname(__FILE__) . "/../include/common.inc.php");

03 $output = fopen('','w') or die("can't open ");

04 header('Content-Type: application/csv');

05 header('Content-Disposition: attachment; filename="认证列表.csv"');

06 $arrkeys = array(

07 iconv('utf-8','gb2312','姓名'),

08 iconv('utf-8','gb2312','电话')//文件表头

09 );

10 fputcsv($output, $arrkeys);

11 //取得符合条件的数组

12 $sql = "SELECT * FROM dede_test";

13 $dsql->Execute('me',$sql);

14 while($row = $dsql->GetArray('me')){

15 $name =$row['name'];

16 $mobile= $row['mobile']; //循环

17

18 $arrkeys = array(

19 iconv('utf-8','gb2312',$name),

20 iconv('utf-8','gb2312',$mobile)

21 );

22 fputcsv($output, $arrkeys);

23 }

24

25 fclose($output) or die("can't close ");

26 exit();

27 ?>


2、导入xls文件

01 <?php

02 require_once (dirname(__FILE__) . "/../include/common.inc.php");

03 include_once("excel/reader.php");

04 $tmp = $_FILES['file']['tmp_name'];

05 if (empty ($tmp)) {

06 ShowMsg("请选择要导入的Excel文件!","test.php");

07 exit;

08 }

09

10 $save_path = "xls/";

11 $file_name = $save_path.date('Ymdhis') . ".xls";

12 if (copy($tmp, $file_name)) {

13 $xls = new Spreadsheet_Excel_Reader();

14 $xls->setOutputEncoding('utf-8');

15 $xls->read($file_name);

16 for ($i=2; $i<=$xls->sheets[0]['numRows']; $i++) {

17 $name = $xls->sheets[0]['cells'][$i][1];

18 $mobile = $xls->sheets[0]['cells'][$i][2]

19 $data_values .= "('$name','$mobile'),";

20 }

21 $data_values = substr($data_values,0,-1);

22 $sql= "INSERT INTO dede_test (name,mobile) VALUES $data_values";

23 $dsql->ExecuteNoneQuery($sql);

24 $lastInsertID = $dsql->GetLastID();

25 if($dsql){

26 ShowMsg("成功导入!","test.php");

27 }else{

28 ShowMsg("导入失败!","test.php");

29 }

30 }

31 exit();

32 ?>


dede后台导出demo,点击下载 PHP导出xls

本文链接:http://so.lmcjl.com/news/1419/

展开阅读全文
相关内容