使用java jxl读取excel中的内容,并生成json文件。

比较中间的汉字

package demo;

public class CharUtil {
    // 完整的判断中文汉字和符号

    public static boolean isChinese(String strName) {

        char[] ch = strName.toCharArray();

        for (int i = 0; i < ch.length; i++) {

            char c = ch[i];

            if (isChinese(c)) {

                return true;

            }

        }

        return false;

    }
    // 根据Unicode编码完美的判断中文汉字和符号

    private static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {

            return true;

        }

        return false;

    }

}

实体类

package demo;

import java.util.Date;

public class TimeZoneBean {
    private String cityName;
    private String countryName;
    private String timeZone;// 时区 GMT +8等等
    private String startTime;
    private String endTime;
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    public String getTimeZone() {
        return timeZone;
    }
    public void setTimeZone(String timeZone) {
        this.timeZone = timeZone;
    }
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }


}

获取需要的数据

package demo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.Gson;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class Test {
    private static String regex = "[\u4e00-\u9fa5]";

    static String getOrderNumber(String ordernumber1) {
        Pattern pat = Pattern.compile(regex);
        Matcher mat = pat.matcher(ordernumber1);
        String repickStr = mat.replaceAll("");
        repickStr = repickStr.replace("(", "");
        repickStr = repickStr.replace(")", "");
        repickStr = repickStr.replace("(", "");
        repickStr = repickStr.replace(")", "");
        repickStr = repickStr.replace("UTC/GMT ", "");
        return repickStr;
    }

    public static void main(String[] args) throws Exception {
        // 1:创建workbook
        Workbook workbook = Workbook.getWorkbook(new File("D:\\ttime.xls"));
        // 2:获取第一个工作表sheet
        Sheet sheet = workbook.getSheet(0);

        List<TimeZoneBean> timeZoneBeans = new ArrayList<>();// 时区的

        // 3:获取数据
        System.out.println("行:" + sheet.getRows());
        System.out.println("列:" + sheet.getColumns());
        for (int i = 0; i < sheet.getRows(); i++) {
            TimeZoneBean timeZoneBean = new TimeZoneBean();
            for (int j = 0; j < sheet.getColumns(); j++) {
                Cell cell = sheet.getCell(j, i);
                String string = cell.getContents();
                if (j == 4) {
                    string = getOrderNumber(string);
                    // 去除中文,提取纯时区
                    System.out.println(string);
                }
                switch (j) {
                case 0:
                    timeZoneBean.setCityName(string);
                    break;
                case 2:
                    timeZoneBean.setCountryName(string);
                    break;
                case 4:
                    timeZoneBean.setTimeZone(string);
                    break;
                case 6:
                    timeZoneBean.setStartTime(string);
                    break;
                case 7:
                    timeZoneBean.setEndTime(string);

                    break;
                }

            }

            timeZoneBeans.add(timeZoneBean);
        }
        // 最后一步:关闭资源
        workbook.close();
        Gson gson = new Gson();
        String time = gson.toJson(timeZoneBeans);
        writeFile(time, "D:\\ttime.json");





        // System.out.println(time);

    }

    public static void writeFile(String content, String FilePath) {

        try {
            // String data = " This content will append to the end of the file";
            File file = new File(FilePath);
            // if file doesnt exists, then create it

            if (!file.exists()) {
                file.createNewFile();
            } else {
                file.delete();
                file.createNewFile();

            }
            FileWriter fw = new FileWriter(file);

            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")));
            out.write((content));
            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

猜你喜欢

转载自blog.csdn.net/qq_35674951/article/details/81285127