简体   繁体   中英

Using PHPExcel to export Excel to MySql

I've found information on how to export data from MySQL to Excel, but not upside down, from Excel to MySQL.

Also, I'm asking using PHPExcel because a friend suggested to do it with it, but if there is an easiest way to do it, it's welcome.

I know I can export from excel to MySql (with static field) using following syntax:

LOAD DATA LOCAL INFILE '\\path' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'

But not all Excel fields will be imported into MySQL or in the same order.

This is the table structure that I have:

CREATE TABLE cliente (
  clienteId int(11) NOT NULL AUTO_INCREMENT,
  clienteIdentidadTipo varchar(45) DEFAULT NULL,
  clienteIdEntidadNumero varchar(45) DEFAULT NULL,
  clienteNombres varchar(100) DEFAULT NULL,
  clienteApellidosPrimero varchar(45) DEFAULT NULL,
  clienteApellidosSegundo varchar(45) DEFAULT NULL,
  clienteDireccion varchar(45) DEFAULT NULL,
  clienteCelular varchar(45) DEFAULT NULL,
  clienteTelefono varchar(45) DEFAULT NULL,
  clienteGenero varchar(45) DEFAULT NULL,
  clienteFechaNacimiento date DEFAULT NULL,
  clienteEstadoCivil varchar(45) DEFAULT NULL,
  clienteCargo varchar(45) DEFAULT NULL,
  clienteOcupacion varchar(45) DEFAULT NULL,
  clienteRecibeMensajesEmail tinyint(1) DEFAULT NULL,
  clienteEmail varchar(45) DEFAULT NULL,
  clienteClave varchar(100) DEFAULT NULL,
  clienteRecibeMensajesSms tinyint(1) DEFAULT NULL,
  clienteObjetivoVermeMejor tinyint(1) DEFAULT NULL,
  clienteObjetivoSalud tinyint(1) DEFAULT NULL,
  clienteObjetivoDesempeno tinyint(1) DEFAULT NULL,
  clientePracticaDeporte tinyint(1) DEFAULT NULL,
  clienteServicioCardio tinyint(1) DEFAULT NULL,
  clienteServicioPersonalizado tinyint(1) DEFAULT NULL,
  clienteServicioGrupales tinyint(1) DEFAULT NULL,
  clienteServicioPesas tinyint(1) DEFAULT NULL,
  clienteServicioFisioterapia tinyint(1) DEFAULT NULL,
  clienteServicioFuncional tinyint(1) DEFAULT NULL,
  clienteServicioOtro varchar(45) DEFAULT NULL,
  clienteMedioConocioGym varchar(45) DEFAULT NULL,
  clienteEmergenciaNombre varchar(45) DEFAULT NULL,
  clienteEmergenciaTelefono varchar(45) DEFAULT NULL,
  clienteEps varchar(45) DEFAULT NULL,
  clienteIps varchar(45) DEFAULT NULL,
  clienteCiudadId int(11) NOT NULL,
  clienteEmpresaId int(11) NOT NULL,
  clienteGrupoId int(11) NOT NULL,
  clienteCertificaConcentimientoInformado tinyint(1) DEFAULT NULL,
  clienteCertificaCuestionamientoAntecedente tinyint(1) DEFAULT NULL,
  clienteCongelacion int(11) DEFAULT NULL,
  clienteDiasTolerancia int(11) DEFAULT NULL,
  clienteFechaPago int(11) DEFAULT NULL,
  clienteTiquetera tinyint(1) DEFAULT NULL,
  clienteUsuario varchar(45) DEFAULT NULL,
  historiaClinicaId int(11) NOT NULL,
  planId int(11) NOT NULL,
  clientePracticaDeporteCual varchar(45) DEFAULT NULL,
  clienteCupoCredito int(7) NOT NULL,
  clienteImagen varchar(500) NOT NULL,
  clienteValidacion int(11) NOT NULL,
  clienteRh varchar(45) DEFAULT NULL,
  PRIMARY KEY (clienteId,clienteCiudadId,clienteEmpresaId,clienteGrupoId,historiaClinicaId,planId),
  UNIQUE KEY clienteId_UNIQUE (clienteId),
  KEY fk_cliente_ciudad_idx (clienteCiudadId),
  KEY fk_cliente_empresa1_idx (clienteEmpresaId),
  KEY fk_cliente_grupo1_idx (clienteGrupoId),
  KEY fk_cliente_historiaClinica1_idx (historiaClinicaId),
  KEY fk_cliente_plan1_idx (planId),
  CONSTRAINT fk_cliente_ciudad FOREIGN KEY (clienteCiudadId) REFERENCES ciudad (ciudadId) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_cliente_empresa1 FOREIGN KEY (clienteEmpresaId) REFERENCES empresa (empresaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_cliente_grupo1 FOREIGN KEY (clienteGrupoId) REFERENCES grupo (grupoId) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_cliente_historiaClinica1 FOREIGN KEY (historiaClinicaId) REFERENCES historiaclinica (historiaClinicaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_cliente_plan1 FOREIGN KEY (planId) REFERENCES plan (planId) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And this is the Excel file that I want to import into the database: 截屏

This will probably help: Import an excel file into a MySQL table with PHPExcel

Also, in the future, most of the questions you will have with programming have already been answered. So you can usually find the answer in 1-2 google searches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM