用PHP EXCEL插件吧。

創(chuàng)新互聯(lián)建站為您提適合企業(yè)的網(wǎng)站設(shè)計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進行網(wǎng)站設(shè)計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站設(shè)計、做網(wǎng)站, 我們的網(wǎng)頁設(shè)計師為您提供的解決方案。
class ExcelToArrary extends Service{
public function __construct() {
/*導入phpExcel核心類 注意 :你的路徑跟我不一樣就不能直接復制*/
include_once('./Excel/PHPExcel.php');
}
/* 導出excel函數(shù)*/
public function push($data,$name='Excel'){
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
/*以下是一些設(shè)置 ,什么作者 標題啊之類的*/
$objPHPExcel-getProperties()-setCreator("轉(zhuǎn)彎的陽光")
-setLastModifiedBy("轉(zhuǎn)彎的陽光")
-setTitle("數(shù)據(jù)EXCEL導出")
-setSubject("數(shù)據(jù)EXCEL導出")
-setDescription("備份數(shù)據(jù)")
-setKeywords("excel")
-setCategory("result file");
/*以下就是對處理Excel里的數(shù)據(jù), 橫著取數(shù)據(jù),主要是這一步,其他基本都不要改*/
foreach($data as $k = $v){
$num=$k+1;
$objPHPExcel-setActiveSheetIndex(0)
//Excel的第A列,uid是你查出數(shù)組的鍵值,下面以此類推
-setCellValue('A'.$num, $v['uid'])
-setCellValue('B'.$num, $v['email'])
-setCellValue('C'.$num, $v['password'])
}
$objPHPExcel-getActiveSheet()-setTitle('User');
$objPHPExcel-setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter-save('php://output');
exit;
}
講的復雜了啊!\x0d\x0a你先在一個完整版的PHPExcel之后解壓,在“Examples”目錄下會找到一大堆例子,根據(jù)你的要求這個“01simple-download-xlsx.php”文件就可以了!\x0d\x0a注:你先保持“01simple-download-xlsx.php”文件所在的目錄位置不要變,測試好了,再改變名,移到別的地方,地方變了的話,文件里的 “require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';”的所在位置也要變!\x0d\x0a我們要改動代碼很少,如下:\x0d\x0a// Add some data\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)\x0d\x0a-setCellValue('A1', 'Hello')\x0d\x0a-setCellValue('B2', 'world!')\x0d\x0a-setCellValue('C1', 'Hello')\x0d\x0a-setCellValue('D2', 'world!');\x0d\x0a\x0d\x0a// Miscellaneous glyphs, UTF-8\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)\x0d\x0a-setCellValue('A4', 'Miscellaneous glyphs')\x0d\x0a-setCellValue('A5', 'éàèùaê?????ü???ü?');\x0d\x0a、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、\x0d\x0a直接用的我的替換\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)//這個就是現(xiàn)實導出的表第一行,有幾列是根據(jù)你的那張表有幾列!\x0d\x0a-setCellValue('A1', '單號')\x0d\x0a-setCellValue('B1', '標題')\x0d\x0a-setCellValue('C1', '內(nèi)容')\x0d\x0a-setCellValue('D1', '序列')\x0d\x0a-setCellValue('E1', '數(shù)字');\x0d\x0a//下面實現(xiàn)的就是建立數(shù)據(jù)庫連接,直接到表,你的連接數(shù)據(jù)庫、表、字段應(yīng)該與我的不一樣,你可以參考\x0d\x0a$conn=@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫服務(wù)器連接錯誤".mysql_error());//連接mysql數(shù)據(jù)庫\x0d\x0amysql_select_db("temp",$conn) or die("數(shù)據(jù)庫訪問錯誤".mysql_error());//數(shù)據(jù)庫\x0d\x0amysql_query("set character set gb2312");\x0d\x0amysql_query("set names gb2312");\x0d\x0a\x0d\x0a$sqlgroups="select * from test ";//查詢這一張表的條件\x0d\x0a$resultgroups=mysql_query($sqlgroups);\x0d\x0a$numrows=mysql_num_rows($resultgroups);\x0d\x0aif ($numrows0)\x0d\x0a{\x0d\x0a$count=1;\x0d\x0awhile($data=mysql_fetch_array($resultgroups))\x0d\x0a{\x0d\x0a$count+=1;\x0d\x0a$l1="A"."$count";\x0d\x0a$l2="B"."$count";\x0d\x0a$l3="C"."$count";\x0d\x0a$l4="D"."$count";\x0d\x0a$l5="E"."$count";\x0d\x0a$objPHPExcel-setActiveSheetIndex(0) \x0d\x0a-setCellValue($l1, $data['id'])//這就是你要導出表的字段、與對應(yīng)的名稱\x0d\x0a-setCellValue($l2, $data['title'])\x0d\x0a-setCellValue($l3, $data['content'])\x0d\x0a-setCellValue($l4, $data['sn'])\x0d\x0a-setCellValue($l5, $data['num']);\x0d\x0a}\x0d\x0a}
在php開發(fā)過程中,對于大量的數(shù)據(jù)考慮用表格輸出的時候可以用得到,代碼如下:
html?
head?
title二行5列一共10個數(shù)據(jù)/title?
/head?
body?
table?border="1"?width=80%?
tr?
?php?
$num?=?5;?//當前每一行顯示列數(shù)?
$k?=?1;?//初始化?
while($k=10)?
{?
if($k?%?$num?==?0){?
if($k==10){?
echo?'td'.$k.'/td/tr'."\r\n";?
}else{?
echo?'td'.$k.'/td/trtr'."\r\n";?
}?
}?
else?{?
echo?'td'.$k.'/td'."\r\n";?
}?
$k+=1;?//自加?
}?
??
/table?
/body?
/html
下面是從數(shù)據(jù)庫讀取代碼:
?php?
$sql?=?"select?*?from?table?";?
$query?=?mysql_query($sql);?
$num?=?mysql_num_rows($query);?
for($i=0;$i?$num;$i++)?{?
$result?=?mysql_fetch_array($query);?
if($i%3==0){?
$str.=?"?tr?style=?'word-break:break-all?'?";?
}?
$str.=?"?td?".$result[?"title?"].?"?/td?";?
if(($i+1)%$num==0)?{?
$str.=?"?/tr?";?
}?
}?
??
table?border=0?
??
echo?$str;?
??
/table
可以先放把表頭信息放到一個集合里,
然后把數(shù)據(jù)放入一個二維數(shù)組或二維的容器里
例如 :
后臺
String[] tis = {"7.1","7.2","7.3"};
ListString[] data = new ArrayListString[]();
data.add(new String[]{"1","2","3"});
data.add(new String[]{"4","5","6"});
data.add(new String[]{"7","8","9"});
request.setAttribute("tis", tis);
request.setAttribute("data", tis);
Jsp:
table
!-- 表頭 --
tr
c:forEach items="tis" var="ti"
td${ti}/td
/c:forEach
/tr
!-- 數(shù)據(jù) --
c:forEach items="data" var="ds"
tr
c:forEach items="ds" var="d"
td$cwmuewm/td
/c:forEach
/tr
/c:forEach
/table
1、建立一個數(shù)組,取名為$ShuZu,舉例數(shù)組的內(nèi)容是學生的信息,包括姓名、性別和年齡。
$ShuZu = array
(
array("姓名","性別","年齡"),
array("張三","男",13),
array("里斯","女",12),
array("王五","男",15)
);
2、建立一個變量,用來存儲需要顯示的表格。
$BiaoGe = "";
給¥BiaoGe賦初始值,
$BiaoGe = $BiaoGe."table ";
3、構(gòu)建外循環(huán),取出數(shù)組$ShuZu中的行數(shù)。
for ($i=0; $icount($ShuZu); $i++)
{
$BiaoGe = $BiaoGe. "tr";
$BiaoGe = $BiaoGe. "/tr";
}
4、構(gòu)建內(nèi)循環(huán),取出每行中的每一個列。
for ($i=0; $icount($ShuZu); $i++)
{
$BiaoGe = $BiaoGe. "tr";
for ($j=0;$jcount($ShuZu[$i]);$j++)
{
$BiaoGe = $BiaoGe. "td".$ShuZu[$i][$j]."/td";
}
$BiaoGe = $BiaoGe. "/tr";
}
5、加入表示表格結(jié)尾的html標記。
$BiaoGe = $BiaoGe. "/table";
6、使用echo 方法顯示表格。
echo? $BiaoGe;
7、查看運行結(jié)果,這樣,表格就制作完成了。
php 把數(shù)據(jù)導出到excel表格有多種方法,比如使用 phpExcel 等,以下代碼是直接通過 header 生成 excel 文件的代碼示例:
?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?配置
//鏈接數(shù)據(jù)庫
$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要大寫,否則沒有結(jié)果
echo?"th".$row['Field']."/th";
}
echo?"/tr";
//導出100條數(shù)據(jù)
$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";
?
網(wǎng)站名稱:php中生成表格數(shù)據(jù),php獲取表單數(shù)據(jù)的基本方法
標題URL:http://chinadenli.net/article49/dsegehh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化、網(wǎng)站導航、ChatGPT、做網(wǎng)站、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)