php封装数据输入到表格,php封装数据输入到表格中

php 怎么把数据导出到excel表格

php 把数据导出到excel表格有多种方法,比如使用 phpExcel 等,以下代码是直接通过 header 生成 excel 文件的代码示例:

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计、网站制作、武威网络推广、微信小程序定制开发、武威网络营销、武威企业策划、武威品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供武威建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

?php

header("Content-type:application/vnd.ms-excel");

header("Content-Disposition:filename=xls_region.xls");

$cfg_dbhost = 'localhost';

$cfg_dbname = 'testdb';

$cfg_dbuser = 'root';

$cfg_dbpwd = 'root';

$cfg_db_language = 'utf8';

// END 配置

//链接数据库

$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);

mysql_select_db($cfg_dbname);

//选择编码

mysql_query("set names ".$cfg_db_language);

//users表

$sql = "desc users";

$res = mysql_query($sql);

echo "tabletr";

//导出表头(也就是表中拥有的字段)

while($row = mysql_fetch_array($res)){

$t_field[] = $row['Field']; //Field中的F要大写,否则没有结果

echo "th".$row['Field']."/th";

}

echo "/tr";

//导出100条数据

$sql = "select * from users limit 100";

$res = mysql_query($sql);

while($row = mysql_fetch_array($res)){

echo "tr";

foreach($t_field as $f_key){

echo "td".$row[$f_key]."/td";

}

echo "/tr";

}

echo "/table";

?

php如何把mysql数据库导入到excel表格

?php

//需求:用php将mysql数据导入到excel中

//数据库配置信息

$DB_Server = "localhost";

$DB_Username = "root";

$DB_Password = "admin";

$DB_DBName = "shop";

$DB_TBLName = "sdb_widgets_set";

$savename = date("Y-m-j H:i:s");

// 数据库连接

$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");

//注意mysql 导入cxcel中的时候

mysql_query("Set Names 'gbk'");

//定义文件导出的格式

$file_type = "vnd.ms-excel";

//定义文件后缀名称

$file_ending = "xls";

header("Content-Type: application/$file_type;charset=gbk");

header("Content-Disposition: attachment; filename=".$savename.".$file_ending");

//header("Pragma: no-cache");

$now_date = date("Y-m-j H:i:s");

//定义要输出的数据表标题

$title = "数据表名:$DB_TBLName, | 日期:$now_date";

$sql = "Select * from $DB_TBLName";

$ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database");

$result = @mysql_query($sql,$Connect) or die(mysql_error());

echo("$title/n");

//定义制表格符号

$sep = "/t";

//逐个取出数据表字段

for ($i = 0; $i mysql_num_fields($result); $i++) {

echo mysql_field_name($result,$i) . "/t";

}

print("/n");

// $i = 0;

//循环打印出数据表中的数据

while($row = mysql_fetch_row($result)) {

$schema_insert = "";

for($j=0; $j mysql_num_fields($result);$j++) {

if(!isset($row[$j]))

$schema_insert .= "NULL".$sep;

else if ($row[$j] != "")

$schema_insert .= "$row[$j]".$sep;

else

$schema_insert .= "".$sep;

}

$schema_insert = str_replace($sep."$", "", $schema_insert);

$schema_insert .= "/t";

print(trim($schema_insert));

print "/n";

// $i++;

}

return (true);

?

用php如何把查询到的数据导进excel表格中去呢

如果你要求的excle表格不是很复杂 .可以用html的table表格来实现.因为excle的兼容性可以打开xml文档.所以你可以使用html文件来当做excle文件.

如果比较复杂 ,就要使用phpexcle了

这个是网页自动导出的基本思路

如果只是导出几个少数的表和内容

你可以使用phpmyadmin的导出功能.能够实现导出excle文档.

如何使用php将数据输入到已存在的html表格

$result = mysql_query("SELECT * FROM table_name");

while($row = mysql_fetch_array($result))

{

echo $row['ID'] . " " . $row['xuehao'] . " " . $row['xingming'] . " " . $row['chengji1'] . " " . $row['chengji2'];

echo "br /";

}

怎么使用php把表格中的数据导入到excel中

下面是我写的一个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;

}


当前题目:php封装数据输入到表格,php封装数据输入到表格中
文章地址:http://ybzwz.com/article/dsiehjs.html