java读取Excel表格数据

标准格式的Excel表格数据读取是我们开发中或多或少遇到的一个问题,例如批量导入某商品信息、批量导入用户信息等等。

例如:

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();
		}




猜你喜欢

转载自blog.csdn.net/u014505277/article/details/51982278
今日推荐