programing

PHP가 Excel을 데이터베이스로 가져오기(xls & xlsx)

showcode 2023. 5. 25. 22:12
반응형

PHP가 Excel을 데이터베이스로 가져오기(xls & xlsx)

MySQL 데이터베이스로 Excel 파일을 가져오기 위해 플러그인을 검색하려고 했는데, 그 중 하나가 http://code.google.com/p/php-excel-reader/ 입니다.

이 도구는 매우 강력하여 전체 엑셀 콘텐츠를 html로 표시합니다.

다만 엑셀 파일을 읽고 예를 들어 배열로 내용을 추출한 다음 데이터베이스에 들어갈 SQL 문을 작성하면 될 것 같습니다.

좋은 코드와 패키지가 있을까요?감사합니다!

이 플러그인은 올바른 설명서와 예제를 사용하는 것이 가장 좋습니다.

https://github.com/PHPOffice/PHPExcel

추가 사항: 토론 포럼에서 도움을 요청할 수 있습니다. 그러면 저자로부터 하루 안에 답변을 받을 수 있습니다. 정말 인상적입니다.

때때로 대용량 xlsx 파일을 데이터베이스로 가져와야 하기 때문에spreadsheet-reader행당 파일을 읽을 수 있기 때문입니다.메모리 효율적인 가져오기 방법입니다.

<?php
    // If you need to parse XLS files, include php-excel-reader
    require('php-excel-reader/excel_reader2.php');

    require('SpreadsheetReader.php');

    $Reader = new SpreadsheetReader('example.xlsx');
    // insert every row just after reading it
    foreach ($Reader as $row)
    {
        $db->insert($row);
    }
?>

https://github.com/nuovo/spreadsheet-reader

처리하기 전에 .xls를 .csv로 변환할 수 있는 경우 아래 쿼리를 사용하여 csv를 데이터베이스로 가져올 수 있습니다.

load data local infile 'FILE.CSV' into table TABLENAME fields terminated by ',' enclosed by '"' lines terminated by '\n' (FIELD1,FIELD2,FIELD3)

Excel 파일을 CSV 파일로 저장하면 PHP MyAdmin과 같은 도구를 사용하여 mysql 데이터베이스로 가져올 수 있습니다.

이것이 당신의 상황에 도움이 될지는 모르겠지만, 수동 또는 프로그래밍 방식의 csv 파일이 내가 생각했던 엑셀 파일보다 데이터베이스로 구문 분석하는 것이 훨씬 쉬울 것입니다.

편집: 하지만 @diEcho 답변이 더 적절할 것 같기 때문에 저는 제 답변보다 다른 답변을 보는 것을 제안합니다.

나는 상속된 수업을 썼습니다.

<?php
class ExcelReader extends Spreadsheet_Excel_Reader {	
	
 function GetInArray($sheet=0) {

  $result = array();

   for($row=1; $row<=$this->rowcount($sheet); $row++) {
    for($col=1;$col<=$this->colcount($sheet);$col++) {
     if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
      $val = $this->val($row,$col,$sheet);
      $result[$row][$col] = $val;
     }
    }
   }
  return $result;
 }

}
?>

제가 할 수 있는 일은

<?php

 $data = new ExcelReader("any_excel_file.xls");
 print_r($data->GetInArray());

?>

사용할 수 있습니다.PHPOFFICE"diEcho"가 말했듯이.그런 다음 사용합니다.bin2hex()데이터베이스(SQL SERVER 또는 MySQL)에 BLOB 데이터 유형으로 데이터를 삽입하는 기능입니다.

bin2hex()기능은 저에게 많은 도움이 되었습니다.

언급URL : https://stackoverflow.com/questions/11447918/php-import-excel-into-database-xls-xlsx

반응형