我有codeigniter应用程序,现在我需要使用codigniter上载excel。 怎么做。我遵循一些在线教程,但没有完整的教程。特别是,它们没有使用什么库。
if(isset($_POST['Submit']))
{ $ mimes = array('application / vnd.ms-excel','text / xls','text / xlsx','application / vnd.oasis.opendocument.spreadsheet','application / vnd.openxmlformats-officedocument.spreadsheetml。片'); if(in_array($ _ FILES [“ file”] [“ type”],$ mimes)) { $ uploadFilePath ='uploads /'。basename($ _ FILES ['file'] ['name']);
$filename = pathinfo($uploadFilePath);
$f = array("name"=>$filename['filename']);
foreach ($f as $k)
{
$policy = $k;
}
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
//echo "You have total ".$totalSheet." sheets".
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$treeno ++;
$policyno = isset($Row[0]) ? $Row[0] : '';
$act_treeno = isset($Row[1]) ? $Row[1] : '';
$dbh = isset($Row[2]) ? $Row[2] : '';
$height = isset($Row[3]) ? $Row[3] : '';
$pno = substr($policyno, -6);
$treeno = $pno.'_'.$act_treeno.'_T';
$query = "insert into tbl_trees(policyno,actual_treeno,dbh,height,treeno) values('".$policyno."','".$act_treeno."','".$dbh."','".$height."','".$treeno."')";
echo '</br>';
$mysqli->query($query);
这是工作,但我不知道在codeigniter中使用此方法。
答案 0 :(得分:0)
首先认为您需要上传该文件,然后尝试打开并准备要插入的数据-
$fileName = $_FILES['file']['name'];
$uploadData = array();
if (!empty($fileName)) {
$config['upload_path'] = './your location/';
$config['allowed_types'] = 'txt|csv|xls|xlsx';
$config['max_size'] = 2048;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file')) {
json_error("File Upload Error", $this->upload->display_errors(), 400);
} else {
$uploadData = array('upload_data' => $this->upload->data());
}
}//if
准备插入数据-
if (!empty($uploadData)) {
$fileName = $uploadData['upload_data']['file_name'];
$data = array(); //empty array;
$fileInfo = fopen(base_url() . 'your file location/' . $fileName, "r");
$i = 0;
while (!feof($fileInfo)) {
$colInfo = fgets($fileInfo); //fgets means read file in line by line.
//prefer data to save
$data[$i]['your_column'] = $colInfo;
$i++;
}//while
fclose($fileInfo);
}
插入批处理数据-
$this->db->insert_batch('dbtable', $data);