T
T
tr1ck12020-06-16 17:47:03
Java
tr1ck1, 2020-06-16 17:47:03

Why doesn't replace remove spaces in java (groovy)?

Hello everyone, why does replace replace dots with commas, whatever, but does not remove spaces? The problem is that in excel there is a group separator and the number is read as 3 535 174.10, but it needs to be read as 3535174.10 Does
anyone know and can practically point out the error? I can't figure it out for a few days now.
ps please do not advise to change the locale.
p.s.s. and even better, how to count values ​​without these spaces, so as not to use a replay? so I understand that you need to replace the method to read the number inside the cell, and not superficially? value = formatter.formatCellValue(mycell);

import groovy.sql.Sql
import java.sql.Connection
import java.sql.DriverManager

import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*

import java.text.DecimalFormat

class XM_PARSE_XLS {

    def execute(Connection conn, InputStream p_file, String p_filename) {

        Sql sql = new Sql(conn);

        HSSFWorkbook wb = new HSSFWorkbook(p_file);

        ArrayList<HashMap<String, String>> arrAllData = new ArrayList<HashMap<String, String>>();

        String strsql
        Integer cntStr = 0
        String els_list = ""
        String st_code_list = ""
        String rep_date_list = ""

        // идем по листам в файле
        wb.each { HSSFSheet myExcelSheet ->
            DataFormatter formatter = new DataFormatter();

            // идем по страницам файла
            myExcelSheet.each{  Row myrow ->

                HashMap<String, String> hm = new HashMap<String, String>();

                if (cntStr >= 0) {
                    // идем по строкам с данными
                    Integer numCell = 0;
                    String typ = '';

                    myrow.each { Cell mycell ->
                        String value = ""; // приводит любые ячейки к строковому формату

                        // если тип Строка
                        if (mycell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            value = mycell.getStringCellValue();
                            typ = 'S'
                            // если тип число
                        }  else {
                            // а для числовых ячеек или ячеек даты значение будет отформатировано на основе правил форматирования / стиля, примененных к ячейке, а затем возвращено как строка
                            value = formatter.formatCellValue(mycell);
                            value = value.replace(' ',''); //убрать пробелы, тут же ставил replace (',','.') и заменял запятую на точку, а пробелы игнорирует
                            typ = 'N'
                        }
                        if (cntStr == 0) {
                            Boolean Err = false;
                            switch(numCell) {
                                case 0:
                                    Err=(value.toUpperCase()!='ELS');
                                    break;
                                case 1:
                                    Err=(value.toUpperCase()!='ST_CODE');
                                    break;
                                case 2:
                                    Err=(value.toUpperCase()!='SUMMA');
                                    break;
                                case 3:
                                    Err=(value.toUpperCase()!='REP_DATE');
                                    break;
                            }
                            numCell = numCell + 1;
                            if (Err) {
                                throw new Exception("Неверный заголовок столбца "+ value)
                            }
                        } else {
                            if( (typ!='N') && (numCell>0)&&(numCell<3) ){
                                throw new Exception("Неверные данные для загрузки "+ value)
                            }
                            switch(numCell) {
                                case 0:
                                    hm.put("els", value);
                                    break;
                                case 1:
                                    hm.put("st_code", value);
                                    break;
                                case 2:
                                    hm.put("summa", value); //здесь не убирает пробелы
                                    break;
                                case 3:
                                    hm.put("rep_date", value);
                                    break;
                            }
                            numCell = numCell + 1;

                        }

                    }
                }
                if(hm) arrAllData.add(hm);
                cntStr = cntStr + 1;
            }
        }

        for (int i = 0; i < arrAllData.size(); i++) {
            HashMap <String, String> hm = arrAllData.get(i);

            if ("".equals(els_list)) {
                els_list = hm?.els}
            else{
                els_list = els_list+";"+hm?.els}

            if ("".equals(st_code_list)) {
                st_code_list = hm?.st_code}
            else{
                st_code_list = st_code_list+";"+hm?.st_code}

            if ("".equals(rep_date_list)) {
                rep_date_list = hm?.rep_date}
            else{
                rep_date_list = rep_date_list+";"+hm?.rep_date}

            strsql = """pl/sql code
""";
            sql.execute( strsql);
        }

        HashMap<String, Object> res = new HashMap<>();
        res.put("P_CNT",cntStr-1);
        res.put("P_SQL",strsql);
        res.put("P_ELS_LIST",els_list);
        res.put("P_ST_CODE_LIST",st_code_list);
        res.put("P_REP_DATE_LIST",rep_date_list);
        return res;
    }

    static void main(String... args) {
        Class.forName("oracle.jdbc.driver.OracleDriver")
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@ip:port/OLAP2", "name", "password")
        connection.setAutoCommit(true)

        try {
            def file = new File("201408_teo.xls").newInputStream()
            def SSC = new XM_PARSE_XLS()
            def res = SSC.execute(connection,file,"201408_teo.xls")

        } finally {
            connection.close()
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayton2019, 2020-06-16
@mayton2019

Is that Groovy? How did it even compile? What's on the right? Character or string?
value = value.replace(' ','');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question