IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Web en Java Discussion :

Formater un nombre en texte dans un fichier excel


Sujet :

Développement Web en Java

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur android & Java Web application
    Inscrit en
    Avril 2016
    Messages
    105
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur android & Java Web application
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2016
    Messages : 105
    Points : 59
    Points
    59
    Par défaut Formater un nombre en texte dans un fichier excel
    Bonjour la famille

    Je suis confronter à un problème
    lorsque je génère un fichier excel je force à ce que le zéro au début du nombre reste

    Mais mon problème est :

    Je veux garder le nombre en format texte lorsque je génère le fichier excel - csv

    Merci d'avance

  2. #2
    Membre actif
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2012
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 170
    Points : 234
    Points
    234
    Par défaut
    Bonjour,

    lorsque je génère un fichier excel je force à ce que le zéro au début du nombre reste
    Quel nombre? et quel zéro?
    Proposer un code est toujours plus simple qu’écrire des phrases incompréhensibles....

    Bref, tu peux utiliser cela:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    setCellValue(nombre.toString())
    Si jamais cela ne t'affiche pas le zéro alors tu peux concaténer avec un ou plusieurs zéros ...

    Cette réponse est aussi claire que la question.

    Bon courage.

  3. #3
    Membre du Club
    Homme Profil pro
    Développeur android & Java Web application
    Inscrit en
    Avril 2016
    Messages
    105
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur android & Java Web application
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2016
    Messages : 105
    Points : 59
    Points
    59
    Par défaut
    Bonjour

    le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
     
    Connection conn = null;
            Statement ss = null;
     
            String dateStar11 = request.getParameter("Usermail");
            String dateStar22 = request.getParameter("Password");
     
            String url = "http://localhost:6968/withdraw/list/b005fd07f0";
     
            try {
     
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lonaci_hub", "root", "jason");
                ss = conn.createStatement();
     
                PreparedStatement pst = conn.prepareStatement("Select * from code ORDER BY code DESC LIMIT 1");
     
                ResultSet rs = pst.executeQuery();
     
                if (rs.next()) {
     
                    Integer code = rs.getInt("code");
     
                    Date datee = new Date();
     
                    String DateJour = String.valueOf(datee);
     
                    SimpleDateFormat s = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
                    SimpleDateFormat ou = new SimpleDateFormat("ddMMyyyy");
                    SimpleDateFormat hh = new SimpleDateFormat("HHmmss");
     
                    Date d = s.parse(DateJour);
     
                    String formatJour = ou.format(d);
                    String formatHeure = hh.format(d);
     
                    // TODO Auto-generated method stub
                    System.out.println("************** doGet ************");
                    response.setContentType("text/csv; charset=UTF-8");
                    PrintWriter out = response.getWriter();
                    response.setDateHeader("Expires", 0);
     
                    StringBuffer fileNameFormat = new StringBuffer();
                    fileNameFormat.append("attachment; filename=Incription_vrac_employes_BulkPayee_" + formatJour + "_" + formatHeure);
                    fileNameFormat.append(".csv");
                    response.setHeader("Content-disposition", fileNameFormat.toString());
     
                    StringBuffer totalString = new StringBuffer();
                    StringBuffer header = new StringBuffer();
                    header.append("Mobile Number*");
                    header.append(";");
                    header.append("Unique Code*");
                    header.append(";");
                    header.append("User Type*");
     
                    StringBuffer body = new StringBuffer();
     
                    HttpClient client = HttpClientBuilder.create().build();
                    HttpPost post = new HttpPost(url);
     
                    //String USER_AGENT = "";
                    // add header
                    post.setHeader("User-Agent", "Mozilla/5.0");
     
                    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
     
                    //urlParameters.add(new BasicNameValuePair("from", ""));
                    //urlParameters.add(new BasicNameValuePair("to", dateStar22));
                    urlParameters.add(new BasicNameValuePair("status", "pending"));
                    post.setEntity(new UrlEncodedFormEntity(urlParameters));
     
                    HttpResponse respons = client.execute(post);
                    System.out.println("Response Code : " + respons.getStatusLine().getStatusCode());
     
                    BufferedReader rd = new BufferedReader(new InputStreamReader(respons.getEntity().getContent()));
     
                    String resu = "";
     
                    StringBuffer result = new StringBuffer();
                    String line = "";
                    while ((line = rd.readLine()) != null) {
                        System.out.println(result.append(line));
                        resu += line;
                    }
                    rd.close();
     
                    String weatherResult = resu;
     
                    System.out.println(weatherResult);
     
                    // read the json file
                    JSONParser jsonParser = new JSONParser();
     
                    JSONArray array = (JSONArray) jsonParser.parse(weatherResult);
     
                    Iterator i = array.iterator();
     
                    int ii = code + 1;
     
                    while (i.hasNext()) {
     
                        JSONObject innerObj = (JSONObject) i.next();
                        System.out.println("transaction_id " + innerObj.get("transaction_id"));
                        String transaction_id = (String) innerObj.get("transaction_id");
                        System.out.println("number " + innerObj.get("number"));
                        String number = (String) innerObj.get("number");
                        System.out.println("cashout_account_number " + innerObj.get("cashout_account_number"));
                        String cashout_account_number = (String) innerObj.get("cashout_account_number");
                        System.out.println("created_at " + innerObj.get("created_at"));
                        String created_at = (String) innerObj.get("created_at");
                        System.out.println("paid_currency_id " + innerObj.get("paid_currency_id"));
                        Long paid_currency_id = (Long) innerObj.get("paid_currency_id");
                        System.out.println("transaction_amount " + innerObj.get("transaction_amount"));
                        Double transaction_amount = (Double) innerObj.get("transaction_amount");
                        System.out.println("fees " + innerObj.get("fees"));
                        Double fees = (Double) innerObj.get("fees");
                        System.out.println("payment_status " + innerObj.get("payment_status"));
                        Boolean payment_status = (Boolean) innerObj.get("payment_status");
                        System.out.println("transaction_type " + innerObj.get("transaction_type"));
                        Long transaction_type = (Long) innerObj.get("transaction_type");
     
                        if (transaction_type == 2 && cashout_account_number != null) {
     
                            String sql = ("INSERT INTO code (code, date) VALUES ('" + ii + "', NOW())");
                            ss.execute(sql);
     
                            body.append("\"\t"+cashout_account_number+"\""); // c'est ca que je veux formatter en text avant de mettre dans le fichier excel
                            body.append(";");
                            body.append(ii++);
                            body.append(";");
                            body.append("Subscriber");
                            body.append("\n");
     
                        }
     
                    }
                    totalString.append(header.toString());
                    totalString.append("\n");
                    totalString.append(body.toString());
                    out.write(totalString.toString());
     
                }
     
            }

  4. #4
    Membre actif
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2012
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 170
    Points : 234
    Points
    234
    Par défaut
    Bonjour,

    Merci pour le code!
    il est malgré cela incomplet car je ne connais pas le type de cashout_account_number et d'ou vienne les zeros dont tu parles (est ce que c'est une histoire de scale sur BigDecimal ou autre chose....

    En tout cas je t'ai déjà répondu:
    Soit tu utilise la méthode toString()
    Si cela ne marche pas , tu concatène un zéro avec ta valeur.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String cashout_account_number = "0" +  (String) innerObj.get("cashout_account_number");
    Si tu as plusieurs zéros à mettre avant alors tu écris la fonction qui retourne la chaîne contenant le nombre de zéros nécessaires à concaténer avec le reste.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String cashout_account_number = fonction() +  (String) innerObj.get("cashout_account_number");


  5. #5
    Membre du Club
    Homme Profil pro
    Développeur android & Java Web application
    Inscrit en
    Avril 2016
    Messages
    105
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur android & Java Web application
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2016
    Messages : 105
    Points : 59
    Points
    59
    Par défaut
    est ce qui représente les numéros venant du json,
    avant je n'arrivait pas à garder le 0 au début du numéro ,

    Maintenant c'est OK,

    Mon soucis est de mettre la colonne du fichier excel généré en texte dans mon code ou les numéros sont insérés

  6. #6
    Membre actif
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2012
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 170
    Points : 234
    Points
    234
    Par défaut
    Bonjour,

    Je ne vois dans ton code que l’écriture dans un fichier csv. Je ne sais pas si tu souhaite le convertir par la suite...
    Pourrais tu être plus clair.

    Pour manipuler un fichier excel il faut te documenter sur POI API:
    https://poi.apache.org/apidocs/dev/o...FWorkbook.html

    tu auras un ensemble de méthodes qui te permettront de customiser ton fichier.
    Et comme je t'ai repondu dans ma premiere reponse il faut utiliser :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    setCellValue(cashout_account_number)
    étant donné que ta variable est de type String.

    Bon courage.

  7. #7
    Membre actif
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Septembre 2012
    Messages
    170
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Septembre 2012
    Messages : 170
    Points : 234
    Points
    234
    Par défaut
    Bonjour ,

    Tu peux aussi jetter un coup d'oeil ici:

    https://medium.com/asposecells/conve...a-a8552181da12

    Bon courage

Discussions similaires

  1. [WD17] Nombre de lignes dans un fichier excel
    Par lucienkany dans le forum WinDev
    Réponses: 7
    Dernier message: 03/09/2016, 12h12
  2. Réponses: 1
    Dernier message: 13/03/2015, 14h58
  3. importer plusieurs fichiers texte dans 1 fichier Excel
    Par dannie dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 11/10/2011, 21h39
  4. [Excel] Insérer du texte dans un fichier Excel
    Par Longrais dans le forum Langage
    Réponses: 3
    Dernier message: 15/02/2006, 17h46
  5. Réponses: 2
    Dernier message: 02/03/2004, 19h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo