我需要先把excel表格弄成html的格式,然后把html的代码上传到数据库的某个表里,然后以字段名content存入,这样我直接echo $row[content];就可以出来表格,但是之前这布怎么做,我不能每次都放到dreamweaver里面搞,太麻烦了,我这边还要做一个上传端,传的是excel文件,录入数据库的是html代码,中间的这部转换如何做?
第ä¸ï¼å¨åå°html页é¢è¿è¡ä¸ä¼ æ件ï¼å¦ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
<form method="post" action="phpæ件"enctype="multipart/form-data">
<h3>å¯¼å ¥Excel表ï¼</h3><input type="file" name="file_stu" />
<input type="submit" value="å¯¼å ¥"/>
</form>
第äºï¼å¨å¯¹åºçphpæ件è¿è¡æ件çå¤ç
å¤å¶ä»£ç 代ç å¦ä¸:
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æ件ï¼éæ°ä¸ä¼ ' );
}
/*设置ä¸ä¼ è·¯å¾*/
$savePath = SITE_PATH . '/public/upfile/Excel/';
/*以æ¶é´æ¥å½åä¸ä¼ çæ件*/
$str = date ( 'Ymdhis' );
$file_name = $str . "." . $file_type;
/*æ¯å¦ä¸ä¼ æå*/
if (! copy ( $tmp_file, $savePath . $file_name ))
{
$this->error ( 'ä¸ä¼ 失败' );
}
/*
*对ä¸ä¼ çExcelæ°æ®è¿è¡å¤ççæç¼ç¨æ°æ®,è¿ä¸ªå½æ°ä¼å¨ä¸é¢ç¬¬ä¸æ¥çExcelToArrayç±»ä¸
注æï¼è¿éè°ç¨æ§è¡äºç¬¬ä¸æ¥ç±»éé¢çreadå½æ°ï¼æExcel转å为æ°ç»å¹¶è¿åç»$res,åè¿è¡æ°æ®åºåå ¥
*/
$res = Service ( 'ExcelToArray' )->read ( $savePath . $file_name );
/*
éè¦ä»£ç 解å³Thinkphp MãDæ¹æ³ä¸è½è°ç¨çé®é¢
å¦æå¨thinkphpä¸éå°M ãDæ¹æ³å¤±ææ¶å°±å å ¥ä¸é¢ä¸å¥ä»£ç
*/
//spl_autoload_register ( array ('Think', 'autoload' ) );
/*对çæçæ°ç»è¿è¡æ°æ®åºçåå ¥*/
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 ( 'å¯¼å ¥æ°æ®åºå¤±è´¥' );
}
}
}
}
第ä¸ï¼ExcelToArraryç±»,ç¨æ¥å¼ç¨phpExcel并å¤çExcelæ°æ®ç
å¤å¶ä»£ç 代ç å¦ä¸:
class ExcelToArrary extends Service{
public function __construct() {
/*å¯¼å ¥phpExcelæ ¸å¿ç±» 注æï¼ä½ çè·¯å¾è·æä¸ä¸æ ·å°±ä¸è½ç´æ¥å¤å¶*/
include_once('./Excel/PHPExcel.php');
}
/**
* 读åexcel $filename è·¯å¾æ件å$encode è¿åæ°æ®çç¼ç é»è®¤ä¸º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;
}
}
å¤å¶ä»£ç 代ç å¦ä¸:
<form method="post" action="phpæ件"enctype="multipart/form-data">
<h3>å¯¼å ¥Excel表ï¼</h3><input type="file" name="file_stu" />
<input type="submit" value="å¯¼å ¥"/>
</form>
第äºï¼å¨å¯¹åºçphpæ件è¿è¡æ件çå¤ç
å¤å¶ä»£ç 代ç å¦ä¸:
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æ件ï¼éæ°ä¸ä¼ ' );
}
/*设置ä¸ä¼ è·¯å¾*/
$savePath = SITE_PATH . '/public/upfile/Excel/';
/*以æ¶é´æ¥å½åä¸ä¼ çæ件*/
$str = date ( 'Ymdhis' );
$file_name = $str . "." . $file_type;
/*æ¯å¦ä¸ä¼ æå*/
if (! copy ( $tmp_file, $savePath . $file_name ))
{
$this->error ( 'ä¸ä¼ 失败' );
}
/*
*对ä¸ä¼ çExcelæ°æ®è¿è¡å¤ççæç¼ç¨æ°æ®,è¿ä¸ªå½æ°ä¼å¨ä¸é¢ç¬¬ä¸æ¥çExcelToArrayç±»ä¸
注æï¼è¿éè°ç¨æ§è¡äºç¬¬ä¸æ¥ç±»éé¢çreadå½æ°ï¼æExcel转å为æ°ç»å¹¶è¿åç»$res,åè¿è¡æ°æ®åºåå ¥
*/
$res = Service ( 'ExcelToArray' )->read ( $savePath . $file_name );
/*
éè¦ä»£ç 解å³Thinkphp MãDæ¹æ³ä¸è½è°ç¨çé®é¢
å¦æå¨thinkphpä¸éå°M ãDæ¹æ³å¤±ææ¶å°±å å ¥ä¸é¢ä¸å¥ä»£ç
*/
//spl_autoload_register ( array ('Think', 'autoload' ) );
/*对çæçæ°ç»è¿è¡æ°æ®åºçåå ¥*/
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 ( 'å¯¼å ¥æ°æ®åºå¤±è´¥' );
}
}
}
}
第ä¸ï¼ExcelToArraryç±»,ç¨æ¥å¼ç¨phpExcel并å¤çExcelæ°æ®ç
å¤å¶ä»£ç 代ç å¦ä¸:
class ExcelToArrary extends Service{
public function __construct() {
/*å¯¼å ¥phpExcelæ ¸å¿ç±» 注æï¼ä½ çè·¯å¾è·æä¸ä¸æ ·å°±ä¸è½ç´æ¥å¤å¶*/
include_once('./Excel/PHPExcel.php');
}
/**
* 读åexcel $filename è·¯å¾æ件å$encode è¿åæ°æ®çç¼ç é»è®¤ä¸º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;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-03-26
下载一个phpexcel,当前大概是1.7.6版本
例子:
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
set_time_limit(1200); //20分钟
date_default_timezone_set('Europe/London');
require_once '../176/Classes/PHPExcel.php';
require_once '../176/Classes/PHPExcel/IOFactory.php';
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("../a.xls");
$sheet = $objPHPExcel->getSheet(0); //打开sheet0 序号从0开始
$highestRow = $sheet->getHighestRow(); //得到行数
$highestColumn = $objWorksheet->getHighestColumn(); //
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//echo '总行数'.$highestRow;
$abiao=array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$abiao[$row][$col]=$sheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
//以上把表sheet0取到了$abiao这个数组中
//保存
//$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
//$objWriter->save("../aa.xls");追问
例子:
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
set_time_limit(1200); //20分钟
date_default_timezone_set('Europe/London');
require_once '../176/Classes/PHPExcel.php';
require_once '../176/Classes/PHPExcel/IOFactory.php';
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("../a.xls");
$sheet = $objPHPExcel->getSheet(0); //打开sheet0 序号从0开始
$highestRow = $sheet->getHighestRow(); //得到行数
$highestColumn = $objWorksheet->getHighestColumn(); //
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//echo '总行数'.$highestRow;
$abiao=array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$abiao[$row][$col]=$sheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
//以上把表sheet0取到了$abiao这个数组中
//保存
//$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
//$objWriter->save("../aa.xls");追问
求加qq 69371279
第2个回答 2012-03-26
用客户端 读excel文件,然后传数据
vbscript和javascript都可以实现,excel.application
vbscript和javascript都可以实现,excel.application
第3个回答 2012-03-25
没有必要这样麻烦的,你用phpexcel实现吧本回答被网友采纳