2021.11.7 读取文件生成数据字典
一、今日学习内容
数据分析平台自动读取文件生成数据字典
关键代码:
public List<Data1> poi2(String path){ List<Data1> list=new ArrayList<>(); try { FileInputStream fis = new FileInputStream(path); // String city; Data1 data1=null; XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheetAt(0); int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); Row firstRow = sheet.getRow(firstRowNum); int firstCellNum = firstRow.getFirstCellNum(); int lastCellNum = firstRow.getLastCellNum(); Row row= sheet.getRow(3); String content1=""; for(int i = 0; i < lastCellNum; i++) { //sheet.getRow(3).getCell(i).setCellType(Cell.CELL_TYPE_STRING); //sheet.getRow(3).getCell(i).setCellType(CellType.STRING_FORMULA); Cell cell= row.getCell(i); int celltype=cell.getCellType(); switch (celltype){ case HSSFCell.CELL_TYPE_STRING: // 字符串 content1=sheet.getRow(3).getCell(i).getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: // 数字 content1 =String.valueOf(sheet.getRow(3).getCell(i).getNumericCellValue()); break; } String city=sheet.getRow(2).getCell(i).getStringCellValue(); String danwei=sheet.getRow(1).getCell(i).getStringCellValue(); //System.out.print(sheet.getRow(0).getCell(j).getNumericCellValue() + " "); String user=sheet.getRow(0).getCell(i).getStringCellValue(); //String data=sheet.getRow(3).getCell(i).getStringCellValue(); data1=new Data1(user,city,danwei,content1); list.add(data1); //System.out.println(list.get(i)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; }