PHP怎样将查询出来的数据导出成excel表格

PHP怎样将查询出来的数据导出成excel表格,第1张

根据下列编码程序可以。

1/ 批量导出数据 @param $arr 从数据库查询出来,即要导出的数据  $name excel表歌名/

2function expExcel($arr,$name){ require_once 'PHPExcelphp';

3 //实例化 $objPHPExcel = new PHPExcel(); /右键属性所显示的信息/

4$objPHPExcel->getProperties()->setCreator("zxf")  // ->setLastModifiedBy("zxf")  //最后一  ->setTitle('数据EXCEL导出')  //标题->setSubject('数据EXCEL导出') //主题>setDescription('导出数据')  //描>setKeywords("excel")   //标记>setCategory("result file");  //类别

5 //设置当前的表格  $objPHPExcel->setActiveSheetIndex(0);// 设置表格第一行显示内容$objPHPExcel->getActiveSheet()  ->setCellValue('A1', '业主姓名') ->setCellValue('B1', '密码')->setCellValue('C1', '手机号码'  ->setCellValue('D1', '地址')

6//设置第一行为红色字体 ->getStyle('A1:D1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);$key = 1; /以下就是对处理Excel里的数据。

下载PHPExcel后保存到自己的类文件目录中,然后使用以下代码可以打开Excel 2007(xlsx)格式的文件:

require_once '/libs/PHPExcel-180/Classes/PHPExcelphp';     //修改为自己的目录

echo '<p>TEST PHPExcel 180: read xlsx file</p>';

$objReader = PHPExcel_IOFactory::createReaderForFile($filename);

$objPHPExcel = $objReader->load($filename);

$objPHPExcel->setActiveSheetIndex(1);

$date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();

读出以后再根据内容写入数据库

用 php Win32 OLE

##Using OLE;

read('Book1xls');

// print number of rows, columns and sheets

echo "Number of sheets: " sizeof($excel->sheets) "\n";

for ($x=0; $xsheets); $x++) {

echo "Number of rows in sheet " ($x+1) ": " $excel->sheets[$x]["numRows"] "\n";

echo "Number of columns in sheet " ($x+1) ": " $excel->sheets[$x]["numCols"] "\n";

1 $fp = fopen('php://output', 'a');

2

3 // 输出Excel列名信息

4 $head = array("邮件");

5 foreach ($head as $i => $v) {

6 // CSV的Excel支持GBK编码,一定要转换,否则乱码

7 $head[$i] = iconv('utf-8', 'gbk', $v);

8 }

9

10 // 将数据通过fputcsv写到文件句柄

11 fputcsv($fp, $head);

12

13 // 计数器

14 $cnt = 0;

15 // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小

16 $limit = 100000;

17

18 // 逐行取出数据,不浪费内存

19 $count = count($email);

20

21 for($t=0;$t<$count;$t++) {

22

23 $cnt ++;

24 if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题

25 ob_flush();

26 flush();

27 $cnt = 0;

28 }

29 $row[] = $email[$t];

30 foreach ($row as $i => $v) {

31 $row[$i] = iconv('utf-8', 'gbk', $v);

32 }

33 fputcsv($fp, $row);

34 unset($row);

35 }

public function insert_excel(){

     set_time_limit(0);

   require_once '/data/excel/PHPExcelphp';

       $objReader = PHPExcel_IOFactory::createReader('Excel5');

       $objReader->setReadDataOnly(true);

       $objPHPExcel = $objReader->load("/Public/Book1xls");

       $objWorksheet = $objPHPExcel->getSheet(0);

       $highestRow = $objWorksheet->getHighestRow();

       $highestColumn = $objWorksheet->getHighestColumn();

       $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

       $excelData = array();

   //注意列数和行数的起始位置

       for ($row = 2; $row <= $highestRow;$row++){

           for ($col=0;$col<$highestColumnIndex;$col++) {

               $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();

           }

       } 

$map = M('map_info');

//获取该地图的;

foreach($excelData as $key => $val){

//此处需要对自定义的字段序列map_map_customcid

$result = curl_get('> }

上面是导入数据

public function export_excel(){

$map_info = D("MapInfo");

        $mid = intval($_GET["mid"]);

        $mapinformation = M("map_info");

        

$search_type = $_REQUEST['search_type'];

$keywords = t($_REQUEST['keywords']);

$status = $_REQUEST['status'];

if(!empty($status) || $status === '0'){

$where['status'] = $status;

$map['status'] = $status;

}

$where['uid'] = $this->uid;

$where['is_del'] = 0;

if(empty($search_type) && !empty($_REQUEST['keywords'])){

$map['search_type'] = $search_type;

$map['keywords'] = $keywords;

$_search_where['title']  = array('like',"%"$keywords"%");

$_search_where['contents']  = array('like',"%"$keywords"%");

$_search_where['_logic'] = 'or';

$where['_complex'] = $_search_where;

}elseif(!empty($search_type) && !empty($_REQUEST['keywords'])){

$map['search_type'] = $search_type;

$map['keywords'] = $keywords;

$where[$search_type] = array('like',"%"$keywords"%");;

}

$map = D('Map');

if($mid){

$where['mid'] = $mid;

$excel_title = $map->where("mid=$mid") -> getField('title');

$map_info_field = "title,contents,map_info";

}else{

$excel_title = "我的全部地图数据";

$map_info_field = "title,contents,mid";

}

$info_list = $map_info -> getAllMapInfo($mid,$where,$map_info_field);

header("Content-type:application/vndms-excel");

header("Content-Disposition:filename="$excel_title"-"date("Ymd",time())"xls");

echo iconv('utf-8', 'gb2312', '标题'); echo  "\t";

echo iconv('utf-8', 'gb2312', '简介'); echo  "\t";

if(!$mid){

echo iconv('utf-8', 'gb2312', '地图名称'); echo  "\t";

}else{

$custom_list = D('MapCustom') -> getCustomList($mid);

foreach($custom_list as $k =>$v){

if($v['title']){

echo iconv('utf-8', 'gb2312', $v['title']); echo  "\t";

}

}

}

echo "\n";

$order = array("'•'","'\r\n'", "'\n'", "'\r'","'\s+'");//正则匹配回车,换行,空格

$replace = array('','','','','');

foreach($info_list as $key => $val){

echo mb_convert_encoding(preg_replace($order,$replace,strip_tags($val['title']) ) ,'gb2312', 'utf-8'); echo  "\t";

echo mb_convert_encoding(preg_replace($order,$replace,strip_tags($val['contents']) ) ,'gb2312', 'utf-8'); echo  "\t";

if(!$mid){

$map_title = $map->where("mid="$val['mid'])->getField('title');

echo iconv('utf-8', 'gb2312', preg_replace($order,$replace,$map_title) ); echo "\t";

echo "\n";

}else{

foreach($custom_list as $ke => $va){

echo iconv('utf-8', 'gb2312', preg_replace($order,$replace, strip_tags( $val['map_info'][$va['cid']] ) ) ); echo  "\t";

}

echo "\n";

}

}

}

PHPEXCEL类最好自己下载,我只是提交一个文件而已

以上就是关于PHP怎样将查询出来的数据导出成excel表格全部的内容,包括:PHP怎样将查询出来的数据导出成excel表格、php 5.4 如何读取excel中的列名跟值,导入到数据库中形成“列1,值1,列2,值2......”这种样式、php读取excel并写入到数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9747616.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存