How to Import CSV/xls files to drupal? Eg: i have number of fields like name, id, address etc stored as csv/excel sheet. Now i want to retrive the content to my drupal page.

Comments

rajiv.singh’s picture

Use feeds module to import the CSV stuff.

kannanraj’s picture

i my new in drupal so please explain how to use feeds module in drupal for importing csv/excel file

rob230’s picture

Install ctools and job_scheduler and feeds.

Go to admin/structure/feeds, click add importer, fill in the details. It's mostly self-explanatory, but there is some help here.

Afterwards go to yoursite/import and click your importer.

kannanraj’s picture

sorry im new in drupal so only ask again this question. how to import csv files using feeds module in drupal 7 plz tel me step by step process. including enable feeds module

PrabhuG’s picture

Hi Kannan,

Please check it on below site, if you are using profile 2 for drupal 7.
http://drupal.org/node/1285276

And please avoid duplicate questions, If you need to edit your post, then you can easily navigate in dashboard.
http://drupal.org/node/1905088
http://drupal.org/node/1905172

@rajiv.singh and @Rob230 - thank you for the quick support. Kannan has made his first comment :)

best regards,
Prabhu G

WorldFallz’s picture

Please don't post duplicate threads, I've deleted the dupe. Thanks.

dhineshkumar’s picture

I found 'Taxonomy Csv Importexport' module on http://drupalmodules.com/module/taxonomy-csv-importexport site. Check this link.

saurabh.tripathi.cs’s picture


function post_procure_menu() {
    $form['down']= array(
    '#prefix'  => '<br><b>Upload File in Below format:</b><br>
    <table><tr><th>Courier name</th><th>AWB</th></tr>
    <tr><td>DTDC</td><td>2238383</td></tr>
    <tr><td>Fedex</td><td>244355</td></tr><tr><td>BLUEDART</td><td>4343545</td></tr></table><br>etc.',
    '#suffix'  => '</div>',
    ); 
   
 
 
  return $form;
    
}
/**
 * Implements submitform of post_procure_awb_upload
 */

function post_procure_awb_upload(){
        	drupal_add_js(drupal_get_path('module','post_procure').'/post_procure.js'); 
	drupal_add_css(drupal_get_path('module', 'post_procure')."/post_procure.css");
  
    $form['post_procure_awbfile'] = array(
    '#type' => 'file',
    '#title' => t('Select Awb File'),
    '#size' => 22,
    '#description' => '<br/>#Note : xls and xlsx are the only two valid extensions',
    );
    $form['post_procure_paisareport'] = array(
    '#type' => 'submit', 
    '#value' => t('Upload'),
    );
    $form['down']= array(
    '#prefix'  => '<br><b>Upload File in Below format:</b><br>
    <table><tr><th>Courier name</th><th>AWB</th></tr>
    <tr><td>DTDC</td><td>2238383</td></tr>
    <tr><td>Fedex</td><td>244355</td></tr><tr><td>BLUEDART</td><td>4343545</td></tr></table><br>etc.',
    '#suffix'  => '</div>',
    ); 
   
 
 
  return $form;
    
}
/**
 * Implements validate of post_procure_awb_upload
 */
