node读取docx文件并写入数据库

徘徊边缘 提交于 2020-03-10 10:50:32
const mysqlx = require("@mysql/xdevapi");
const path = "E:\\微信文档\\WeChat Files\\mxds123456\\FileStorage\\File\\2020-03\\101-200";
const fs = require('fs');
const iconv = require("iconv-lite");
var mammoth = require("mammoth");

fs.readdir( path, (err, files)=>        // 读取文件夹下所有文件名(返回数组)
{
  if(files.length > 0)
  {
    console.log(files.length);
    files = files.splice(6, 93);

    console.log(files.length);

    for(let i = 0; i < files.length; i++)
    {
      {
        const pathContent = `${path}\\${ files[i] }`;
        console.log(pathContent);

        mammoth.extractRawText({path: pathContent})   //读取docx文件
          .then(function(result)
          {
            execute_mysql( files[i].slice(0, 3), result.value);
          })
          .catch(e => console.log(e))
          .done();
      }
    }
  }
});

async function execute_mysql(name, report)
{
  console.log(name, report);

  const mysqlx = require("@mysql/xdevapi");
  const session = await mysqlx.getSession({
    user: 'root',
    password: '123456',
    host: 'localhost',
    port: '33060'
  });

  const sql = `insert into test1.patient_report(name, report) values( "${name}", "${ report }" )`;
  console.log(sql);

  await session.sql(sql).execute( r=> console.log(r) );

  await session.close();
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!