


ExcelFileParser processes excel to obtain data and can be imported into the database in batches_PHP tutorial
ExcelFileParser处理excel获得数据 可作批量导入到数据库
提交表单
提交处理
[php]
/**
* CopyRight (c) 2009,
* All rights reserved.
* File name:
* Abstract:
*
* @author Monday ixqbar@hotmail.com
* @version
*/
class IndexAction extends Action
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Default index page
*/
public function index()
{
$this->display();
}
/**
* Submit processing
*/
public function parse()
{
/**
* $_FILES Array Description
File name
* "] = & Gt; Error (0 Success 1 File is too large than upload_max_filesize2 files are too large than max_file3 upload incomplete 4 without upload files)
*[" size "] = & gt; file size (unit: kb)
* }
* }
*/
$return=array(0,'');
/**
* Determine whether to submit
* is_uploaded_file (file name) is used to determine whether the specified file is uploaded using the POST method to prevent illegal submission. It is usually used together with move_upload_file to save the uploaded file to the specified path
*/
if(!isset($_FILES) || !is_uploaded_file($_FILES['excel']['tmp_name']))
{
$return=array(1,'提交不合法');
}
//处理
if(0 == $return[0])
{
import('@.Util.ExcelParser');
$excel=new ExcelParser($_FILES['excel']['tmp_name']);
$return=$excel->main();
}
//输出处理
print_r($return);
}
}
?>
[/php]
处理类
[php]
/**
* CopyRight (c) 2009,
* All rights reserved.
* File name: excel data acquisition
* Abstract:
*
* @author Monday ixqbar@hotmail .com
* @version 0.1
*/
class ExcelParser
{
private $_data=array(0,'');
private $_excel_handle;
private $_excel=array();
/**
* Constructor
* @param
*/
public function __construct($filename)
{
/**
* Introduce excelparser class
* Common method is
* requires path.'excelparser.php';
* Import is ThinkPHP’s own import class method
*/
import('@.Util.PHPExcelParser.excelparser','','.php');
$this->_excel_handle=new ExcelFileParser();
//错误获取
$this->checkErrors($filename);
}
/**
* Error checking
*/
private function checkErrors($filename)
{
/**
*Method 1
*/
$error_code=$this->_excel_handle->ParseFromFile($filename);
/**
* 方法二
* $file_handle = fopen($this->_filename,'rb');
* $content = fread($file_handle,filesize($this->_filename));
* fclose($file_handle);
* $error_code = $this->_excel->ParseFromString($content);
* unset($content,$file_handle);
*/
switch($error_code)
{
case 0:
//无错误不处理
break;
case 1:
$this->_data=array(1,'文件读取错误(Linux注意读写权限)');
break;
case 2:
$this->_data=array(1,'文件太小');
break;
case 3:
$this->_data=array(1,'读取Excel表头失败');
break;
case 4:
$this->_data=array(1,'文件读取错误');
break;
case 5:
$this->_data=array(1,'文件可能为空');
break;
case 6:
$this->_data=array(1,'文件不完整');
break;
case 7:
$this->_data=array(1,'读取数据错误');
break;
case 8:
$this->_data=array(1,'版本错误');
break;
}
unset($error_code);
}
/**
* Excel information acquisition
*/
private function getExcelInfo()
{
if(1==$this->_data[0])return;
/**
* Get the number of sheets
* Get the rows and columns corresponding to the sheet units
*/
$this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);
for($i=0;$i<$this->_excel['sheet_number'];$i++)
{
/**
* Rows in columns */
$row=$this->_excel_handle->worksheet['data'][$i]['max_row'];
$col=$this->_excel_handle->worksheet['data'][$i]['max_col'];
$this->_excel['row_number'][$i]=($row==NULL)?0:++$row;
$this->_excel['col_number'][$i]=($col==NULL)?0:++$col;
unset($row,$col);
}
}
/**
* Chinese processing function
* @return
*/
private function uc2html($str)
{
$ret = '';
for( $i=0; $i
$charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
$ret .= ''.$charcode.';';
}
return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES');
}
/**
* Excel data acquisition
*/
private function getExcelData()
{
if(1==$this->_data[0])return;
//修改标记
$this->_data[0]=1;
//获取数据
for($i=0;$i<$this->_excel['sheet_number'];$i++)
{
/**
*/
for($j=0;$j<$this->_excel['row_number'][$i];$j++)
{
/**
*/
for($k=0;$k<$this->_excel['col_number'][$i];$k++)
{
/**
* array(4) {
=> Font
* ["data"] => Data
*/
$data=$this->_excel_handle->worksheet['data'][$i]['cell'][$j][$k];
switch($data['type'])
{
case 0:
//字符类型
if($this->_excel_handle->sst['unicode'][$data['data']])
{
//中文处理
$data['data'] = $this->uc2html($this->_excel_handle->sst['data'][$data['data']]);
}
else
{
$data['data'] = $this->_excel_handle->sst['data'][$data['data']];
case 1:
Case 2:
🎜> case 3:
Date
$this->_data[1][$i][$j][$k]=$data['data'];
🎜> public function main()
{
//Get Excel information
getExcelInfo(); > }
}
?>
http://www.bkjia.com/PHPjc/444996.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/444996.html
TechArticle
ExcelFileParser processes excel to obtain data, which can be imported into the database in batches to submit the form! DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

According to news on June 26, at the opening ceremony of the 2024 World Mobile Communications Conference Shanghai (MWC Shanghai), China Mobile Chairman Yang Jie delivered a speech. He said that currently, human society is entering the fourth industrial revolution, which is dominated by information and deeply integrated with information and energy, that is, the "digital intelligence revolution", and the formation of new productive forces is accelerating. Yang Jie believes that from the "mechanization revolution" driven by steam engines, to the "electrification revolution" driven by electricity, internal combustion engines, etc., to the "information revolution" driven by computers and the Internet, each round of industrial revolution is based on "information and "Energy" is the main line, bringing productivity development

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.
