/** * 读Excel * * @param pathname */ public static LinkedList<HashMap<String, String>> readExcel(String pathname) { LinkedList<HashMap<String, String>> list = new LinkedList<HashMap<String, String>>(); File file = new File(pathname); String uri = "f:\\xml\\map\\edge.xml"; Workbook wb = null; try { wb = Workbook.getWorkbook(file); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); // ThreadPoolExecutorTest threadPool = new // ThreadPoolExecutorTest(queue); for (int i = 0; i < rows; i++) { HashMap<String, String> map = new HashMap<String, String>(); // System.out.println("第"+(i+1)+"条数据正在执行"); Cell[] cols = sheet.getRow(i); String sender = cols[0].getContents(); String nodeID = cols[1].getContents(); String wayID = cols[2].getContents(); String depart = cols[3].getContents(); String edgeID = SaxService.getWayIdFromNodeXML(uri, nodeID, wayID); map.put("sender", sender); map.put("nodeID", nodeID); map.put("wayID", wayID); map.put("edgeID", edgeID); map.put("depart", depart); list.add(map); } } catch (Exception e) { e.printStackTrace(); } return list; }
public static void writeExcel( String fileName, Map<String, List<Map<String, String>>> senderMap, HashSet<String> nameSet) { List<Map<String, String>> list = null; System.out.println("当前文件包含:" + nameSet.size() + "辆车。"); for (String name : nameSet) { list = senderMap.get(name); int index = 0; int rows = list.size(); int cols = list.get(0).size(); Workbook wb = null; WritableWorkbook wwb = null; boolean flag = false; try { File is = new File(fileName + "\\" + name + ".xls"); if (!is.exists()) { wwb = Workbook.createWorkbook(is); flag = true; } else { wb = Workbook.getWorkbook(is); Sheet sheet = wb.getSheet(0); // 获取行 int length = sheet.getRows(); if (length >= MAXROWS) { index++; flag = true; } wwb = Workbook.createWorkbook(is, wb); // 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 // wwb = Workbook.createWorkbook(new File(fileName)); } } catch (Exception e) { e.printStackTrace(); } if (wwb != null) { // 创建一个可写入的工作表 // Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置 // System.out.println(index); WritableSheet ws = null; if (flag) { ws = wwb.createSheet("vehicle", 0); } else ws = wwb.getSheet(0); // 下面开始添加单元格 for (int i = 0; i < rows; i++) { Vehicle vh = SaxService.setVehicle(list.get(i)); String sender = vh.getSender(); String longitude = vh.getLongitude(); String latitude = vh.getLatitude(); String dateTime = vh.getDateTime(); String seconds = vh.getSeconds(); List<String> items = new ArrayList<String>(); items.add(sender); items.add(longitude); items.add(latitude); items.add(dateTime); items.add(seconds); for (int j = 0; j < cols; j++) { write2Cell(ws, j, i, items.get(j)); } } // index += rows; try { // 从内存中写入文件中 wwb.write(); // 关闭资源,释放内存 wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } }