java读取Excel表格数据
2016-07-21 14:32
525 查看
标准格式的Excel表格数据读取是我们开发中或多或少遇到的一个问题,例如批量导入某商品信息、批量导入用户信息等等。
例如:
接下来我们简单的来开下该如何获取数据并且去除空格换行符号:
1、导入用于读取Excel表格的poi jar包,如果是maven工程的话则在pom.xml中添加
<span style="white-space:pre"> </span><!-- 读取excel文件jar -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>2、读取Excel代码如下
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.util.regex.Matcher;
import java.util.regex.Pattern;
String tempSavePath = "C:\\Users\\Administrator\\Downloads\\酒店.xlsx"; // 文件地址
/* 读取excel数据 */
boolean isE2007 = false; // 判断是否是excel2007格式
if (tempSavePath.endsWith("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream(tempSavePath); // 建立输入流
Workbook wb = null;
// 根据文件格式(2003或者2007)来初始化
if (isE2007)
wb = new XSSFWorkbook(input);
else
wb = new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0); // 获得第一个表单
Iterator<Row> rows = sheet.rowIterator(); // 获得第一个表单的迭代器
while (rows.hasNext()) {
Row row = rows.next(); // 获得行数据
// TODO 获得行号从0开始 row.getRowNum();
Iterator<Cell> cells = row.cellIterator(); // 获得第一行的迭代器
while (cells.hasNext()) {
Cell cell = cells.next();
// System.out.println("Cell #" + cell.getColumnIndex()); // 列号,从0开始。
int line = row.getRowNum()+1;
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//System.out.println(cell.getStringCellValue());
if (row.getRowNum() != 0) { // 跳过第一行属性名获取第二行数据
if (cell.getColumnIndex() == 0) { // 第一列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
} // 用于去除回车符、制表符、空格
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 1) { // 第二列
//u.setUmauRealname(cell.getStringCellValue());
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 2) { // 第三列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 3) { // 第四列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
}
}
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){ // 内容
if (row.getRowNum() != 0) {
if (cell.getColumnIndex() == 0) { // 第一列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 1) { // 第二列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 2) { // 第三列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 3) { // 第四列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
}
}
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
System.out.println(cell.getBooleanCellValue());
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
System.out.println(cell.getCellFormula());
}else{
System.out.println("unsuported sell type");
}
}
System.out.println();
/*if(u.getUmauUsername() != null&& !"".equals(u.getUmauUsername())){
usersAddList.add(u); // 向list中添加数据
}*/
}
} catch (IOException ex) {
ex.printStackTrace();
}
例如:
接下来我们简单的来开下该如何获取数据并且去除空格换行符号:
1、导入用于读取Excel表格的poi jar包,如果是maven工程的话则在pom.xml中添加
<span style="white-space:pre"> </span><!-- 读取excel文件jar -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>2、读取Excel代码如下
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.util.regex.Matcher;
import java.util.regex.Pattern;
String tempSavePath = "C:\\Users\\Administrator\\Downloads\\酒店.xlsx"; // 文件地址
/* 读取excel数据 */
boolean isE2007 = false; // 判断是否是excel2007格式
if (tempSavePath.endsWith("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream(tempSavePath); // 建立输入流
Workbook wb = null;
// 根据文件格式(2003或者2007)来初始化
if (isE2007)
wb = new XSSFWorkbook(input);
else
wb = new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0); // 获得第一个表单
Iterator<Row> rows = sheet.rowIterator(); // 获得第一个表单的迭代器
while (rows.hasNext()) {
Row row = rows.next(); // 获得行数据
// TODO 获得行号从0开始 row.getRowNum();
Iterator<Cell> cells = row.cellIterator(); // 获得第一行的迭代器
while (cells.hasNext()) {
Cell cell = cells.next();
// System.out.println("Cell #" + cell.getColumnIndex()); // 列号,从0开始。
int line = row.getRowNum()+1;
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//System.out.println(cell.getStringCellValue());
if (row.getRowNum() != 0) { // 跳过第一行属性名获取第二行数据
if (cell.getColumnIndex() == 0) { // 第一列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
} // 用于去除回车符、制表符、空格
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 1) { // 第二列
//u.setUmauRealname(cell.getStringCellValue());
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 2) { // 第三列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 3) { // 第四列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
}
}
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){ // 内容
if (row.getRowNum() != 0) {
if (cell.getColumnIndex() == 0) { // 第一列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 1) { // 第二列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 2) { // 第三列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
} else if (cell.getColumnIndex() == 3) { // 第四列
String value= cell.getStringCellValue();
if (value!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(value);
value = m.replaceAll("");
}
System.out.print(value+" ");
}
}
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
System.out.println(cell.getBooleanCellValue());
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
System.out.println(cell.getCellFormula());
}else{
System.out.println("unsuported sell type");
}
}
System.out.println();
/*if(u.getUmauUsername() != null&& !"".equals(u.getUmauUsername())){
usersAddList.add(u); // 向list中添加数据
}*/
}
} catch (IOException ex) {
ex.printStackTrace();
}
相关文章推荐
- Exception in thread “main” com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
- Springmvc 4.x利用@ResponseBody返回Json数据
- javaEE:day6-requset和response用法、表单参数的接受、文件手动上传(简易版)
- Java进程CPU使用率高排查
- Sublime Text3 解决编辑java保存后在cmd下运行中文显示乱码的问题
- Struts2简单例子
- Java JVM:编译加载机制与自定义类加载器
- Struts2 多种方式接收表单文件
- 使用qrcode生成二维码
- Java 内部类的一些总结
- HashCode方法介绍
- java Lambda exp Aggregate聚合总体 Operations Reduction
- spring事务_注解
- java字符流
- 25个Java机器学习工具和库
- struts2 hello world
- RSA加密算法及其与SpringMVC集成
- Spring任务调度器之Task的使用
- 个人学习-java-结构模式(23种设计模式)
- Java File操作基础