All Downloads are FREE. Search and download functionalities are using the official Maven repository.

z11.jxl.JXLUtil Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package z11.jxl;

import z11.samplestructs.Account;
import z11.samplestructs.Pair;
import z11.F_ile;
import z11.S_tring;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class JXLUtil {
    
    public static Sheet getSheet(File file, int sheetNo) throws Exception {
        if ((file.exists()) && (file.length() > 0L)) {

            Workbook w = Workbook.getWorkbook(file);
            Sheet sheet = w.getSheet(sheetNo-1);
            return sheet;
        }
        throw new Exception();
    }
    
    public static ArrayList getSmss(String filePath, int sheetNumber) throws Exception {
        z11.L_ogger.print("__getSmss:" + filePath + " " + sheetNumber);
        Sheet iSheet = getSheet(new File(filePath), sheetNumber);
        ArrayList arrSms = new ArrayList<>();
        int ROW = iSheet.getRows();
        for (int i = 0; i < ROW; i++) {
            jxl.Cell cellSimNo = iSheet.getCell(0, i);
            String text = cellSimNo.getContents();
            if (text.length()==0) continue;
            arrSms.add(text);

        }
        z11.L_ogger.print("__getSmss: Return " + arrSms.size());
        return arrSms;
    }
    
    public static ArrayList getAccountSims(String filePath, int sheetNumber) throws Exception {
        z11.L_ogger.print("__getAccountSims:" + filePath + " " + sheetNumber);
        Sheet iSheet = getSheet(new File(filePath), sheetNumber);
        ArrayList simAccounts = new ArrayList<>();
        int ROW = iSheet.getRows();
        for (int i = 0; i < ROW; i++) {

            String simNo = iSheet.getCell(0, i).getContents().trim();
            String password = iSheet.getCell(1, i).getContents().trim();
            
            simNo = simNo.replace(" ", "");
            simNo = simNo.replace("-", "");
            simNo = simNo.replace(".", "");
            password = password.replace(" ", "");
            password = password.replace("-", "");
            password = password.replace(".", "");
            
            if (simNo.length()<5 || password.length()==0) continue;
            if (!S_tring.isNumeric(simNo)) continue;
            
            if (simNo.startsWith("0")){
                simNo = simNo.substring(1);
            } else if (simNo.startsWith("84")){
                simNo = simNo.substring(2);
            } else if (simNo.startsWith("+84")){
                simNo = simNo.substring(3);
            }
            
            simNo = "84" + simNo;
            
            if (simNo.length() != 11 && simNo.length() != 12) continue;
            
            simAccounts.add(new Account(simNo, password));
            
        }
        z11.L_ogger.print("__getAccountSims: Return " + simAccounts.size());
        return simAccounts;
    }

    public static ArrayList< Pair > getReceiveSimsContent(String filePath, int sheetNumber) throws Exception {
        z11.L_ogger.print("__getReceiveSims:" + filePath + " " + sheetNumber);
        Sheet iSheet = getSheet(new File(filePath), sheetNumber);
        ArrayList< Pair > receiveList = new ArrayList<>();
        int ROW = iSheet.getRows();
        for (int i = 0; i < ROW; i++) {

            String simNo = iSheet.getCell(0, i).getContents().trim();
            String content = iSheet.getCell(1, i).getContents().trim();
            
            simNo = simNo.replace(" ", "");
            simNo = simNo.replace("-", "");
            simNo = simNo.replace(".", "");
            
            if (simNo.length() < 5 || content.length() < 5) continue;
            
            if (!S_tring.isNumeric(simNo)) continue;
            
            if (simNo.startsWith("0")) {
                simNo = simNo.substring(1);
            } else if (simNo.startsWith("84")) {
                simNo = simNo.substring(2);
            } else if (simNo.startsWith("+84")) {
                simNo = simNo.substring(3);
            }
            
            simNo = "0" + simNo;
            
            if (simNo.length() != 10 && simNo.length() != 11) continue;
            
            //D_common.Logger.Logger.print("sim = " + simNo);
            
            receiveList.add(new Pair<>(simNo, content));   
        }
        z11.L_ogger.print("__getReceiveSims: Return " + receiveList.size());
        return receiveList;
    }
    
    
    public static ArrayList getReceiveSims(String filePath, int sheetNumber) throws Exception {
        z11.L_ogger.print("__getReceiveSims:" + filePath + " " + sheetNumber);
        Sheet iSheet = getSheet(new File(filePath), sheetNumber);
        ArrayList receiveList = new ArrayList<>();
        int ROW = iSheet.getRows();
        for (int i = 0; i < ROW; i++) {

            String simNo = iSheet.getCell(0, i).getContents().trim();
            
            simNo = simNo.replace(" ", "");
            simNo = simNo.replace("-", "");
            simNo = simNo.replace(".", "");
            
            if (simNo.length() < 5 ) continue;
            
            if (!S_tring.isNumeric(simNo)) continue;
            
            if (simNo.startsWith("0")) {
                simNo = simNo.substring(1);
            } else if (simNo.startsWith("84")) {
                simNo = simNo.substring(2);
            } else if (simNo.startsWith("+84")) {
                simNo = simNo.substring(3);
            }
            
            simNo = "0" + simNo;
            
            if (simNo.length() != 10 && simNo.length() != 11) continue;
            
            //D_common.Logger.Logger.print("sim = " + simNo);
            
            receiveList.add(simNo);
        }
        z11.L_ogger.print("__getReceiveSims: Return " + receiveList.size());
        return receiveList;
    }
    
    public static void appendToXls(String filePath, ArrayList> dataList) {
        try {
            WritableSheet sheet;
            WritableWorkbook workbook;
            if (!F_ile.checkFileExists(filePath)) {
                workbook = Workbook.createWorkbook(new File(filePath));
                sheet = workbook.createSheet("Ket qua", 0);
            } else {
                Workbook workbook1 = Workbook.getWorkbook(new File(filePath));
                workbook = Workbook.createWorkbook(new File(filePath), workbook1);
                sheet = workbook.getSheet(0);
            }
            final int ROW = sheet.getRows();
            for (Pair data : dataList) {
                int col = data.getFirst();
                String value = data.getSecond();
                Label label = new Label(col - 1, ROW, value);
                sheet.addCell(label);
            }

            workbook.write();
            workbook.close();
        } catch (IOException | BiffException | IndexOutOfBoundsException | WriteException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void main(String []arg) throws Exception{
    	ArrayList> list = new ArrayList<>();
    	list.add(new Pair(1, "11111"));
    	list.add(new Pair(2, "22222"));
    	list.add(new Pair(3, "33333"));
    	JXLUtil.appendToXls("C:\\Users\\vietduc\\Desktop\\out.xls", list);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy