下面是我寫的一個(gè)PHP導(dǎo)出數(shù)據(jù)到CSV問價(jià)的函數(shù),你到時(shí)候直接調(diào)用就行了
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)沾益免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
/**
*?導(dǎo)出CSV文件
*?@param?string?$fileName 文件名字
*?@param?string|array?$data?導(dǎo)出數(shù)據(jù),csv格式的字符串|數(shù)值數(shù)組
*?@param?string?$to_encoding?目標(biāo)轉(zhuǎn)換編碼
*?@param?string?$from_encoding?當(dāng)前編碼
*/
function?exportCSV($fileName?=?'',?$data?=?'',?$to_encoding?=?'gb2312',?$from_encoding?=?'utf-8')?{
$fileName?=?empty($fileName)???date('YmdHis')?:?$fileName;
//?文件標(biāo)簽
Header("Content-type:?application/octet-stream");
header("Content-type:?application/vnd.ms-excel;?charset=$from_encoding");
Header("Content-Disposition:?attachment;?filename=$fileName.csv");
$str?=?'';
if($data)?{
if(is_array($data))?{
foreach?($data?as?$v)?{
if(is_array($v))?{
foreach?($v?as?$vo)?{
$str?.=?(is_numeric($vo)???"'".$vo?:?$vo."").",";
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?.=?(is_numeric($v)???"'".$v?:?$v).",";
}
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?=?$data;
}
}
echo?mb_convert_encoding($str,?"gb2312",?"utf-8");
exit;
}
難道你導(dǎo)入的時(shí)候不是選擇的本地 文件嗎?input框獲取之后 提交過去就可以導(dǎo)入了啊 phpexcel中有例子的啊 可以看看簡(jiǎn)單的嘛 基本上 按那個(gè)例子改點(diǎn)東西就可以用了啊
如果是將數(shù)據(jù)導(dǎo)出到excel,應(yīng)該是用vba方便點(diǎn)。
如果非要用php,請(qǐng)參考:
Header( "Content-type: application/octet-stream ");
Header( "Accept-Ranges: bytes ");
Header( "Content-type:application/vnd.ms-excel ");
Header( "Content-Disposition:attachment;filename=test.xls ");
$con = mysql_connect("localhost","root","123456");
mysql_select_db("db_test");
mysql_query('set names utf8');
$sql = "select id,FirstName,LastName,Age,Hometown,Job from user";
$result = mysql_query($sql,$con);
echo "id\tFirstName\tLastName\tAge\tHometown\tJob";
while ($rs=mysql_fetch_array($result)){
echo "\n";
echo $rs['id']."\t".$rs['FirstName']."\t".$rs['LastName']."\t".$rs['Age']."\t".$rs['Hometown']."\t".$rs['Job'];
}
第一,在前臺(tái)html頁面進(jìn)行上傳文件:如:
復(fù)制代碼代碼如下:
form method="post" action="php文件"enctype="multipart/form-data"
h3導(dǎo)入Excel表:/h3input type="file" name="file_stu" /
input type="submit" value="導(dǎo)入"/
/form
第二,在對(duì)應(yīng)的php文件進(jìn)行文件的處理
復(fù)制代碼代碼如下:
if (! empty ( $_FILES ['file_stu'] ['name'] ))
{
$tmp_file = $_FILES ['file_stu'] ['tmp_name'];
$file_types = explode ( ".", $_FILES ['file_stu']['name'] );
$file_type = $file_types [count ( $file_types ) - 1];
/*判別是不是.xls文件,判別是不是excel文件*/
if (strtolower ( $file_type ) !="xls")
{
$this-error ( '不是Excel文件,重新上傳' );
}
/*設(shè)置上傳路徑*/
$savePath = SITE_PATH . '/public/upfile/Excel/';
/*以時(shí)間來命名上傳的文件*/
$str = date ( 'Ymdhis' );
$file_name = $str . "." . $file_type;
/*是否上傳成功*/
if (! copy ( $tmp_file, $savePath . $file_name ))
{
$this-error ( '上傳失敗' );
}
/*
*對(duì)上傳的Excel數(shù)據(jù)進(jìn)行處理生成編程數(shù)據(jù),這個(gè)函數(shù)會(huì)在下面第三步的ExcelToArray類中
注意:這里調(diào)用執(zhí)行了第三步類里面的read函數(shù),把Excel轉(zhuǎn)化為數(shù)組并返回給$res,再進(jìn)行數(shù)據(jù)庫寫入
*/
$res = Service ( 'ExcelToArray' )-read ( $savePath . $file_name );
/*
重要代碼解決Thinkphp M、D方法不能調(diào)用的問題
如果在thinkphp中遇到M 、D方法失效時(shí)就加入下面一句代碼
*/
//spl_autoload_register ( array ('Think', 'autoload' ) );
/*對(duì)生成的數(shù)組進(jìn)行數(shù)據(jù)庫的寫入*/
foreach ( $res as $k = $v )
{
if ($k != 0)
{
$data ['uid'] = $v[0];
$data ['password']= sha1 ( '111111' );
$data ['email'] =$v [1];
$data ['uname'] = $v [3];
$data ['institute'] = $v [4];
$result = M ( 'user' )-add( $data );
if (! $result)
{
$this-error ( '導(dǎo)入數(shù)據(jù)庫失敗' );
}
}
}
}
第三:ExcelToArrary類,用來引用phpExcel并處理Excel數(shù)據(jù)的
復(fù)制代碼代碼如下:
class ExcelToArrary extends Service{
public function __construct() {
/*導(dǎo)入phpExcel核心類 注意:你的路徑跟我不一樣就不能直接復(fù)制*/
include_once('./Excel/PHPExcel.php');
}
/**
* 讀取excel $filename 路徑文件名$encode 返回?cái)?shù)據(jù)的編碼默認(rèn)為utf8
*以下基本都不要修改
*/
public function read($filename,$encode='utf-8'){
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader-setReadDataOnly(true);
$objPHPExcel = $objReader-load($filename);
$objWorksheet = $objPHPExcel-getActiveSheet();
$highestRow =$objWorksheet-getHighestRow();
$highestColumn = $objWorksheet-getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData = array();
for($row = 1; $row = $highestRow; $row++) {
for ($col = 0; $col $highestColumnIndex;$col++) {
$excelData[$row][] =(string)$objWorksheet-getCellByColumnAndRow($col,$row)-getValue();
}
}
return $excelData;
}
}