IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Voir le flux RSS

joel.drigo

Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation

Noter ce billet
par , 14/11/2016 à 17h55 (1301 Affichages)
Comme j'ai un peu galéré pour trouver comment mettre plusieurs lignes dans le prompt d'un validateur Excel en xlsx, l'API ne le gérant pas directement (ça sent le bogue), je vous partage le truc que j'ai trouvé en décortiquant un xlsx pour le faire : il suffit de remplacer le caractère \n dans la chaîne par _x000a_.

J'ai analysé les différents whitespaces (du moins les classiques sauf l'espace (\n, \n, \t, \b, \f)) et fait une petite méthode de conversion :
Code java : 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
public class Utils {
 
	public static String escapeWhitespaces(String string) { 
 
		final StringBuilder sb = new StringBuilder(string.length());
		for(int i=0; i<string.length(); i++) {
			char c = string.charAt(i);
			if( c!=' ' && Character.isWhitespace(c) ) {
				switch(c) { 
				case '\f': // ff render a female sign if escaped or make doc corrupted if not
				case '\b': // backspace make doc corrupted (escaped or not)
					break;
				case '\t': // render a single space (not escaped) or nothing (escaped) 
					sb.append("    ");
					break; 
				case '\r': // cr render nothing if escaped or space if escaped 
					if ( !equals(string,i+1,'\n') ) { // (except if followed with \n)
						sb.append(' '); // replace with a char
					}
					break;
				case '\n': // lf
					sb.append(String.format("_x%04x_",(int)c));
					break;
				default:
					// ignore others
				}
			}
			else {
				sb.append(c);
			}
		} 
		return sb.toString();
	}
 
	private static boolean equals(String string, int i, char c) {
		if ( i<string.length() ) {
			return c==string.charAt(i);
		}
		return false;
	}
 
}

Exemple d'appel (une validation avec combo de choix):
Code java : 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
 
final CellRangeAddressList regions = new CellRangeAddressList(firstRowNum, lastRowNum, colNum, colNum);
final DataValidation dataValidation;
if( sheet instanceof HSSFSheet ) {
	final DVConstraint constraint = DVConstraint.createExplicitListConstraint(values);
	dataValidation = new HSSFDataValidation(regions, constraint);
}
else if ( sheet instanceof XSSFSheet ) {
	final XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) sheet);
	final XSSFDataValidationConstraint constraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(values);
	dataValidation = (XSSFDataValidation)dvHelper.createValidation(constraint, regions);
}
else {
	throw new IllegalStateException();
}
if ( prompt!=null ) {
 
	final String message;
	if ( sheet instanceof XSSFSheet ) {
		message = Utils.escapeWhitespaces(prompt.message);
	}
	else {
		message = prompt.message;
	}
	dataValidation.createPromptBox(prompt.title, message);
	dataValidation.setShowPromptBox(true); 
 
}
sheet.addValidationData(dataValidation);

Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Viadeo Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Twitter Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Google Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Facebook Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Digg Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Delicious Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog MySpace Envoyer le billet « Tip Apache POI Excel XLSX : avoir plusieurs lignes dans le prompt d'un DataValidation » dans le blog Yahoo

Commentaires

  1. Avatar de joel.drigo
    • |
    • permalink
    Le bug sera corrigé dans une prochaine version. Cf https://bz.apache.org/bugzilla/show_bug.cgi?id=60370