HSSF changeant l'épaisseur de la colonne

Je travaille sur un projet qui écrit sur un file excel. J'utilise le POI HSSF. Mon code fonctionne bien en ce qui concerne l'écriture sur le file Excel, mais j'ai un problème concernant le formatting. Je veux faire de chaque colonne une largeur spécifique. J'ai fait la search pour cela et je suis certain que je suis exactement ce qui a été décrit mais la cellule ne change pas au moment de l'exécution. Voici mon code:

Classe d'essai

package mainClass; import parsing.WriteExcel; public class TestClass { public static void main(Ssortingng args[]){ WriteExcel wExcel = new WriteExcel(); wExcel.writeExcel(); } } 

Classe d'écriture

 package parsing; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; import javafx.scene.control.Cell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; public class WriteExcel { private static final Ssortingng FILE_PATH = "C:\\Users\\jhamric\\Documents\\PainDifferences.xls"; public void writeExcel(){ ArrayList<Ssortingng> sample1 = new ArrayList<Ssortingng>(); ArrayList<Ssortingng> sample2 = new ArrayList<Ssortingng>(); sample1.add("1"); sample1.add("2"); sample2.add("a"); sample2.add("b"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Unmatched Transactions"); sheet.setColumnWidth(1, 25); HSSFFont fontBold = workbook.createFont(); fontBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle styleBold = workbook.createCellStyle(); styleBold.setFont(fontBold); Map<Ssortingng, Object[]> data = new HashMap<Ssortingng, Object[]>(); data.put("0",new Object[]{"Number","Letter"}); //for(int i = 0; i < ParseFiles.InstrId.size(); i++){ for(int i = 0; i < sample1.size(); i++){ Ssortingng currentISsortingng = Integer.toSsortingng(i+1); data.put(currentISsortingng, new Object[]{sample1.get(i),sample2.get(i)}); //data.put(currentISsortingng, new Object[]{ParseFiles.InstrId.get(i)}); } Set<Ssortingng> keyset = data.keySet(); int rownum = 0; for(Ssortingng key: keyset){ Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0; for(Object obj : objArr){ org.apache.poi.ss.usermodel.Cell cell = row.createCell(cellnum++); if(cell.toSsortingng().equals("Number")){ cell.setCellStyle(styleBold); } cell.setCellValue((Ssortingng)obj); } } try{ FileOutputStream out = new FileOutputStream(new File(FILE_PATH)); workbook.write(out); out.close(); workbook.close(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } } importer java.io.IOException; package parsing; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; import javafx.scene.control.Cell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; public class WriteExcel { private static final Ssortingng FILE_PATH = "C:\\Users\\jhamric\\Documents\\PainDifferences.xls"; public void writeExcel(){ ArrayList<Ssortingng> sample1 = new ArrayList<Ssortingng>(); ArrayList<Ssortingng> sample2 = new ArrayList<Ssortingng>(); sample1.add("1"); sample1.add("2"); sample2.add("a"); sample2.add("b"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Unmatched Transactions"); sheet.setColumnWidth(1, 25); HSSFFont fontBold = workbook.createFont(); fontBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle styleBold = workbook.createCellStyle(); styleBold.setFont(fontBold); Map<Ssortingng, Object[]> data = new HashMap<Ssortingng, Object[]>(); data.put("0",new Object[]{"Number","Letter"}); //for(int i = 0; i < ParseFiles.InstrId.size(); i++){ for(int i = 0; i < sample1.size(); i++){ Ssortingng currentISsortingng = Integer.toSsortingng(i+1); data.put(currentISsortingng, new Object[]{sample1.get(i),sample2.get(i)}); //data.put(currentISsortingng, new Object[]{ParseFiles.InstrId.get(i)}); } Set<Ssortingng> keyset = data.keySet(); int rownum = 0; for(Ssortingng key: keyset){ Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0; for(Object obj : objArr){ org.apache.poi.ss.usermodel.Cell cell = row.createCell(cellnum++); if(cell.toSsortingng().equals("Number")){ cell.setCellStyle(styleBold); } cell.setCellValue((Ssortingng)obj); } } try{ FileOutputStream out = new FileOutputStream(new File(FILE_PATH)); workbook.write(out); out.close(); workbook.close(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } } 

Plus précisément, je déclare le code pour définir ici la largeur de la colonne:

 sheet.setColumnWidth(1, 25); 

Je crois comprendre que le premier nombre est l'index (donc il s'agirait en fait de la 2ème colonne) et le second nombre est la longueur du caractère, de sorte que toutes les cellules de la colonne doivent comporter 25 caractères d'espace blanc.

J'apprécierais toute aide à ce sujet!

J'ai compris le problème.

La valeur n'est en réalité pas le nombre de caractères mais autre chose. J'ai essayé 5000 et redimensionné correctement la colonne.

La vraie réponse est que Sheet.setColumnWidth(int index, int width) définit la colonne avec pour la colonne en largeur / 256. Vous avez raison d'affirmer que l'indice est un numéro de colonne basé sur zéro. La largeur est spécifiée en 1/256 d'un caractère.