Comment créer un classeur excel de façon spécifique ?
Salut à tous ?
J'ai un bouton qui permet de générer un classeur Excel (xls, xlsx) qui s'occupe uniquement de créer le classeur, et appel une méthodes pour son contenu.
La méthode du bouton de génération :
Code:
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 boolean dataNEMO2Excel()
{
String fileName = Events.getFile();
StringBuilder builder = new StringBuilder(fileName);
if (fileName != "")
{
try {
int index = fileName.lastIndexOf(".xl");
builder.insert(index,"_NEMO");
fileName = builder.toString();
System.out.println(fileName);
FileOutputStream out = new FileOutputStream(fileName);
FileInputStream in = new FileInputStream(fileName);
Workbook workbook = WorkbookFactory.create(in);
utiMethods.addLog(fileName +" was created", infoMessage.info);
//dataNEMO2Excel(UtilityMethods.getInstance().getMap_cable());
//dataNEMO2Excel(UtilityMethods.getInstance().getMap_link());
//dataNEMO2Excel(UtilityMethods.getInstance().getMap_networkList());
//dataNEMO2Excel(UtilityMethods.getInstance().getMap_networkItem());
dataNEMO2Excel(workbook, UtilityMethods.getInstance().getMap_port());
workbook.write(out);
in.close();
out.close();
workbook.close();
} catch (EncryptedDocumentException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (InvalidFormatException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
} catch (IOException e) {
UtilityMethods.getStackTrace(e);
e.printStackTrace();
}
return true;
}
return false;
} |
La méthode qui crée le contenu :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public Sheet dataNEMO2Excel(Workbook workbook, MAP_Port map_port)
{
Sheet sheet = null;
Row row = null;
String[] headers = new String[]{};
for (String sheetName :mapHeaderNEMO.keySet() )
{
sheet = workbook.createSheet(sheetName);
utiMethods.addLog(sheetName +" was created", infoMessage.info);
headers = mapHeaderNEMO.get(sheetName);
}
row = sheet.createRow(2);
// Ajouter des données dans les cellules
row.createCell(0).setCellValue("Java Execl");
row.createCell(1).setCellValue("Mesexemple.com");
row.createCell(2).setCellValue("Sakoba Adams");
return sheet;
} |
Ce code Crée un classeur sans feuille et donc avec un message d'erreur quand on l'ouvre disant que le format est incorrect, et dans la console :
Code:
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
|
HDMT_NEMO.xlsx
Exception in thread "AWT-EventQueue-0" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:55)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:203)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:170)
at EVENT.UtilityMethods.dataNEMO2Excel(UtilityMethods.java:802)
at EVENT.Events.btnGenerate_click(Events.java:162)
at EVENT.Events.actionPerformed(Events.java:55)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) |
Merci d'avance :) :) :)