Computer Science, asked by TAQUI9557, 1 year ago

While exporting data to xls file the data start from second row in codeingniter why?

Answers

Answered by shekhar713370
0

Most of the time my clients need to download data from their database tables. Exporting to CSV is a pain in the rear for users and it leads to confusion (you know the colon and semicolon stuff). Today, I decided to make a very small controller that is portable and efficient for exporting full MySQL tables to Excel 2003 using PHPExcel and CodeIgniter.

First of all, you need PHPExcel which should be installed as a CodeIgniter library. In order to do this, you should follow the steps posted here.

Once you have PHPExcel installed and configured, make a controller exactly like this one:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

class Table_export extends Controller {

 

   function __construct()

   {

       parent::Controller();

 

       // Here you should add some sort of user validation

       // to prevent strangers from pulling your table data

   }

 

   function index($table_name)

   {

       $query = $this->db->get($table_name);

 

       if(!$query)

           return false;

 

       // Starting the PHPExcel library

       $this->load->library('PHPExcel');

       $this->load->library('PHPExcel/IOFactory');

 

       $objPHPExcel = new PHPExcel();

       $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

 

       $objPHPExcel->setActiveSheetIndex(0);

 

       // Field names in the first row

       $fields = $query->list_fields();

       $col = 0;

       foreach ($fields as $field)

       {

           $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);

           $col++;

       }

 

       // Fetching the table data

       $row = 2;

       foreach($query->result() as $data)

       {

           $col = 0;

           foreach ($fields as $field)

           {

               $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);

               $col++;

plz like me

Similar questions