Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the simple import function of the ExecelPHP class tp3.1.3.
Public function import ($ file = ''){
Vendor ('excel. PHPExcel ');
Vendor ('excel. PHPExcel. IOFactory ');
Vendor ('excel. PHPExcel. Reader. excel5 ');
$ ObjReader = new PHPExcel_Reader_Excel5;
$ ObjPHPExcel = $ objReader-> load ($ file );
$ Sheet = $ objPHPExcel-> getSheet (0 );
$ HighestRow = $ sheet-> getHighestRow ();
$ HighestColumn = $ sheet-> getHighestColumn ();
$ Count = 0;
$ S_count = 0;
$ T_count = 0;
$ B _count = 0;
$ M_count = 0;
$ String = '';
For ($ j = 2; $ j <= $ highestRow; $ j ++ ){
$ Haipin_id = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("A". $ j)-> getValue (); // haipin No.
$ Goods_name = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("B". $ j)-> getValue (); // Product Name
$ Common_name = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("E". $ j)-> getValue (); // common name
$ License_number = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("F". $ j)-> getValue (); // approve the document number
$ Spec = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("G". $ j)-> getValue (); // Specification
$ Manufacturer = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("Q". $ j)-> getValue (); // manufacturer
$ Brand = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("R". $ j)-> getValue (); // brand
$ Type = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("S". $ j)-> getValue (); // type
$ Cate = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("U". $ j)-> getValue (); // category
$ Keywords = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("C". $ j)-> getValue (); // keyword
$ Effect = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("D". $ j)-> getValue (); // Efficacy
$ Is_drug = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("H". $ j)-> getValue (); // prescription drug
$ Drug_description = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("I". $ j)-> getValue (); // prescription drug description
$ Form = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("J". $ j)-> getValue (); // dosage form
$ Material = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("L". $ j)-> getValue (); // raw material
$ Leading_effect = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("M". $ j)-> getValue (); // Function
$ Crowd = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("N". $ j)-> getValue (); // applicable audience
$ Instructions = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("P". $ j)-> getValue (); // usage
$ Is_import = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("T". $ j)-> getValue (); // whether to import
$ Department = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("V". $ j)-> getValue (); // department name
$ Haipin = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("W". $ j)-> getValue (); // haipin pharmacy
$ Zhimeilan = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("X". $ j)-> getValue (); // Mary
$ Off_line = trim ($ objPHPExcel-> getActiveSheet ()-> getCell ("Y". $ j)-> getValue (); // offline
$ Goods_data = array ();
$ Empty_field = 0;
If (empty ($ cate )){
Continue;
} Else {
$ Cate_id = A ('admin/cate')-> check_cate_exists ($ Cate );
If (! $ Cate_id ){
$ String. = $ cate. "The category does not have a category list. Please enter it first.
";
Continue;
} Else {
$ Goods_data ['cate _ id'] = (int) $ cate_id;
}
}
If (empty ($ department )){
$ Goods_data ['department _ id'] = 0;
} Else {
$ Department_id = $ this-> check_department_exists ($ department );
If (! $ Department_id ){
$ Goods_data ['department _ id'] = 0;
} Else {
$ Goods_data ['department _ id'] = (int) $ department_id;
}
}
If (empty ($ brand )){
// Continue;
$ Goods_data ['brand _ id'] = 0;
$ Empty_field = 1;
} Else {
$ Brand_id = A ('admin/brand')-> check_brand_exists ($ Brand );
If (! $ Brand_id ){
$ Data = array (
'Brand _ name' => $ brand,
);
$ Insert_brand_id = M ('brand')-> add ($ data );
If ($ insert_brand_id! = False ){
$ B _count ++;
$ Goods_data ['brand _ id'] = $ insert_brand_id;
} Else {
Continue;
}
} Else {
$ Goods_data ['brand _ id'] = $ brand_id;
}
}
If (empty ($ type )){
// Continue;
$ Goods_data ['Type _ id'] = 0;
} Else {
$ Type_id = A ('admin/GoodsType ')-> check_type_exists ($ type );
If (! $ Type_id ){
$ Data = array (
'Type _ name' => $ type,
);
$ Insert_type_id = M ('goods _ type')-> add ($ data );
If ($ insert_type_id! = False ){
$ T_count ++;
$ Goods_data ['Type _ id'] = $ insert_type_id;
} Else {
Continue;
}
} Else {
$ Goods_data ['Type _ id'] = $ type_id;
}
}
If (empty ($ spec )){
// Continue;
$ Goods_data ['spec _ id'] = 0;
$ Empty_field = 1;
} Else {
$ Spec_id = A ('admin/GoodsSpec ')-> check_goods_spec_exists ($ spec );
If (! $ Spec_id ){
$ Data = array (
'Spec _ name' => $ spec,
);
$ Insert_spec_id = M ('goods _ spec ')-> add ($ data );
If ($ insert_spec_id! = False ){
$ S_count ++;
$ Goods_data ['spec _ id'] = $ insert_spec_id;
} Else {
Continue;
}
} Else {
$ Goods_data ['spec _ id'] = $ spec_id;
}
}
If (empty ($ manufacturer )){
// Continue;
$ Goods_data ['manufacturer _ id'] = 0;
$ Empty_field = 1;
} Else {
$ Manufacturer_id = A ('admin/Manufacturer ')-> check_manufacturer_exists ($ manufacturer );
If (! $ Manufacturer_id ){
$ Data = array (
'Manufacturer _ name' => $ manufacturer,
);
$ Insert_manufacturer_id = M ('manufacturer')-> add ($ data );
If ($ insert_manufacturer_id! = False ){
$ M_count ++;
$ Goods_data ['manufacturer _ id'] = $ insert_manufacturer_id;
} Else {
Continue;
}
} Else {
$ Goods_data ['manufacturer _ id'] = $ manufacturer_id;
}
}
$ Goods_data ['is _ drug '] = array_search (strtoupper ($ is_drug), C (IS_DRUG ));
If (empty ($ goods_data ['is _ drug ']) {
$ Goods_data ['is _ drug '] = 0;
$ Empty_field = 1;
}
$ Goods_id = $ this-> is_exist_goods ($ goods_name, $ goods_data ['brand _ id'], $ goods_data ['is _ drug '], $ goods_data ['spec _ id'], $ goods_data ['manufacturer _ id']);
If (! $ Goods_id ){
$ Goods_data ['haipin _ id'] = $ haipin_id;
$ Goods_data ['goods _ name'] = $ goods_name;
$ Goods_data ['common _ name'] = $ common_name;
$ Goods_data ['license _ number'] = $ license_number;
$ Goods_data ['keyword'] = $ keywords;
$ Goods_data ['effect '] = $ effect;
// $ Goods_data ['is _ drug '] = array_search (strtoupper ($ is_drug), C (IS_DRUG ));
$ Goods_data ['drug _ description'] = $ drug_description;
$ Goods_data ['form'] = $ form;
$ Goods_data ['material '] = $ material;
$ Goods_data ['leading _ effect '] = $ leading_effect;
$ Goods_data ['crowd '] = $ crowd;
$ Goods_data ['structs'] = $ instructions;
$ Goods_data ['is _ import'] = empty ($ is_import )? 0: 1;
$ Goods_data ['add _ time'] = time ();
$ Goods_data ['user _ id'] = session ('uid ');
$ Goods_model = M ('goods ');
$ Goods_model-> startTrans ();
$ Goods_id = $ goods_model-> add ($ goods_data );
If ($ goods_id! = False ){
$ G_new_goods_sn = '';
$ G_new_goods_sn. = str_pad (string) $ goods_data ['cate _ id'], 1, "0", STR_PAD_LEFT );
$ G_new_goods_sn. = str_pad (string) $ goods_id, 7, "0", STR_PAD_LEFT );
$ Goods_model-> save (array ('new _ goods_sn '=> $ g_new_goods_sn, 'goods _ id' => $ goods_id ));
// Process the relationship between the website and the product
If ($ haipin ){
$ S_data = array (
'Site _ id' => 2,
'Goods _ id' => $ goods_id,
'Type' => 1,
);
$ This-> deal_with_goods_to_site ($ s_data );
}
If ($ zhimeilan ){
$ S_data = array (
'Site _ id' => 3,
'Goods _ id' => $ goods_id,
'Type' => 1,
);
$ This-> deal_with_goods_to_site ($ s_data );
}
If ($ off_line ){
$ S_data = array (
'Site _ id' => 4,
'Goods _ id' => $ goods_id,
'Type' => 1,
);
$ This-> deal_with_goods_to_site ($ s_data );
}
If (! $ Empty_field ){
$ Goods_sn = '';
$ Cate_res = M ('cate')-> field ('Alias _ name')-> where (array ('cate _ name' => $ Cate )) -> find ();
$ Goods_sn. = substr ($ cate_res ['Alias _ name'], 0, 2 );
$ Goods_sn. = str_pad (string) $ goods_data ['brand _ id'], 5, "0", STR_PAD_LEFT );
$ Goods_sn. = str_pad (string) $ goods_data ['is _ drug '], 3, "0", STR_PAD_LEFT );
$ Goods_sn. = str_pad (string) $ goods_id, 5, "0", STR_PAD_LEFT );
$ Goods_sn. = str_pad (string) $ goods_data ['manufacturer _ id'], 5, "0", STR_PAD_LEFT );
$ Goods_sn. = str_pad (string) $ goods_data ['spec _ id'], 5, "0", STR_PAD_LEFT );
$ Data = array (
'Goods _ id' => $ goods_id,
'Goods _ sn '=> $ goods_sn,
);
$ Res = $ goods_model-> save ($ data );
If ($ res! = False ){
$ Goods_model-> commit ();
} Else {
$ Goods_model-> rollback ();
}
} Else {
$ Goods_model-> commit ();
}
$ Count ++;
} Else {
$ Goods_model-> rollback ();
$ String. = $ haipin_id. '/'. $ goods_name. "input failed.
";
}
} Else {
$ String. = $ haipin_id. '/'. $ goods_name. "already exists.
";
}
}
$ O_string = '';
$ O_string. = 'input in this operation '. $ count.' products ,';
$ O_string. = 'input '. $ s_count.' specifications ,';
$ O_string. = 'input '. $ t_count.' type ,';
$ O_string. = 'input '. $ B _count.' brand ,';
$ O_string. = 'input '. $ m_count. "manufacturers.
";
Return''. $ O_string. $ string .'
';
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB