phpexcel mysql 导出,使用PHP将简单的excel数据导出到MySQL中

My client got a excel file with the following structure

name | email

----------------------------

Name | email here

Name | email here

Name | email here

Name | email here

Name | email here

Name | email here

I would likes to make a MySQL database table according to this pattern and save the data into MySQL.

I am wonder how to do this.

Also there is an option needed

We have to check that if that corresponding user had a correct email address, ie of the form @ .

Can we check the data as a loop while importing ?

Also how to convert this data to MySQL ?

解决方案

Save this excel file as csv and run the following code with add your changings

$source = fopen('email.csv', 'r') or die("Problem open file");

while (($data = fgetcsv($source, 1000, ",")) !== FALSE)

{

$name = $data[0];

$email = $data[1];

mysql_query("INSERT INTO `table` (`name`,`email`) VALUES ('".$name."','".$email."') ");

}

fclose($source);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP可以使用PHPExcel库来导出MySQL数据Excel。 步骤如下: 1. 首先需要连接MySQL数据库,可以使用mysqli或PDO等扩展库。 2. 查询需要导出数据,将结果存储到一个数组。 3. 引入PHPExcel库,创建一个PHPExcel对象。 4. 设置Excel的属性,如标题、作者、描述等。 5. 创建一个工作表,并设置表头。 6. 将查询结果填充到工作表。 7. 设置Excel的输出格式为Excel2007,并输出Excel文件。 示例代码如下: ```php //连接MySQL数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); //查询需要导出数据 $sql = "SELECT * FROM table"; $result = mysqli_query($conn, $sql); $data = array(); while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } //引入PHPExcel库 require_once 'PHPExcel.php'; //创建PHPExcel对象 $objPHPExcel = new PHPExcel(); //设置Excel属性 $objPHPExcel->getProperties()->setTitle("MySQL数据导出")->setCreator("Your Name")->setDescription("MySQL数据导出"); //创建工作表 $objPHPExcel->setActiveSheetIndex(); $objPHPExcel->getActiveSheet()->setTitle("Sheet1"); //设置表头 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID'); $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Name'); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Age'); //填充数据 foreach ($data as $key => $value) { $row = $key + 2; $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $value['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $value['name']); $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $value['age']); } //设置输出格式为Excel2007 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //输出Excel文件 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="data.xlsx"'); header('Cache-Control: max-age='); $objWriter->save('php://output'); ``` 以上代码将查询结果导出Excel文件,并以附件形式下载。可以根据需要修改代码,如更改查询语句、表头等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值