# Import excel data in cakephp data base

**URL:** <https://discourse.cakephp.org/t/import-excel-data-in-cakephp-data-base/6937>\
**Category:** Need Help\
**Created:** [November 28, 2019, 11:36am UTC](https://discourse.cakephp.org/t/import-excel-data-in-cakephp-data-base/6937 "2019-11-28T11:36:43Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [December 12, 2019, 1:52am UTC](https://discourse.cakephp.org/t/import-excel-data-in-cakephp-data-base/6937/3 "2019-12-12T01:52:21Z")

</div>

Just write a function that will import a csv with fields you need. Not a cake example but similar and you should get the idea:

```auto
    public function incsv()
    {
        $file = 'C:\mydocs\incsv.csv';
        $content = file($file);
        $array = array();

        for ($i = 1; $i < count($content); $i++) {
            $line = explode(',', $content[$i]);
            for ($j = 0; $j < count($line); $j++) {
                $array[$i][$j + 1] = $line[$j];
            }
        }

        $k = count($array) + 1;
        for ($i = 1; $i < $k; $i++) {
            $tdate = new \DateTime($array[$i][2]);
            $ndate = $tdate->format('Y-m-d');
            $descraw = $array[$i][8];
            $descspace = preg_replace('/[^a-z\d]/i', '', $descraw);
            $desc = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', $descspace);

            $amt = $array[$i][3];
            $namt = number_format($amt, 2, '.', '');
            if ($namt < 0) {
                $wd = $namt * -1;
                $dep = 0;
                } else {
                $dep = $namt;
            }
            $maxid = $this->Check->getMaxid();
            $checkid = $maxid + 1;

            $data = [
                'checkid' => $checkid,
                'transdate' => $ndate,
                'transdescribe' => $desc,
                'widthdraw' => $wd,
                'deposit' => $dep
            ];
            $this->Check->insertCsv($data);
        }
        $this->Check->checkRecalc();
        Url::redirect('check/index');
    }

```

I did not need to use all fields, so just got the needed ones.

To achieve something like:

 ![](https://i.imgur.com/FHvPLIS.jpg)

Besides learning cake I would highly advise you to study and learn various PHP methods and functions. Cake is a PHP framework.

And the above is just an example you need to figure out your own routine that works in your case.

---

_[View the full topic](https://discourse.cakephp.org/t/import-excel-data-in-cakephp-data-base/6937)._
