excel单元格数字变成字符串方法 '1001010002400000,数值前加单引号
当excel文件中的数据设置过,导入的数据和excel显示的不同,单个修改数据个单元格格式又很麻烦,这时把excel另存为csv格式即可,用文本方式打开看数据
导入$data->sheets
使用PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader
- <?php
- //$data_header_var = array(1=>'$incrementId',2=>'$price');
- function Excel2Db($fileName, $data_header_var=array()){
- require_once 'Excel/reader.php';
- $data = new Spreadsheet_Excel_Reader();
- $data->setOutputEncoding('utf-8'); //gbk
- $data->read($fileName);
- $numRows = $data->sheets["0"]['numRows'];
- for ($i = 2; $i <= $numRows; $i++) {
- foreach ($data_header_var as $j => $var){
- eval($var.'= $data->sheets["0"]["cells"][$i][$j];');
- }
- //save2Db
- }
- }
- /*$data2 = $data->sheets["0"]["cells"];
- unset($data2['1']);
- foreach ($data2 as $i=> $v) {
- foreach ($data_header_var as $j => $var){
- eval($var.'= $v[$j];');
- }*/
- function Excel2Array($fileName, $data_header_var=array()){
- require_once 'Excel/reader.php';
- $data = new Spreadsheet_Excel_Reader();
- $data->setOutputEncoding('utf-8'); //gbk
- $data->read($fileName);
- $dataRows = $lineData = array();
- $numRows = $data->sheets["0"]['numRows'];
- for ($i = 2; $i <= $numRows; $i++) {
- foreach ($data_header_var as $j => $var){
- $lineData[$var] = $data->sheets["0"]["cells"][$i][$j];
- }
- $dataRows[] = $lineData;
- }
- return $dataRows;
- }
导出
- <?php
- /*$excel = new Excel();
- //$excel->setEncode("utf-8","gb2312"); //设置编码
- $titleData = array("name","price","content","total");//设置标题栏
- $data = array(
- array("ab","ac","ad","ae"),
- array("abc","acc","adc","aec"),
- array("abd","acd","add","aed"),
- 't'=>array("abe","ace","ade","aee"),
- );
- $excel->getExcel($titleData, $data, "demo");
- */
- class Excel {
- var $inEncode = "utf-8"; //一般是页面编码
- var $outEncode = "utf-8"; //一般是Excel文件的编码
- var $content = '';
- /**
- *设置编码
- */
- public function setEncode($incode, $outcode){
- $this->inEncode = $incode;
- $this->outEncode = $outcode;
- }
- /**
- *设置Excel的行记录
- */
- public function setRow($lineData){
- $title = "";
- foreach($lineData as $v){
- if($this->inEncode != $this->outEncode){
- $title .= iconv($this->inEncode,$this->outEncode,$v)."\t";
- }else{
- $title .= $v."\t";
- }
- }
- $title .= "\n";
- return $title;
- }
- /**
- *设置Excel内容
- */
- public function setRows($array){
- $content = "";
- foreach($array as $k => $v){
- $content .= $this->setRow($v);
- }
- return $content;
- }
- /**
- *生成并自动下载Excel
- * $titleData 标题栏数组
- * $array 内容数组
- * $filename 文件名称 (为空,已当前日期为名称)
- */
- public function getExcel($titleData, $array, $filename = ''){
- if($filename==''){
- $filename = date("Y-m-d");
- }
- $title = $this->setRow($titleData);
- $content = $this->setRows($array);
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:filename=".$filename.".xls");
- echo $title;
- echo $content;
- }
- }
php读取淘宝数据包csv文件 unicode
- public static function fopen_utf8($filename)
- {
- $encoding = '';
- $handle = fopen($filename, 'r');
- $bom = fread($handle, 2);
- rewind($handle);
- if ($bom === chr(0xff) . chr(0xfe) || $bom === chr(0xfe) . chr(0xff)) {
- // UTF16 Byte Order Mark present
- $encoding = 'UTF-16';
- } else {
- $file_sample = fread($handle, 1000) + 'e'; //read first 1000 bytes
- rewind($handle);
- $encoding = mb_detect_encoding($file_sample, 'UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP');
- }
- if ($encoding) {
- stream_filter_append($handle, 'convert.iconv.' . $encoding . '/UTF-8');
- }
- return ($handle);
- }
- //得到csv data
- public function getCsvFileData($filename)
- {
- $handle = Helper::fopen_utf8($filename, "r");
- $csvData = array();
- for ($j = 1; !feof($handle); $j++) {
- $line = fgets($handle);
- $val = explode("\t", $line);
- if ($j > 1) {
- $line = array(
- 'title' => $val[0],
- //'cate' => $val[1],
- 'price' => $val[2], //价格
- 'inventory' => trim($val [3]), //数量
- 'descript' => trim($val [4]), //描述
- 'import_images' => trim($val[5]), //图片
- 'sku' => trim($val[6]), //商家编码
- 'weight' => trim($val[7]), //重量
- );
- $csvData[] = $line;
- }
- }
- return $csvData;
- }
- //校验数据
- public function checkProductImportData($adminId, $csvData)
- {
- $checkArray = array();
- foreach ($csvData as $product) {
- $checkArray[] = $product['sku'];
- }
- $list = 'db中的sku列表在文件sku中的'
- $result = array('status' => 10000, 'msg' => '');
- if (count($list) == count($checkArray)) {
- $result['status'] = 2;
- $result['msg'] = '文件里的商品已经导入过';
- } elseif (count($list) > 0) {
- $dbhasskulist = array();
- $alllist = self::model()->findAllSkuFromDb();
- foreach ($alllist as $info) {
- if ($info->sku && in_array($info->sku, $checkArray)) {
- $dbhasskulist[] = $info->sku;
- }
- }
- $result['status'] = 1;
- $result['msg'] = "商品货号重复:" . implode(",", array_unique($dbhasskulist));
- }
- return $result;
- }
时间: 2024-11-08 17:44:30