php导入导出excel表格

时间:2022-10-28 19:20:19

1 简介

1.1 导出  

在实际的工作项目中,经常需要将一些重要的数据库中存的数据导出成Excel,比如导出考勤报表,导出财务报表,导出业绩报表,导出销售报表等。

1.2 导入  

同时有的项目也需要,将一些Excel数据导入到数据库中。比如银行给的银行流水,销售报表导入的数据库中。这个通常的做法都是使用PHPExcel。  

2导出代码

<pre code_snippet_id="1603104" snippet_file_name="blog_20160309_1_8751202" name="code" class="php" style="font-family: 'microsoft yahei'; white-space: pre-wrap;">$kit=new kit();  
$kit->downloadkitofferFile("配件订单",$re);//调用导出方法
/**    
* 导出配件订单    
* @param int $ilename 生成的表格名称</span>  
* @param int $exportDatasss 需要导出的数据数组
*/  

 
 function downloadkitofferFile($filename, $exportDatasss){
    header("Content-type:text/csv");
    $filename = iconv('utf-8', 'gb2312', $filename);   
    $file_ending = "csv";
    header("Content-Disposition:attachment;filename=" . $filename . '.' . $file_ending);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    $output = "订单编号, 订单类型, 零件编号, 零件名称, 总价, 下单时间, 订单状态\n";//表格中的每一列名称
    $output = iconv('utf-8', 'gb2312', $output);   //转码
    foreach($exportDatasss as $val){
        $id = iconv('utf-8', 'gb2312', $val['did']);
        $kname = iconv('utf-8', 'gb2312', $val['typevalue']);
        $brand = iconv('utf-8', 'gb2312', $val['kid']);
        $system = iconv('utf-8', 'gb2312', $val['kname']);
        $type = iconv('utf-8', 'gb2312', $val['total']);
        $costprice = iconv('utf-8', 'gb2312', $val['ctime']);
        $storagstrate = iconv('utf-8', 'gb2312', $val['state']);
        $output .= $id .",". $kname .",". $brand .",". $system .",". $type .",". $costprice .",". $storagstrate ."\n"; //用引文逗号分开  
    }
    echo $output;
  }
 //转码
3.导入代码

3.1执行导入的代码 do.php

include_once ("connect.php");

$action = $_GET['action'];
if ($action == 'import') { //导入CSV
$filename = $_FILES['file']['tmp_name'];
if (empty ($filename)) {
echo '请选择要导入的CSV文件!';
exit;
}
$handle = fopen($filename, 'r');
$result = input_csv($handle); //解析csv
$len_result = count($result);
if($len_result==0){
echo '没有任何数据!';
exit;
}
for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值
$name = iconv('gb2312', 'utf-8', $result[$i][0]); //中文转码
$sex = iconv('gb2312', 'utf-8', $result[$i][1]);
$age = $result[$i][2];
$data_values .= "('$name','$sex','$age'),";
}
$data_values = substr($data_values,0,-1); //去掉最后一个逗号
fclose($handle); //关闭指针
$query = mysql_query("insert into student (name,sex,age) values $data_values");//批量插入数据表中
if($query){
echo '导入成功!';
}else{
echo '导入失败!';
}
} elseif ($action=='export') { //导出CSV
$result = mysql_query("select * from student");
$str = "姓名,性别,年龄\n";
$str = iconv('utf-8','gb2312',$str);
while($row=mysql_fetch_array($result)){
$name = iconv('utf-8','gb2312',$row['name']);
$sex = iconv('utf-8','gb2312',$row['sex']);
$str .= $name.",".$sex.",".$row['age']."\n";
}
$filename = date('Ymd').'.csv';
export_csv($filename,$str);
}
function input_csv($handle) {
$out = array ();
$n = 0;
while ($data = fgetcsv($handle, 10000)) {
$num = count($data);
for ($i = 0; $i < $num; $i++) {
$out[$n][$i] = $data[$i];
}
$n++;
}
return $out;
}

function export_csv($filename,$data) {
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}
3.2 connect.php

$host="localhost";
$db_user="root";
$db_pass="";
$db_name="demo";
$timezone="Asia/Shanghai";

$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");

3.3选择文件的代码

<form id="addform" action="do.php?action=import" method="post" enctype="multipart/form-data">
<p>请选择要导入的CSV文件:<br/><input type="file" name="file"> <input type="submit" class="btn" value="导入CSV">
<input type="button" class="btn" id="exportCSV" value="导出CSV" onClick="window.location.href='do.php?action=export'"></p>
</form>



4代码集

4.1附加上传代码

php导入代码

4.2附加导出代码

php导出代码