0

I want to upload an Excel file in my codeigniter project. But before uploading I want to show number of records in the file. So I created a function in script and called the function in the onclick event of submit button. In the function I wrote the following code to count the rows

var excel = new ActiveXObject("Excel.Application");
var fil = $('#contactFile');
var excel_file = excel.Workbooks.Open(fil.value);
var excel_sheet = excel.Worksheets(1);
var rows = excel_sheet.UsedRange.Rows.Count;
alert(rows);

But it shows an error that is,

Uncaught ReferenceError: ActiveXObject is not defined

I think modern web browser's do not support ActiveXObject. But instead of this ActiveXObject, there may be some other methods in modern browsers. But after a long search I didn't get a solution. Someone please help me

geeth
  • 704
  • 2
  • 14
  • 42

1 Answers1

0

I using angular 6. I hope it can help you. My idea is counting rows in excel file after uploading file. enter image description here enter image description here

import * as XLSX from 'xlsx';
type AOA = any[][];

export class ETicketStep3Component implements OnInit {
  constructor(
  ) {
  }
  ...

    data: AOA = [];
  wopts: XLSX.WritingOptions = { bookType: 'xlsx', type: 'array' };

  onFileChange(evt: any, output: UploadOutput) {
    /* wire up file reader */
    const target: DataTransfer = <DataTransfer>(evt.target);
    if (target.files.length !== 1) throw new Error('Cannot use multiple files');
    const reader: FileReader = new FileReader();
    reader.onload = (e: any) => {
      /* read workbook */
      const bstr: string = e.target.result;
      const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });

      /* grab first sheet */
      const wsname: string = wb.SheetNames[0];
      const ws: XLSX.WorkSheet = wb.Sheets[wsname];

      /* save data */
      this.data = <AOA>(XLSX.utils.sheet_to_json(ws, { header: 1 }));
      console.log("The count in excel file: ",this.data.length)
    };
    reader.readAsBinaryString(target.files[0]);
  }
}
Fourat
  • 2,366
  • 4
  • 38
  • 53