网络配置:apache+php+mysql,windows电脑下的自己编程心得。
前提自己下载好PHPExcel_1.8.0,放置到appchi的home目录下。
然后是对应的PHP举例:
require("../PHPExcel_1.8.0_doc/Classes/PHPExcel.php");
# 调整日期格式
function GetData($val){
$jd = GregorianToJD(1, 1, 1970);
$gregorian = JDToGregorian($jd+intval($val)-25569);
return $gregorian;/**显示格式为 “月/日/年” */
}
$action = $_GET['action'];
if ($action == 'import') { //导入CSV
//获取文件名
$show_name = trim($_FILES['file']['name']);
/*判别是不是.xls文件,判别是不是excel文件*/
$file_types = explode ( ".", $show_name );
$file_type = $file_types [count ( $file_types ) - 1];
if(strtolower($file_type) != "xls" && strtolower($file_type) != "xlsx"){
echo "
alert('不是Excel文件,重新上传')
";
$type_excle=0;
}else{
$type_excle=1;
}
//移动文件
$dest_dir= 'c:/tmp/';
if($type_excle){
//move file
$show_name = $_FILES['file']['name'];
$file_name = $dest_dir.time().$show_name;
if(!move_uploaded_file($_FILES['file']['tmp_name'],$file_name)){
warn('文件上传失败!',$url);
}
}
//读文件
if($type_excle){
$filePath = $file_name;
$PHPExcel = new PHPExcel();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
echo 'no Excel';
return ;
}
}
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
/**从第A列开始输出*/
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数 A列转为1列*/
if($currentColumn == 'B'){
echo GetData($val)."\t";
}elseif($currentColumn == 'A'){ $num = number_format($val,'0','','');
echo $num."\t";
}else{
//echo $val;
/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
echo iconv('utf-8','gb2312', $val)."\t";
}
}
echo "
";
}
echo "\n";
}
} elseif ($action=='export') { //导出CSV
//导出处理
}
?>
阅读(366) | 评论(0) | 转发(0) |