java生成sql文件_java解析excel生成sql文件

该博客介绍了一个Java程序,它读取Excel文件中的数据,然后根据读取的内容生成SQL插入语句。程序首先打开指定路径的Excel文件,接着遍历工作表中的每一行,提取每行的特定列(如'testName'和'user_'),并将这些数据转化为INSERT IGNORE INTO SQL语句,写入到一个新的SQL文件中。最后,程序输出了生成SQL语句的耗时。
摘要由CSDN通过智能技术生成

packagedemo;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classexcelToSql {public static void executeInfo() throwsIOException{

String filePath= "D:/FavoriteVideo/testExcel.xlsx"; //指定本地的数据目录

String slqPath = "D:/FavoriteVideo/testSql.sql"; //指定生成文件目录

try{

InputStream in= newFileInputStream(filePath);

Workbook wb= newXSSFWorkbook(in);

in.close();

String sql= "";

Sheet sheetAt= wb.getSheetAt(0);int lastRowNum =sheetAt.getLastRowNum();

Map map= newHashMap();

sql= "";

List excelBean =getExcelBean(sql, sheetAt, lastRowNum);

Map map1 =new HashMap<>();for(ExcelBean bean :excelBean){map1.put(bean.getTestName(),bean.getUser_());

}

Iterator> it =map1.entrySet().iterator();while(it.hasNext()) {

Map.Entry entry =it.next();

System.out.println("key= " + entry.getKey() + " and value= " +entry.getValue());

sql+= String.format("INSERT IGNORE INTO `test_table_info` (`testName`, `updateTime`, `createTime`, `user_`) VALUES ('%s', now(), now(), '%s');\n", entry.getKey(), entry.getValue());

}

System.out.println(map1);

System.out.println(map1.size());

File file= newFile(slqPath);if(!file.exists()){

file.createNewFile();

}byte[] bytes =sql.getBytes();

OutputStream os= newFileOutputStream(slqPath);

os.write(bytes);

os.flush();

os.close();

System.out.println("sheets");

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}private static List getExcelBean(String sql, Sheet sheetAt, intlastRowNum) {

List list = new ArrayList<>();for (int rowNum=0; rowNum <= lastRowNum; rowNum++){

ExcelBean bean= new ExcelBean(); //创建一个单独Bean、

System.out.println("rowNum = "+rowNum);

Row row=sheetAt.getRow(rowNum);

String testName= "";

String user_= "";if (row != null){

Cell cell= row.getCell(0);if (cell != null) {

testName=cell.getStringCellValue();

bean.setTestName(testName);

}

cell= row.getCell(1);if (cell != null) {

user_=cell.getStringCellValue();

bean.setUser_(user_);

}/*** 更多列在这里补充*/}

list.add(bean);

}returnlist;

}public static void main(String args[]) throwsIOException{long startTime =System.currentTimeMillis();

executeInfo();long endTime =System.currentTimeMillis();

System.out.println("耗时为m:" + (endTime - startTime)/1000);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值