Welcome Guest, Not a member yet? Register   Sign In
Export database data into different columns of a csv file
#1

[eluser]ethereal1m[/eluser]
Dear all,
I want to export data from database into a csv file using the following code:
Code:
function export()
  {
  
  $this->load->dbutil();
  $this->db->order_by('id','asc');
  $query = $this->db->get('some_table');
  $enclosure = '';
  $delimiter = ",";
  $newline = "\r\n";
  $data = $this->dbutil->csv_from_result($query,$delimiter,$newline,$enclosure);
  
   if ( ! write_file('my_directory\data.csv', $data))
   {
    echo 'Unable to write the file';
   }
   else
   {
    echo 'File written!';
   }
  }
Basically it uses csv_from_result to create the layout and later it is written to a file using write_file. I delimit each column with comma (,) and each row with carriage return. However when I open the file from Excel, each column is separated by comma and placed into one cell of Excel instead of different cells. Like this:

How can I make so that they are in different cells? What should I used for the arguments?

regards,
ethereal1m
#2

[eluser]ethereal1m[/eluser]
I got it, I put
Code:
/t
as delimiter instead of comma....
#3

[eluser]jpganz[/eluser]
Hi, any other way to do this? I tried with /t but still no work.. any idea?
#4

[eluser]Hampti[/eluser]
Hi there,

1) use
Code:
$enclosure = '"';
  $delimiter = ";";

2) set MS Excel CSV-Import Options accordingly

3) use openOffice or libreOffice

If i am right the latter ones let you choose delimiter, enclosure and character set on csv import. In Microsoft Excel it has to be set somewhere in options i believe, you wont get asked on importing the file.

Best Regards,
Constantin





Theme © iAndrew 2016 - Forum software by © MyBB