怎么使用php把表格中的数据导入到excel中
2个回答
展开全部
下面是我写的一个PHP导出数据到CSV问价的函数,你到时候直接调用就行了
/**
* 导出CSV文件
* @param string $fileName 文件名字
* @param string|array $data 导出数据,csv格式的字符串|数值数组
* @param string $to_encoding 目标转换编码
* @param string $from_encoding 当前编码
*/
function exportCSV($fileName = '', $data = '', $to_encoding = 'gb2312', $from_encoding = 'utf-8') {
$fileName = empty($fileName) ? date('YmdHis') : $fileName;
// 文件标签
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;
}
展开全部
/**
* @param array $data //二维数组 不是对象
* @param string $filename //导出文件名
*/
public static function Export($data = array(), $filename = 'report') {
ob_get_contents();
ob_end_clean();//会清除缓冲区的内容,并将缓冲区关闭,但不会输出内容。此函数丢弃最顶层输出缓冲区的内容并关闭这个缓冲区。如果想要进一步处理缓冲区的内容,必须在ob_end_clean()之前调用ob_get_contents(),因为当调用ob_end_clean()时缓冲区内容将被丢弃。
ob_start();
header ( "Content-type:application/octet-stream" );
header ( "Accept-Ranges:bytes" );
header ( "Content-type:application/vnd.ms-excel" );
header ( "Content-Disposition:attachment;filename=" . $filename . ".xls" );
header ( "Pragma: no-cache" );
header ( "Expires: 0" );
if (! empty ( $data )) {
foreach ( $data as $key => $val ) {
foreach ( $val as $ck => $cv ) {
$data [$key] [$ck] = iconv ( "UTF-8", "GB2312", $cv );
}
$data [$key] = implode ( "\t", $data [$key] );
}
echo implode ( "\n", $data );
}
}
* @param array $data //二维数组 不是对象
* @param string $filename //导出文件名
*/
public static function Export($data = array(), $filename = 'report') {
ob_get_contents();
ob_end_clean();//会清除缓冲区的内容,并将缓冲区关闭,但不会输出内容。此函数丢弃最顶层输出缓冲区的内容并关闭这个缓冲区。如果想要进一步处理缓冲区的内容,必须在ob_end_clean()之前调用ob_get_contents(),因为当调用ob_end_clean()时缓冲区内容将被丢弃。
ob_start();
header ( "Content-type:application/octet-stream" );
header ( "Accept-Ranges:bytes" );
header ( "Content-type:application/vnd.ms-excel" );
header ( "Content-Disposition:attachment;filename=" . $filename . ".xls" );
header ( "Pragma: no-cache" );
header ( "Expires: 0" );
if (! empty ( $data )) {
foreach ( $data as $key => $val ) {
foreach ( $val as $ck => $cv ) {
$data [$key] [$ck] = iconv ( "UTF-8", "GB2312", $cv );
}
$data [$key] = implode ( "\t", $data [$key] );
}
echo implode ( "\n", $data );
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询