请教下导出到excel的问题

Macy0606 2012-09-06 04:21:05
我是循环出的一个表格,想导出为一张excel表格,我网上找过,有phpexcel但是发现搞不定,有没有人能指点指点的,能附上代码更好,谢谢
...全文
133 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Macy0606 2012-09-10
  • 打赏
  • 举报
回复
一个页面循环出表格,点击导出按键,跳转新的页面,弹出保存excel对话框
下面是这一页的内容
<?php

$sql = "select * from shop ";
$query = mysql_query($sql);

while($row = mysql_fetch_array($query)){
$rows[] = $row;
}



header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test.xlsx");

include 'PHPExcel.php'; //对文件的原子性操作类
include 'PHPExcel/IOFactory.php'; //顾名思义 以工厂模式封装的读写类 哈~
$PHPExcelObj = new PHPExcel();

$PHPExcelObj->setActiveSheetIndex(0); //在excel的第一张(Sheet1)表单上写入数据
$PHPExcelObj->getActiveSheet()->setCellValue('A1', '商品名称'); //A1是excel的 第1列第1行
$PHPExcelObj->getActiveSheet()->setCellValue('B1', '商品型号');
$PHPExcelObj->getActiveSheet()->setCellValue('C1', '商品数量');
$PHPExcelObj->getActiveSheet()->setCellValue('D1', '商品单价');
$PHPExcelObj->getActiveSheet()->setCellValue('E1', '商品用于');
$PHPExcelObj->getActiveSheet()->setCellValue('F1', '商品申请人');
$PHPExcelObj->getActiveSheet()->setCellValue('G1', '审批状态');

foreach($rows as $key=>$row){
$row_index = $key+2;

$PHPExcelObj->getActiveSheet()->setCellValue('A'.$row_index, $row['shop_name']); //A1是excel的 第1列第1行
$PHPExcelObj->getActiveSheet()->setCellValue('B'.$row_index, $row['shop_type']);
$PHPExcelObj->getActiveSheet()->setCellValue('C'.$row_index, $row['shop_shuliang']);
$PHPExcelObj->getActiveSheet()->setCellValue('D'.$row_index, $row['shop_value']);
$PHPExcelObj->getActiveSheet()->setCellValue('E'.$row_index, $row['shop_where']);
$PHPExcelObj->getActiveSheet()->setCellValue('F'.$row_index, $row['shop_shenqing']);
$PHPExcelObj->getActiveSheet()->setCellValue('G'.$row_index, $row['shop_state']);
}

$excelWriter = PHPExcel_IOFactory::createWriter($PHPExcelObj, 'Excel5');
$excelWriter->save('php://output');
exit();




?>
Macy0606 2012-09-10
  • 打赏
  • 举报
回复
我是这么写的,但是导出的时候是乱码,请各位帮我看下。

一个页面循环出表格,点击导出excel,跳转到新的页面,下面是这个页面的代码
<?php

$sql = "select * from shop ";
$query = mysql_query($sql);

while($row = mysql_fetch_array($query)){
$rows[] = $row;
}



header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test.xlsx");

include 'PHPExcel.php'; //对文件的原子性操作类
include 'PHPExcel/IOFactory.php'; //顾名思义 以工厂模式封装的读写类 哈~
$PHPExcelObj = new PHPExcel();

$PHPExcelObj->setActiveSheetIndex(0); //在excel的第一张(Sheet1)表单上写入数据
$PHPExcelObj->getActiveSheet()->setCellValue('A1', '商品名称'); //A1是excel的 第1列第1行
$PHPExcelObj->getActiveSheet()->setCellValue('B1', '商品型号');
$PHPExcelObj->getActiveSheet()->setCellValue('C1', '商品数量');
$PHPExcelObj->getActiveSheet()->setCellValue('D1', '商品单价');
$PHPExcelObj->getActiveSheet()->setCellValue('E1', '商品用于');
$PHPExcelObj->getActiveSheet()->setCellValue('F1', '商品申请人');
$PHPExcelObj->getActiveSheet()->setCellValue('G1', '审批状态');

