php导出excel进度条显⽰
下载⼤量数据到Excel表格时,⽆法查看下载的实时进度。
pl sql编程效果展⽰:
1、统计数据数量:
2、显⽰数量:
3、下载进度条展⽰:
代码:
1、前端显⽰代码
[html]
1. <link rel="stylesheet"href="__PUBLIC__/javascripts/admin/artdialog/skins/default.css"type="text/css"
thingjs开发文档2. <script type="text/javascript"src="__PUBLIC__/javascripts/admin/jquery-
1.7.
2.min.js"></script>
3. <script rel="stylesheet"src="__PUBLIC__/javascripts/admin/artdialog/jquery.artDialog.js"></script
4. <script rel="stylesheet"src="__PUBLIC__/javascripts/admin/artdialog/plugins/iframeTools.source.js"
5.
6. <div class="content-search">
7. <form action="{:U('/Sale/ImportB2bHistory/downloadOrders')}"method="get"class="form-
inline"id="downloadOrders"target="_blank">
8. <div class="control-group">
9. <label class="control-label">发货⽇期
10. <input type="datetime"placeholder="起始⽇期"name="begin_time"value="
{$condition.begin_time}"id="begin_time"
11. onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"
12. class="Wdate"/>
js逆向课程13. </label>
14. <label class="control-label">发货⽇期
15. <input type="datetime"placeholder="结束⽇期"name="end_time"value="
{$d_time}"id="end_time"
16. onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"
17. class="Wdate"/>
18. </label>
19. </div>
20. <div>
21. <button class="btn btn-
primary"type="button"onclick="processBar()">
22. <i class="icon-download icon-white"></i>导出
23. </button>
24. </div>
25. </form>
26. </div>
27. <script type="text/javascript"language="javascript">
28. var search_uri = "{:U('downloadOrders')}";// 请求地址
29. var process_uri = "{:U('getProcess')}";// 请求地址
29. var process_uri = "{:U('getProcess')}";// 请求地址
30. var win = p; // 顶层页⾯window对象
31. var processBar2 ;
32.
33. // processBar函数如下
34. function processBar() {
35. tip();// 弹出统计总数对话框
36. doRequest();
37. }
38.
39. function doRequest() {
jquery下载文件请求40. var begin_time = $("#begin_time").val();
41. var end_time = $("#end_time").val();
42.
43. //ajax请求总数
44. $.post(search_uri, {begin_time: begin_time, end_time: end_time}, function (json) {
45. var return_arr = jQuery.parseJSON(json);
46. var num = al;// 任务总数
47. var processnum = return_arr.processnum;// 当前数
48.
49. if (num == 0) {
50. poptip.title('系统消息');
51. t('查询结果为空,不能进⾏数据导出');
52. } else {
53. poptip.close();// 关闭提⽰框
54. doConfirm(num, processnum);// 弹出确认下载交互界⾯
55. }
56. });
57. }
58.
59. // 弹出确认下载
60. function doConfirm(num, processnum) {
61. var msg = '总有 <span >' + num
62. + '</span>⾏,可能会时间较长。<br/>您还需要继续吗?';
63. var content = '<progress max="100" value="0" id="processbar" style="text-
align: center;width: 300px;"></progress><br/>' +
64. '<span id="progress_info"></span>';
65.
66. // 提⽰是否继续
67. firm(msg, function () {
68. // 弹出进度条
69. showProcess = art.dialog.through({
70. title: '数据导出进度',
71. content: content,
72. lock: true,
73. width: 350,
74. opacity: '.1',
75. button: [
76. {
77. name: '取消',
78. callback: function () {
79. load();
80. }
81. }
82. ]
83. });
84. showProcess.show();
85.
86. win.$('#processbar').val(processnum * 100 / num);
87. win.$('#progress_info').show().text('正在导出,请等待,请勿关闭窗⼝...');//当继续的时
候
88.
89. $.ajax({
90. url:"{:U('doDownloadOrders')}",
91. type:"post",
92. async:true,
93. error:function (data) {
94. alert("下载失败!");
95. },
96. success:function (data) {
97. clearInterval(processBar2);// 关闭⾃动定时执⾏
98. showProcess.close();
99. window.location.href = data;
100. }
101. });
102.
103. processBar2 = setInterval(sync_process, 1000);// 每隔1秒执⾏⼀次
rank函数怎么填104. });
105. }
106.
107. // 获取进度
2、服务器代码
[php]
1. /**
2. * 导出客户订单下载
3. */
4. public function downloadOrders(){
5. // 查询条件
6. $condition = I('POST.',null,'trim');
7. $datas = $this->getDatas($condition);
8.
9.
10. S('PROCESS_DOWNLOAD_DATA',$datas,60);// 缓存数据
11. S('PROCESS_DOWNLOAD_TOTAL',count($datas),60);
12. S('PROCESS_DOWNLOAD_PROCESSNUM',0);
13.
14. $re_arr = array(
15. 'total' => count($datas),
16. 'processnum' => 0,
17. );
18. echo json_encode($re_arr);
19. exit;
20. }
21.
22. /**
23. * 获取进度条的执⾏进度数据
24. */
25. public function getProcess(){
26. $re_arr = array(
27. 'total' => S('PROCESS_DOWNLOAD_TOTAL'),
28. 'processnum' => S('PROCESS_DOWNLOAD_PROCESSNUM')
29. );
30.
31. echo json_encode($re_arr);
32. exit;
33. }
34.
35.
36. /**
37. * ⽣成要下载的⽂件
38. */
39. public function doDownloadOrders(){
40. $path = 'Cache/Runtime/Admin/downloads/';
41. if (!is_dir($path)) {
42. @mkdir($path, 0777, true);
43. }
44.
45. set_time_limit(0);
46. session_write_close();// 断开AJAX请求,⽆需等待响应
47. $datas = S('PROCESS_DOWNLOAD_DATA');
48.
49. // 设置表头字段并指定展⽰顺序
开源代码能干嘛50. $firstLine = array('A','B','C');
51.
52. import("Org.Excel.PHPExcel");// 导⼊扩展类
JS 发起请求后必须等待服务器返回结果才向下执⾏,才会解析后⾯请求的结果,这样导致下载数据到Excel ⽂件中时不能再次发起请求读取下载进度。
在此session_write_close()函数端⼝AJAX 请求,⽆需等待服务器返回结果,向下执⾏,该请求会正常执⾏完毕。52. import("Org.Excel.PHPExcel");// 导⼊扩展类 53. $objPHPExcel = new \PHPExc
el();// 实例化类 54. 55. $objPHPExcel ->getProperties() 56. ->setCreator("123") 57. ->setLastModifiedBy("123") 58. ->setTitle("Office 2003 XLSX Test Document") 59. ->setSubject("Office 2003 XLSX Test Document") 60. ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.") 61. ->setKeywords("office 2003 openxml php") 62. ->setCategory("Test result file"); 63. 64. $objActSheet = $objPHPExcel ->setActiveSheetIndex(0);// 设置当前的sheet 65. 66. // EXCEL 表头 67. if ($firstLine ){ 68. $r = 'A'; 69. foreach ($firstLine as $v ){ 70. $objActSheet ->setCellValue($r .'1',$v ); 71. $r ++; 72. } 73. } 74. $i = 2; 75. foreach ($datas as $value ) 76. { 77. $r = 'A'; 78. foreach ($value as $value2) 79. { 80. if ($r == 'B' || $r == 'P' || $r == 'Q'){ 81.
$objActSheet ->setCellValueExplicit($r .$i , $value2, \PHPExcel_Cell_DataType::TYPE_STRING); 82. }else { 83. $objActSheet ->setCellValue($r .$i , $value2); 84. } 85. $r ++; 86. } 87. $i ++; 88. S('PROCESS_DOWNLOAD_PROCESSNUM',$i -2);// 更新缓存的进度 89. } 90. 91. $filename = 'Orders'.date('YmdHis') . '.xls'; 92. $objPHPExcel ->getActiveSheet()->setTitle('⼯作表1');// sheet 标题 93. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel , 'Excel5'); 94. $objWriter ->save($path .$filena
me ); 95. 96. echo '/'.$path .$filename ; // $_SERVER['HTTP_HOST'].'/'.$path.$filename; 97. exit; 98. }
[php]
1.
session_write_close();// 断开AJAX 请求,⽆需等待响应
[php] 1. <pre code_snippet_id="2173596" snippet_file_name="blog_20170208_4_6547481"></pre> 2. <pre></pre> 3. <pre></pre> 4. <pre></pre> 5.
<link rel="stylesheet" href="static.blog.csdn/public/res-min/markdown_views.css?v=2.0"> 6.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论