function post_procure_awb_upload_validate($form , &$form_state){
    
	$file = file_save_upload('post_procure_awbfile', array('file_validate_extensions' => array('xls xlsx'),));
	require_once 'excel_reader2.php';
        
	if ($file) {
      if ($file = file_move($file, 'public://')) {
        $form_state['values']['file'] = $file;
        
        
      }else {
        form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
      }
    }
}
/**
 *Implements submit of post_procure_paisa_upload
*/
function post_procure_awb_upload_submit($form, &$form_state){
    
    include 'simple/simplexlsx.class.php';
    require_once 'excel_reader2.php';
    $file = $form_state['values']['file'];
    $file->status = FILE_STATUS_PERMANENT;
    $filepath = drupal_realpath($file->uri);
    
    ###################mapping of excel readers###################
    $ext = pathinfo($filepath, PATHINFO_EXTENSION);
    ##############################################################
    $output = array();
    //extension logic starts here
    $xlsx_rw = NULL;
   
   if($ext == 'xls'){
      $xlsx_rw = 2;
      $data = new Spreadsheet_Excel_Reader($filepath);
      $numrows = count($data->sheets[0]['cells']);
      $numcols = $data->sheets[0]['numCols']; 
    //xls start from zero 0
    for($row = 2; $row <= $numrows; $row++){
    for ($col = 1; $col <= $numcols; $col++) {
    $output[$row][$col] = $data->val($row, $col, $sheet = 0);            
    }          
    }
    }
    else if($ext == 'xlsx'){
      $xlsx_rw = 1;
      $data = new SimpleXLSX($filepath);
      $test =  list($numcols, $numrows) = $data->dimension();
      $info = $data->rows();
      //xlsx start from zero 0
      for($r = 1; $r < $numrows; $r++){
        $row = $r;  
        foreach($info[$r] as $col => $cols){
         $output[$row][$col+1] = $cols;        
        }  
        
        
      }
    }
 
   
  foreach($output as $cky => $csv_values){
    $maxsno++;
    
    if($cky > 0 && !empty($csv_values[1])){  
    $courier=check_plain($csv_values[1]);
    $awb=check_plain($csv_values[2]);
    
    $checkawb=db_select('sales_awb_details','aw');
    $checkawb->fields('aw',array('awb'));
    $checkawb->condition('aw.awb',$awb);
    $checkawbre = $checkawb->countQuery()->execute()->fetchField();
    
    if(!$checkawbre){
        if($courier=='DTDC'){
        variable_set('DTDCawb_mail_sent',0);
        variable_set('DTDC',0);
        }
        if($courier=='BLUEDART'){
        variable_set('BLUEDARTawb_mail_sent',0);
        variable_set('BLUEDART',0);
        }
        if($courier=='Fedex'){
        variable_set('Fedexawb_mail_sent',0);
        variable_set('Fedex',0);
        }
        $query=db_insert('sales_awb_details')
        ->fields(array(
        'name' => $courier,
        'awb' => $awb,
        'sales_record_no' => 0,
        'used' => 0,
        ));
        $res = $query->execute();
        }//end of if
        }
        } //end of foreach

    $getaw1=db_select('sales_awb_details','aw');
    $getaw1->fields('aw',array('awb'));
    $getaw1->condition('aw.used',0);
    $getaw1->condition('name','Fedex');
    $res1 = $getaw1->countQuery()->execute()->fetchField();
    $getaw2=db_select('sales_awb_details','aw');
    $getaw2->fields('aw',array('awb'));
    $getaw2->condition('aw.used',0);
    $getaw2->condition('name','DTDC');
    $res2 = $getaw2->countQuery()->execute()->fetchField();
    $getaw3=db_select('sales_awb_details','aw');
    $getaw3->fields('aw',array('awb'));
    $getaw3->condition('aw.used',0);
    $getaw3->condition('name','BLUEDART');
    $res3 = $getaw3->countQuery()->execute()->fetchField();
 drupal_set_message("AWB file uploaded successfully.<br><b>CURRENT STATUS</b><br>
 <table><tr><th>Courier name</th><th>Unused AWB</th></tr>
 <tr><td>Fedex</td><td>".$res1."</td>
 <tr><td>DTDC</td><td>".$res2."</td>
 <tr><td>BLUEDART</td><td>".$res3."</td>
 </table> "); 
 
   
 }


ymys’s picture

Dear,
How to do it in drupal 6? I already have import.module and imporxls.inc and your code I put in imporxls.inc but there's no luck. I got this error:
Fatal error: Call to undefined function drupal_realpath() in D:\www\mydru6\sites\all\modules\excert\importxls\imporxls.inc on line 96

And on line 96 is: $filepath = drupal_realpath($file->uri);

Mayble I should have a function called drupal_realpath?