import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main {
public static void main(String[] args) throws IOException {
String Workbookpath="file.xlsx";
XSSFWorkbook XW =ReadWorkbook(Workbookpath);
//here you put how many sheet in your workbook
int sheetNumber=5;
for(int index=0; index<=sheetNumber;index++){
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet=CreateNewWorkBook(wb,"sheet"+index+".xlsx");
ReadDataFromSheet(index,XW,sheet);
FileOutputStream fileOut = new FileOutputStream("sheet"+index+".xlsx");
wb.write(fileOut);
fileOut.close();
}
}
//Lire le fichier mére le workbook en anglais
public static XSSFWorkbook ReadWorkbook(String Workbookpath) throws IOException{
File WorkSheet = new File(Workbookpath);
FileInputStream fis = new FileInputStream(WorkSheet);
XSSFWorkbook XW = new XSSFWorkbook(fis);
return XW;
}
//nice til here
public static void ReadDataFromSheet(int SheetNumber,XSSFWorkbook XW,XSSFSheet sheet){
XSSFSheet HS= XW.getSheetAt(SheetNumber);
Iterator <Row> rowIterator = HS.iterator();
while(rowIterator.hasNext()){
Row nextRow = rowIterator.next();
XSSFRow row = sheet.createRow(nextRow.getRowNum());
Iterator <Cell> cellIterator = nextRow.cellIterator();
while(cellIterator.hasNext()){
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC :
//on copie la valeur contenu dans le champ dans le nouveau champ dans la nouvelle feuille
row.createCell(cell.getColumnIndex()).setCellValue(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING :
row.createCell(cell.getColumnIndex()).setCellValue(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK :
//blank
row.createCell(cell.getColumnIndex()).setCellValue("");
break;
case Cell.CELL_TYPE_FORMULA :
row.createCell(cell.getColumnIndex()).setCellValue(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BOOLEAN :
row.createCell(cell.getColumnIndex()).setCellValue(cell.getBooleanCellValue());
break;
default :
}
}
}
}
//Une méthode pour creer un nouveau workbook pour chaque feuille (sheet)
public static XSSFSheet CreateNewWorkBook(XSSFWorkbook wb ,String WorkbookName) throws IOException{
XSSFSheet sheet = wb.createSheet();
return sheet;
}
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
31 mars 2016 à 19:01
juste une classe java qui prend la feuille1,feuille2,feuille3 d'un classeur excel et créer chaque feuille un fichier excel. donc voici comment je fait en vba "
Sub Splitbook()
'Updateby20140612
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & "__" & " " & xWs.Name & "- 30 - March 16" & ".xls"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
"
en java ca marche pas chez moi