foreach($rows as $key=>$row){
$row_index = $key+2;

$PHPExcelObj->getActiveSheet()->setCellValue('A'.$row_index, $row['shop_name']); //A1是excel的 第1列第1行
$PHPExcelObj->getActiveSheet()->setCellValue('B'.$row_index, $row['shop_type']);
$PHPExcelObj->getActiveSheet()->setCellValue('C'.$row_index, $row['shop_shuliang']);
$PHPExcelObj->getActiveSheet()->setCellValue('D'.$row_index, $row['shop_value']);
$PHPExcelObj->getActiveSheet()->setCellValue('E'.$row_index, $row['shop_where']);
$PHPExcelObj->getActiveSheet()->setCellValue('F'.$row_index, $row['shop_shenqing']);
$PHPExcelObj->getActiveSheet()->setCellValue('G'.$row_index, $row['shop_state']);
}

$excelWriter = PHPExcel_IOFactory::createWriter($PHPExcelObj, 'Excel5');
$excelWriter->save('php://output');
exit();




?>
bailixia520 2012-09-07
  • 打赏
  • 举报
回复
导出EXCEL用不着什么类,就3楼的就行。
我给你一个完整的

<?php
/*
* 文件创建时间 2010-8-26
*
* 创建人:森林
* 联系方式:QQ 83202700 Email:83202700@qq.com
*/
if(!$_COOKIE[username]) exit("请登陆后导出数据");
header("Content-type:appliction/vnd.ms-excel");
header("content-disposition:filename=CW-YCL".date("Ymdhis").".xls");
include("data/conn.php");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<body>
<TABLE cellSpacing=0 cellPadding=0 width="1000px" border=1>
<tr><td colspan=8><font size=5><b><center>EXCEL导出</center></b></font></td> </tr>
<tr><td colspan=8><center>半成品入库记录</center></td> </tr>
<tr><td colspan=8>制表日期:<?php echo date("Y-m-d")?>
</td></tr>
<tr>
<td>工令号</td>
<td>单据编号</td>
<td>料号</td>
<td>品名</td>
<td>数量</td>
<td>类型</td>
<td>日期</td>
<td>仓管操作员</td>

</tr>
</tr>

<?php

$sqllld=mysql_query("SELECT * FROM `tyt_ckbcp_1` where dt>='$_POST[dtks]' && dt<='$_POST[dtjs]' order by id");
while($rowslld=mysql_fetch_array($sqllld)){

?>
<tr>

<td style="vnd.ms-excel.numberformat:@"><?php echo $rowslld[gldh]?></td>
<td><?php echo $rowslld[bianhao]?></td>
<td><?php echo $rowslld[liaohao]?></td>
<td><?php echo $rowslld[pingm]?></td>
<td><?php echo $rowslld[sl]?></td>
<td><?php echo $rowslld[bz]?></td>
<td><?php echo $rowslld[dt]?></td>
<td><?php echo $rowslld[username]?></td>
</tr>

<?php
}
?>

</table>

本报表最终由:<?php echo $_COOKIE[username]?> 导出
</body>
</head>
</html>
diyle 2012-09-06
  • 打赏
  • 举报
回复
http://blog.csdn.net/diyle/article/details/6340187
fozzainter 2012-09-06
  • 打赏
  • 举报
回复
续自己在2L的回复
<?php
$con = mysql_connect("localhost","root","mysql") or die("无法连接到数据库".mysql_error());
mysql_select_db('berecord');
mysql_query('set names utf8');

$where = ' '.'1=1';
if ($_POST['name'] != ''){
$where.=" and name = '".$_POST['name']."'";
}
if($_POST['carnumber']!=''){
$where.=" and carnumber = '".$_POST['carnumber']."'";
}
if($_POST['do']!=''){
$where.=" and zhixing='".$_POST['do']."'";
}
if($_POST['usercode']!=''){
$where.="and usercode='".$_POST['usercode']."'";
}

$sql="select * from record where".$where.' '."and searchtime between '$date1' and '$date2'";

然后再用$row['']; 输出就可以了 不知道符不符合楼主要求
fozzainter 2012-09-06
  • 打赏
  • 举报
回复
在php开始加<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls;");//导出的文件名
?>
franzhong 2012-09-06
  • 打赏
  • 举报
回复
http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2009/0114/309.html

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