Bonjour à tous,

J'ai le problème suivant qui est peut-être assez claire mais je n'y arrive pas encore :
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
 
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Zip File is closed
	at org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45)
	at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:180)
	at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:696)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:230)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:188)
	at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:85)
	at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:319)
	at EVENT.MyUtilityMethods.createTabPanel(MyUtilityMethods.java:71)
	at EVENT.MyActionEvent.btnLoad_click(MyActionEvent.java:67)
	at EVENT.MyActionEvent.actionPerformed(MyActionEvent.java:34)
	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.mouseReleased(Unknown Source)
	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(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)
Et Maintenant la classe MyUtilityMethods :
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
package EVENT;
 
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
 
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
/**
 * Some utility methods for the application
 * 
 * @author t0163126
 */
public class MyUtilityMethods {
 
	/**
         * Create  tabs based on file and panel 
         * 
         * @param tabPanel
         * @param file
         * @return
         */
	public JTabbedPane createJTabbed(List<JPanel> lstPanel, File file) {
 
		JTabbedPane tabbedPane = new JTabbedPane();
 
		for (JPanel panel : lstPanel) {
			int index = tabbedPane.getTabCount();
			JComponent tabPanel = createTabPanel(file);
			tabbedPane.addTab
			(
				panel.getName(), //tab name 
				null, //tab icon
				tabPanel, //Tabs panel for the JTable 
				panel.getToolTipText() //tab tool tip text
			);
		}
		return tabbedPane;
	}
 
	/**
         * Create a tab panel by excel feel
         * 
         * @param fileName
         * @param excelFile
         * @return
         */
	public JComponent createTabPanel(File excelFile) {
		JTable spreadsheet = new JTable();
		JPanel panel = null;
		XSSFWorkbook wb;
		XSSFSheet sheet;
 
		try {
			wb = new XSSFWorkbook(excelFile.getName());
			//Get the sheet number
			int nbFeuilles = wb.getNumberOfSheets();
			for (int i = 0; i < nbFeuilles; i++) {
				sheet = wb.getSheetAt(i);
				//Create one JTable by excel feel
				spreadsheet = createJTableWithExcel(excelFile, i);
 
				spreadsheet.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
				JScrollPane elevator = new JScrollPane();
				elevator.setViewportView(spreadsheet);
				//Create a panel by excel feel number
				panel = new JPanel();
				panel.setBorder(BorderFactory.createTitledBorder(sheet.getSheetName()));
				panel.setBackground(Color.LIGHT_GRAY);
				panel.add(elevator);
			}
			wb.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return panel;
	}
 
	/**
         * Create a JTable depending on the file and the entry sheet number
         * 
         * @param file
         * @param numSheet
         * @return
         */
	public static JTable createJTableWithExcel(File file, int numSheet) {
		// Create new table
		JTable table = new JTable();
		// Reading the excel file
		XSSFWorkbook wb;
		try {
			wb = new XSSFWorkbook(file);
			// Get the contents of the excel file according to the sheet number
			XSSFSheet sheet = wb.getSheetAt(numSheet);
 
			// Count the line number
			int nbRows = sheet.getLastRowNum();
			// Count the column number
			int nbColumns = numberMaxColumn(sheet);
 
			System.out.println("In the sheet " + sheet.getSheetName()
					+ " there are " + nbRows + " lines and " + nbColumns
					+ " columns");
 
			// Create new object array
			Object[][] obj = new Object[nbRows][nbColumns];
 
			// Array header
			String[] title = new String[nbColumns];
			for (int i = 0; i < nbColumns; i++) {
				title[i] = "#" + i;
			}
 
			// Browsing the sheet and retrieves the lines one by one
			for (int rowContent = 0; rowContent < nbRows; rowContent++) {
				XSSFRow row = sheet.getRow(rowContent);
 
				// Browsing the line and retrieves the columns
				if (row != null) {
					for (int columnContent = 0; columnContent < nbColumns; columnContent++) {
						// Retrieves the cell and this value
						XSSFCell cell = row.getCell((short) columnContent);
						Object value = contentCell(cell);
						// System.out.println(value.toString());
						obj[rowContent][columnContent] = value;
					}
				}
			}
 
			wb.close();
			table.setModel(new DefaultTableModel(obj, title));
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return table;
	}
 
	/**
         * The cell can contain different type of value that should be specifically
         * recovered
         * 
         * @param cell
         * @return
         */
	private static Object contentCell(XSSFCell cell) {
		Object value = null;
 
		if (cell == null) {
			value = "";
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
			value = cell.getBooleanCellValue();
		} else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
			value = cell.getErrorCellValue();
		} else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
			value = cell.getCellFormula();
		} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
			value = cell.getNumericCellValue();
		} else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
			value = cell.getStringCellValue();
		}
		return value;
	}
 
	/**
         * Retrieves the maximum number of column. If a line has 2 empty boxes at
         * the end , they are not taken into account. So to retrieve the number of
         * columns to initialize our table we must browse all lines
         * 
         * @param sheet
         * @return
         */
	private static int numberMaxColumn(XSSFSheet sheet) {
		int r = sheet.getLastRowNum();
		int max = 0;
		int s = 0;
		while (s < r) {
			if (sheet.getRow(s) != null) {
				int c = sheet.getRow(s).getLastCellNum();
				if (c > max) {
					max = c;
				}
			}
			s++;
		}
		return max + 1;
	}
}
Puis la méthode load_click de la classe MyActionEvent qui se déclanche lors d'un clique sur le bouton Load :
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
 
private void btnLoad_click() 
	{
		JFileChooser fc = new MyFileChooser();
 
		//Open the dialog box
		int returnVal = fc.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			//if you validate
			MyUtilityMethods myMethods = new MyUtilityMethods();
 
			List<JPanel> lstPanel = (List<JPanel>) myMethods.createTabPanel(fc.getSelectedFile());
			myMethods.createJTabbed(lstPanel, fc.getSelectedFile());
			for (JPanel p : lstPanel) {
				System.out.println(p.getName());
			}
		} else if (returnVal == JFileChooser.CANCEL_OPTION) {
			//if you cancel
			System.out.println("You chose to cancel this action ");	
		}
	}
Il y a sans doute des truc pas très logique ou plus simple peut-être, alors n'hésitez pas à me le dire

Merci Beaucoup Pour Vos Aides