String path = "D:\\C桌面\\CSDN学习计划\\技术训练营\\Excel表格学习\\";
FileInputStream fileInputStream = new FileInputStream(path + "persion.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
//总列数
Row hssfRow=sheet.getRow(0);
int columnNum=hssfRow.getPhysicalNumberOfCells();
//总行数
int rowNum=sheet.getLastRowNum();
FileInputStream fileInputStream = new FileInputStream(path + "persion.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
int rowNum=sheet.getLastRowNum();
for (int i = 0; i <rowNum; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
}
/**
* 存入第一列
*/
@Test
public void cell1() throws IOException, SQLException {
TestDao test = new TestDao();
FileInputStream fileInputStream = new FileInputStream(path + "persion.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
//总列数
Row hssfRow = sheet.getRow(0);
int columnNum = hssfRow.getPhysicalNumberOfCells();
//总行数
int rowNum = sheet.getLastRowNum();
for (int i = 1; i < rowNum; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(0);
String value = cell.getStringCellValue();
test.cun(value,i);
System.out.println("存入成功!");
}
}
/**
* 将第一列存入数据库
*/
public void cun(String value,Integer id) throws SQLException {
Connection connection = null;
PreparedStatement pr = null;
connection = DBUtil.getConnection();
String sql = "insert into excel(id,name) values('"+id+"','"+value+"')";
pr = connection.prepareStatement(sql);
int count =pr.executeUpdate();
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/122868455
内容来源于网络,如有侵权,请联系作者删除!