Java tutorial
/* * IMEI.java * Created on 2011-2-27; Project to Colt2010; $Id: IMEIMerge.java 309 2013-04-25 16:38:44Z tristan $ * * Copyright (c) 2011, Xu Brothers and/or its affiliates. All rights reserved. * Xu Brothers PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.waku.mmdataextract; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; 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 org.dom4j.Element; import org.dom4j.io.SAXReader; /** * @versin $Rev: 309 $, $Date: 2013-04-26 00:38:44 +0800 (, 26 2013) $ * @author Jin */ public class IMEIMerge { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("IMEI"); int colIndex = 0; int rowIndex = 0; Row row = sheet.createRow(rowIndex++); row.createCell(colIndex++).setCellValue(""); row.createCell(colIndex++).setCellValue("?"); row.createCell(colIndex++).setCellValue("?"); row.createCell(colIndex++).setCellValue("IMEI"); int i = 0; InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("Brand.xml"); for (Element brand : (List<Element>) new SAXReader().read(in).selectNodes("/brand/option")) { String brandName = brand.getText(); System.out.println(brandName); File file = new File("output/" + brandName + ".csv"); if (file.exists()) { System.out.println("Found file ->" + file.getAbsolutePath()); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; boolean noDataAvailable = true; while ((line = br.readLine()) != null) { if (line.indexOf(",") != -1) { rowIndex = addRow(line.split(","), sheet, rowIndex); noDataAvailable = false; } } if (noDataAvailable) { System.out.println("No data for " + brandName); rowIndex = addRow(new String[] { "", brandName, "N/A", "N/A" }, sheet, rowIndex); } br.close(); } else { System.out.println("No file for " + brandName); rowIndex = addRow(new String[] { "", brandName, "N/A", "N/A" }, sheet, rowIndex); } System.out.println(i++); } System.out.println(i); System.out.println(" ---------------------------------- File saved!"); wb.write(new FileOutputStream(new File("IMEI.xls"))); } private static int addRow(String[] line, Sheet sheet, int rowIndex) { System.out.println(line[0] + "," + line[1] + "," + line[2] + "," + line[3]); int colIndex = 0; Row row = sheet.createRow(rowIndex++); row.createCell(colIndex++).setCellValue(line[0]); row.createCell(colIndex++).setCellValue(line[1]); row.createCell(colIndex++).setCellValue(line[2]); row.createCell(colIndex++).setCellValue(line[3]); return rowIndex; } }