飞道的博客

thinkphp6 + phpexcel 导入导出数据,设置特殊表格

305人阅读  评论(0)

第一步:安装excel,使用composer安装,我的是在window下,直接cmd切换到项目下面,输入

composer require phpoffice/phpexcel,然后就等待安装完成。如下图:

第二步:引入相关类


  
  1. <?php
  2. namespace app\ admin\ controller;
  3. use app\ admin\ model\ Bingli;
  4. use app\ admin\ model\ Moneyinfo;
  5. use think\ Controller;
  6. use think\ Validate;
  7. use think\ facade\ Request;
  8. use think\ facade\ Db;
  9. use think\ facade\ Session;
  10. use think\ facade\ View;
  11. use PHPExcel_IOFactory; //这个是三方类
  12. class Binglii extends Base
  13. {
  14. /*
  15. * 批量导入数据详情
  16. */
  17. public function upAgent(){
  18. if(Request::param( 'html') == false){
  19. // 获取表单上传文件
  20. $file = request()->file( 'file');
  21. if( empty($file)){
  22. return json([ 'info'=> '请选择上传文件!', 'status'=> 0]);
  23. }
  24. // 移动到框架应用根目录/public/upload/ 目录下,并修改文件名为时间戳
  25. $savename = \think\facade\Filesystem::putFile( 'excel', $file, 'time');
  26. // 文件名称
  27. $info = explode( '/', $savename);
  28. $file = public_path(). 'public/upload/excel/'.$info[ 'excel'];
  29. //导入
  30. $objPHPExcel = PHPExcel_IOFactory::load($file); //获取sheet表格数目
  31. //$objReader = PHPExcel_IOFactory::createReader('Excel5');
  32. //$objPHPExcel = $objReader->load($file,$encode='utf-8');
  33. $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
  34. $sheetSelected = 0;
  35. $objPHPExcel->setActiveSheetIndex($sheetSelected);
  36. //获取表格行数
  37. $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow();
  38. //获取表格列数
  39. $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
  40. $dataArr = array();
  41. /* 循环读取每个单元格的数据 */
  42. for ($i = 2; $i <= $rowCount; $i++) {
  43. $data[ 'mi_num'] = $objPHPExcel->getActiveSheet()->getCell( "D3")->getValue();
  44. $data[ 'mi_time'] = $objPHPExcel->getActiveSheet()->getCell( "B".$i)->getFormattedValue(); // getFormattedValue 获取本来的格式
  45. $data[ 'mi_chargeItems'] = $objPHPExcel->getActiveSheet()->getCell( "E".$i)->getValue();
  46. $data[ 'mi_unit'] = $objPHPExcel->getActiveSheet()->getCell( "M".$i)->getValue();
  47. $data[ 'mi_quantity'] = $objPHPExcel->getActiveSheet()->getCell( "O".$i)->getValue();
  48. $data[ 'mi_unitPrice'] = $objPHPExcel->getActiveSheet()->getCell( "R".$i)->getValue();
  49. $data[ 'mi_money'] = $objPHPExcel->getActiveSheet()->getCell( "V".$i)->getValue();
  50. $data[ 'mi_payCode'] = $objPHPExcel->getActiveSheet()->getCell( "X".$i)->getValue();
  51. $data[ 'mi_cityCode'] = $objPHPExcel->getActiveSheet()->getCell( "AD".$i)->getValue();
  52. $res[] = $data; //数据赋值到数组
  53. }
  54. $lennum = 400; // 400条数据插入一次
  55. //记录一共插入了多少条数据
  56. $insertCount = 0;
  57. $count = count($res); //总共多少条数据
  58. $limit = ceil($count/$lennum); //需要执行多少次插入数据的操作
  59. for($i = 1;$i <= $limit; $i++){
  60. $offset = ($i -1)*$lennum; //当前是第几次遍历,第一条数据是哪一条
  61. //从数组的第几条开始本次数据插入
  62. $datac = array_slice($res,$offset,$lennum);
  63. //模型可以插入大批量的数据
  64. $moneyinfo = new Moneyinfo();
  65. $result = $moneyinfo->saveAll($datac);
  66. $insertCount = count($result) + $insertCount;
  67. }
  68. //删除excel文件
  69. unlink($file);
  70. if($insertCount > 0){
  71. return json([ 'info'=> '文件上传成功,已经导入'.$insertCount. '条数据', 'status'=> 1]);
  72. } else{
  73. return json([ 'info'=> '导入第'.$insertCount. '条失败', 'status'=> 0]);
  74. }
  75. }
  76. }
  77. //数据导出
  78. public function downCsvB(){
  79. //实例化PHPExcel类
  80. $objPHPExcel = new \PHPExcel();
  81. //激活当前的sheet表
  82. $objPHPExcel->setActiveSheetIndex( 0);
  83. //数据
  84. $info = Request::param();
  85. $id = $info[ 'id'];
  86. $map1[] = [ 'bl_num', '=',$id];
  87. $map2[] = [ 'mi_num', '=',$id];
  88. $map3[] = [ 'mi_num', '=',$id];
  89. $data1 = Db::table( 'zdb_bingli')->field( 'bl_name,bl_depart,bl_bed,bl_socialSecNum,bl_socialSecPayment,bl_balance,bl_admissionTime,bl_dischargeTime')->where($map1)->find();
  90. //引入model
  91. $moneyinfoModel = new Moneyinfo();
  92. $data = $moneyinfoModel->tab1( '', '',$id);
  93. $maxTime = Db::table( 'zdb_moneyinfo')->where($map3)->order( 'mi_time asc')->limit( 0, 1)->value( 'mi_time');
  94. $minTime = Db::table( 'zdb_moneyinfo')->where($map3)->order( 'mi_time desc')->limit( 0, 1)->value( 'mi_time');
  95. $totalm = Db::table( 'zdb_moneyinfo')->where($map2)->sum( 'mi_money'); //总金额
  96. $totalm = round($totalm, 2); //四舍五入
  97. $time1 = empty($maxTime) ? $data1[ 'bl_admissionTime'] : $maxTime; //时间段
  98. $time2 = empty($minTime) ? $data1[ 'bl_dischargeTime'] : $minTime; //时间段
  99. //设置表格头(即excel表格的第一行)
  100. // 合并 单元格
  101. $objPHPExcel->getActiveSheet()->mergeCells( 'A1:H1');
  102. $objPHPExcel->getActiveSheet()->mergeCells( 'E2:F2');
  103. $objPHPExcel->getActiveSheet()->mergeCells( 'C3:G3');
  104. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'A1', 'xxxxxxxxxxx费用明细清单');
  105. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'A2', '姓名:'.$data1[ 'bl_name']);
  106. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'B2', '病区:');
  107. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'C2', $data1[ 'bl_depart']);
  108. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'E2', '社保号:'.$data1[ 'bl_socialSecNum']);
  109. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'H2', '床号:'.$data1[ 'bl_bed']);
  110. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'A3', '住院号:'.$id);
  111. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'B3', '时间段:');
  112. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'C3', date( 'Y/m/d H:i',$time1) . '至'. date( 'Y/m/d H:i',$time2));
  113. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'H3', '病人签字:');
  114. //表头
  115. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'A4', '业务日期')->SetCellValue( 'B4', '收费项目')->SetCellValue( 'C4', '单位')->SetCellValue( 'D4', '数量')->SetCellValue( 'E4', '单价')->SetCellValue( 'F4', '金额')->SetCellValue( 'G4', '收费项目编码')->SetCellValue( 'H4', '国家编码');
  116. $styleThinBlackBorder = array(
  117. 'borders' => array(
  118. 'allborders' => array( //设置全部边框
  119. 'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
  120. ),
  121. ),
  122. );
  123. //边框设置
  124. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'A4:H4')->applyFromArray($styleThinBlackBorder);
  125. //循环刚取出来的数组,将数据逐一添加到excel表格。
  126. $num = 4;
  127. for ($i = 0; $i < count($data); $i++) {
  128. $objPHPExcel->getActiveSheet()->SetCellValue( 'A'.($i + 5),date( 'Y-m-d H:i',$data[$i][ 'mi_time']));
  129. $objPHPExcel->getActiveSheet()->SetCellValue( 'B'.($i + 5),$data[$i][ 'mi_chargeItems']);
  130. $objPHPExcel->getActiveSheet()->SetCellValue( 'C'.($i + 5),$data[$i][ 'mi_unit']);
  131. $objPHPExcel->getActiveSheet()->SetCellValue( 'D'.($i + 5),$data[$i][ 'mi_quantity']);
  132. $objPHPExcel->getActiveSheet()->SetCellValue( 'E'.($i + 5),$data[$i][ 'mi_unitPrice']);
  133. $objPHPExcel->getActiveSheet()->SetCellValue( 'F'.($i + 5),$data[$i][ 'mi_money']);
  134. $objPHPExcel->getActiveSheet()->SetCellValue( 'G'.($i + 5), ' '.$data[$i][ 'mi_payCode']);
  135. $objPHPExcel->getActiveSheet()->SetCellValue( 'H'.($i + 5), ' '.$data[$i][ 'mi_cityCode']);
  136. $num++;
  137. //边框设置
  138. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'A'.($i + 5). ':H'.($i + 5))->applyFromArray($styleThinBlackBorder);
  139. }
  140. // 合并 单元格
  141. $objPHPExcel->getActiveSheet()->mergeCells( 'C'.($num+ 2). ':D'.($num+ 2));
  142. $objPHPExcel->getActiveSheet()->mergeCells( 'E'.($num+ 2). ':F'.($num+ 2));
  143. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'E'.($num+ 1), '合计');
  144. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'F'.($num+ 1), $totalm);
  145. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'B'.($num+ 2), '交款:'.($totalm - $data1[ 'bl_socialSecPayment']));
  146. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'C'.($num+ 2), '入院总费用:'.$totalm);
  147. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'E'.($num+ 2), '结算报销:'.$data1[ 'bl_socialSecPayment']);
  148. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'G'.($num+ 2), '余额:'.$data1[ 'bl_balance']);
  149. $objPHPExcel->getActiveSheet( 0)->SetCellValue( 'A'.($num+ 3), '制表人:'. $this->uname);
  150. // 水平居中
  151. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  152. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'A4:H4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  153. //右对齐
  154. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  155. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  156. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'B3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  157. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'E'.($num+ 2). ':G'.($num+ 2))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  158. //设置单元格宽度
  159. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'A')->setWidth( 20);
  160. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'B')->setWidth( 25);
  161. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'C')->setWidth( 10);
  162. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'D')->setWidth( 10);
  163. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'E')->setWidth( 10);
  164. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'F')->setWidth( 10);
  165. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'G')->setWidth( 20);
  166. $objPHPExcel->setActiveSheetIndex( 0)->getColumnDimension( 'H')->setWidth( 28);
  167. // 设置行高度
  168. $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight( 15); //设置默认行高
  169. $objPHPExcel->getActiveSheet()->getRowDimension( '1')->setRowHeight( 20); //第一行行高
  170. $objPHPExcel->getActiveSheet()->getRowDimension(($num+ 1))->setRowHeight( 20);
  171. $objPHPExcel->getActiveSheet()->getRowDimension(($num+ 2))->setRowHeight( 20);
  172. $objPHPExcel->getActiveSheet()->getRowDimension(($num+ 3))->setRowHeight( 20);
  173. // 字体大小和样式
  174. //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(9);
  175. //第一行是否加粗
  176. $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getFont()->setBold( true);
  177. $styleThinBlackBorderTB = array(
  178. 'borders' => array(
  179. 'top' => array( // 设置顶部边框
  180. 'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
  181. ),
  182. 'bottom' => array( //设置底部边框
  183. 'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
  184. ),
  185. ),
  186. );
  187. // 边框
  188. $objPHPExcel->setActiveSheetIndex( 0)->getStyle( 'A'.($num+ 2). ':H'.($num+ 2))->applyFromArray($styleThinBlackBorderTB);
  189. // 设置垂直居中
  190. $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  191. //设置保存的Excel表格名称
  192. $filename = 'user'.date( 'Ymd_his'). '.xls';
  193. //设置当前激活的sheet表格名称
  194. $objPHPExcel->getActiveSheet()->setTitle( 'user');
  195. iconv( "utf-8", "gb2312", $filename); //解决乱码的问题
  196. ob_end_clean(); //解决乱码核心
  197. //设置浏览器窗口下载表格
  198. header( "Content-Type: application/force-download");
  199. header( "Content-Type: application/octet-stream");
  200. header( "Content-Type: application/download");
  201. header( 'Content-Disposition:inline;filename="' . $filename . '"');
  202. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //生成excel文件
  203. //下载文件在浏览器窗口
  204. $objWriter->save( 'php://output');
  205. exit();
  206. }
  207. }

第三步:Model类


  
  1. <?php
  2. namespace app\ admin\ model;
  3. use think\ Model;
  4. /**
  5. * @mixin think\Model
  6. */
  7. class Moneyinfo extends Model
  8. {
  9. public function tab1($startTime = 0,$endTime = 0,$mi_num = 0,$mi_chargeItems = null){
  10. //查询条件
  11. if(! empty($startTime) && ! empty($endTime)){ //时间
  12. $startTime = strtotime($startTime);
  13. $endTime = strtotime($endTime);
  14. $map[] = [ 'mi_time', 'between',[$startTime,$endTime]];
  15. }
  16. //查询条件
  17. if(! empty($mi_chargeItems)){ //项目
  18. $map[] = [ 'mi_chargeItems', 'like', '%'.$mi_chargeItems. '%'];
  19. }
  20. $map[] = [ 'mi_num', '=',$mi_num];
  21. //查询 Moneyinfo 是数据表名
  22. $res = Moneyinfo::field( "mi_num,mi_time,mi_chargeItems,mi_unit,mi_quantity,mi_unitPrice,mi_money,mi_payCode,mi_cityCode")->where($map)->select()->toArray();
  23. //echo Moneyinfo::getLastSql();
  24. //返回数据
  25. return $res;
  26. }
  27. }

关于getBottom不生效,看了一下源代码,改了一下

原写法:$objPHPExcel->getActiveSheet()->getStyle('A1')->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)

正确写法参考上面数据导出的底部边框。

导出的数据结构如下:


转载:https://blog.csdn.net/qq_37682202/article/details/117400130
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场