1.读取Excel表格数据
[Java] 纯文本查看 复制代码 private static void writeStore(String path) throws Exception {
Workbook workbook = new XSSFWorkbook(new File(path));
//读取excel中的sheet表名
Sheet sheet = workbook.getSheet("xxx表名");
int totalRow = sheet.getLastRowNum();
System.out.println("总共:" + totalRow);
//循环独处表中每一行中每一列的值
for (int i = 1; i <= totalRow; i++) {
Row row = sheet.getRow(i);
// 门店编码
String store_code = row.getCell(1).getStringCellValue();
// 门店名称
String store_name = row.getCell(2).getStringCellValue();
// 门店地址
String store_address = row.getCell(3).getStringCellValue();
// 经度 转换string类型
String longitude = resultContent(row.getCell(4));
// 纬度
String latitude = resultContent(row.getCell(5));
.....
String storeSql = "insert into 表名 (store_code,store_name,store_address,longitude,latitude,......)"
+ " values('" + store_code + "','" + store_name + "','" + store_address + "','" + longitude + "','"
+ latitude + "');";
System.out.println(storeSql);
//将sql写入文档中
writeTxtCount("存入sql文件.sql", storeSql);
}
}
2.存入.sql文件中
[Java] 纯文本查看 复制代码 //传入文件路径 和生成的sql
private static boolean writeTxtCount(String path, String content) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(path, true));
bufferedWriter.write(content + "\r\n");
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
##方法
[Java] 纯文本查看 复制代码 //转化成string
private static String resultContent(Cell cell) {
cell.setCellType(CellType.STRING);
return cell.getStringCellValue();
}
[Java] 纯文本查看 复制代码 public static void main(String[] args) throws Exception {
writeStore("表名路径.xlsx ");
}
|