java 通过poi 读取Excel 写入sqlser mysql
2017-08-01 11:06
621 查看
1. 引用pom
<!--读取excl-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
2. 写函数
读取的Excel
目标表格
需要注意数字列的读法
private void importExcel() {
try {
FileInputStream in = new FileInputStream("F:\\country.xlsx");//Excel路径
//Workbook wb = new HSSFWorkbook(in);//Excel版本<=Office 2007
Workbook wb = new XSSFWorkbook(in);//Excel版本Office> 2007
Sheet sheet = wb.getSheetAt(1);//sheet页码, 从0开始,1表示第二个sheet
for (Row row : sheet) {
if (row.getRowNum() < 1) {//第一行通常为表头,不读
continue;
}
CountryExchangeRate eInfo = new CountryExchangeRate();
Cell numCell =row.getCell(0);//如果是数字,1读出来是1.0,不能Integer.parseInt
//数字列处理的第一种方法,推荐
if(numCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
String str = NumberToTextConverter.toText(numCell.getNumericCellValue());
eInfo.setCountryCode(Integer.parseInt(str));
}
//数字列处理的第二种方法,不推荐
//numCell.setCellType(Cell.CELL_TYPE_STRING);//regardless of how the user formatted the cell
//String test=row.getCell(0).toString();//1
//eInfo.setCountryCode(Integer.parseInt(test));
eInfo.setCountryName(row.getCell(1).getStringCellValue());
eInfo.setCurrency(row.getCell(2).getStringCellValue());
eInfo.setCurrencyName(row.getCell(3).getStringCellValue());
eInfo.setExchange(BigDecimal.ZERO);
eInfo.setDatachangeLasttime(new Timestamp(System.currentTimeMillis()));
eInfo.setDatachangeCreatetime(new Timestamp(System.currentTimeMillis()));
countryExchangeRateDao.insert(eInfo);//插入数据到db
}
in.close();
} catch (Exception ex) {
logger.warn("importExcel Exception", 0, ex);
}
}
<!--读取excl-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
2. 写函数
读取的Excel
目标表格
需要注意数字列的读法
private void importExcel() {
try {
FileInputStream in = new FileInputStream("F:\\country.xlsx");//Excel路径
//Workbook wb = new HSSFWorkbook(in);//Excel版本<=Office 2007
Workbook wb = new XSSFWorkbook(in);//Excel版本Office> 2007
Sheet sheet = wb.getSheetAt(1);//sheet页码, 从0开始,1表示第二个sheet
for (Row row : sheet) {
if (row.getRowNum() < 1) {//第一行通常为表头,不读
continue;
}
CountryExchangeRate eInfo = new CountryExchangeRate();
Cell numCell =row.getCell(0);//如果是数字,1读出来是1.0,不能Integer.parseInt
//数字列处理的第一种方法,推荐
if(numCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
String str = NumberToTextConverter.toText(numCell.getNumericCellValue());
eInfo.setCountryCode(Integer.parseInt(str));
}
//数字列处理的第二种方法,不推荐
//numCell.setCellType(Cell.CELL_TYPE_STRING);//regardless of how the user formatted the cell
//String test=row.getCell(0).toString();//1
//eInfo.setCountryCode(Integer.parseInt(test));
eInfo.setCountryName(row.getCell(1).getStringCellValue());
eInfo.setCurrency(row.getCell(2).getStringCellValue());
eInfo.setCurrencyName(row.getCell(3).getStringCellValue());
eInfo.setExchange(BigDecimal.ZERO);
eInfo.setDatachangeLasttime(new Timestamp(System.currentTimeMillis()));
eInfo.setDatachangeCreatetime(new Timestamp(System.currentTimeMillis()));
countryExchangeRateDao.insert(eInfo);//插入数据到db
}
in.close();
} catch (Exception ex) {
logger.warn("importExcel Exception", 0, ex);
}
}
相关文章推荐
- JAVA:通过poi读取excel
- java的poi技术读取Excel数据到MySQL
- JAVA入门之如何通过 POI 读取并修改 Excel(转载)
- [Java]通过Poi包读取Excel表格
- Java模块 -- 读取Excel文件写入数据库 Mybatis , POI , JXL
- java的poi技术读取Excel数据到MySQL
- java的poi技术读取Excel数据到MySQL
- Java的poi技术一-------------读取excel到MySql
- java的poi技术读取Excel数据到MySQL
- java SpringMVC中 POI读取数据库数据并写入Excel表格中,并实现下载功能
- Java通过poi写入和读取Excel文件
- Poi读取excel 并写入mysql
- Java通过poi组件usermodel读取Excel
- java通过poi读取excel中的日期类型数据或自定义类型日期
- java的poi技术读取Excel数据到MySQL
- java的poi技术读取Excel数据到MySQL
- java的poi技术读取Excel数据到MySQL
- java通过poi解析Excel文件,直接读取客户端文件
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片