java导入excel:

package org.example;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

public class ReadEccel {
static Tomysql tomysql;
public static Connection getConnection()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/jianmin?serverTimezone=GMT%2B8";
String username = "root";
String password="123456";
Connection a=null;
try {
a = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public static String insert(String content, String channelName, String title)
{
System.out.println(title);
String result="导入成功";
Connection coon = getConnection();
String sql ="Insert into news(content,channelName,title)values(?,?,?)";
try {
PreparedStatement ps = coon.prepareStatement(sql.toString());
coon.setAutoCommit(false);
ps.setString(1, content);
ps.setString(2, channelName);
ps.setString(3, title);
ps.addBatch();
ps.executeBatch();
coon.commit();
ps.close();
coon.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return result;
}
Map<String, Integer> map = new HashMap<>();
public static void main(String[] args) {
{
File Excel=new File("D:\\桌面\\健民\\2021-11-25\\1617241934831197.xlsx");
Workbook wb;
try {
wb = new XSSFWorkbook(Excel);
for(int i=0;i<9;i++) {
Sheet sheet = wb.getSheetAt(i); //读取sheet 0
int firstRowIndex = sheet.getFirstRowNum() + 1;
int lastRowIndex = sheet.getLastRowNum();
for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行
System.out.println(rIndex);
Row row = sheet.getRow(rIndex);
if (row != null) {


}
}
}
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}

}

}
public static void wordFrequency(String article) throws IOException {
Map<String, Integer> map = new HashMap<>();


}

}
posted on 2021-11-09 17:00  sean1246  阅读(58)  评论(0编辑  收藏  举报