java 解析,读取excel

来源:互联网 发布:开淘宝店能赚钱吗 编辑:程序博客网 时间:2024/06/06 04:42
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ImprotExcelService {/*** 读取xlsx文件内容* * @return* * @return List 对象* @throws IOException*             输入/输出(i/o)异常List* **/public void  Improtxlsx(File file) throws IOException {FileInputStream input = new FileInputStream(file);XSSFWorkbook xssfWorkbook = new XSSFWorkbook(input);ArrayList<String[]> paramList = new ArrayList<String[]>();// 循环工作表Sheetfor (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;}// 获取指定行,索引从0开始XSSFRow xssfRows = xssfSheet.getRow(0);// 获取总列数int columnNum = xssfRows.getLastCellNum();// 循环行Rowfor (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {XSSFRow xssfRow = xssfSheet.getRow(rowNum);if (xssfRow == null) {continue;}// _idXSSFCell idcell = xssfRow.getCell(0);                                if (flagcell != null) {                                  String id = getValue(idcell);                                }// 操作符XSSFCell flagcell = xssfRow.getCell(1);if (flagcell != null) {String flag = getValue(flagcell);}                            paramList.add(new String[] { id, flag });}}int size = paramList == null ? 0 : paramList.size();for (int i = 0; i < size; i++) {String[] itemParam = paramList.get(i);for (int j = 0; j < itemParam.length; j++) {System.out.println(j + 1 + "--" + itemParam[j]);}}}/*** 得到Excel表中的值* * @param hssfCell*            Excel中的每一个单元格* @return Excel中每一个单元格中的值*/// @SuppressWarnings("static-access")private String getValue(XSSFCell xssfCell) {if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {// 返回布尔类型的值return String.valueOf(xssfCell.getBooleanCellValue());} else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {// 返回数值类型的值return String.valueOf(xssfCell.getNumericCellValue());} else {// 返回字符串类型的值return String.valueOf(xssfCell.getStringCellValue());}}}需要jar包:poi-3.8-20120326.jarpoi-ooxml-3.8-20120326.jardom4j-1.6.1.jarpoi-ooxml-schemas-3.8-20120326.jarxmlbeans-2.3.0.jar