You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by mouss4rs <ou...@gmail.com> on 2012/02/23 17:46:47 UTC
Error: Package should contain a content type part [M1.13]
Hello every body,
I have this error:
org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should
contain a content type part [M1.13]
at
org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:147)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:592)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:222)
at Extraction.main(Extraction.java:155)
The ligne 155 is in bold:
int nbfeuille=0;
int ligne_trouvee;
//version .xls
/*InputStream ist = new
FileInputStream("C:/Users/LZ2059/Desktop/Projection_2012_Eq2.xls"); //pour
ouvrir les fichiers xls de 2007
HSSFWorkbook wb2 = new HSSFWorkbook(ist);
HSSFSheet sheet2 = wb2.getSheetAt(0);
nbfeuille= wb2.getNumberOfSheets();//retourne le nombre de feuille
renseigné
HSSFRow row2 = null;
HSSFCell cell2 = null;
HSSFCellStyle cellStyle = null;*/
//version .xlsx
InputStream ist = new
FileInputStream("C:/Users/LZ2059/Desktop/Projection_2012_Eq2.xlsx");
*OPCPackage opc=OPCPackage.open(ist);* XSSFWorkbook wb2= new
XSSFWorkbook(opc);
XSSFSheet sheet2 = wb2.getSheetAt(0);//onglet 0
nbfeuille= wb2.getNumberOfSheets();//retourne le nombre de feuille
renseigné
XSSFRow row2 = null;
XSSFCell cell2 = null;
XSSFCellStyle cellStyle = null;
XSSFDataFormat fmt = wb2.createDataFormat();
//pour ouvrir un .xlsx
FormulaEvaluator evaluator =
wb2.getCreationHelper().createFormulaEvaluator();
evaluator.evaluate(cell2);
String prenom_proj;
Collator usCollator;
System.out.println("wb2.getNumberOfNames(): "+nbfeuille);
// il faut définir la feuille qui sera alimenté:
//Parcourir les feuilles du document
//si la semaine correspond au libellé d'une feuille
for (int g=0; g<nbfeuille; g++){//boucle de parcours des onglets
sheet2 = wb2.getSheetAt(g);
onglet=sheet2.getSheetName();// on récupère le nom de la feuille excel
en String
if(onglet.startsWith("S")){//si ca commence par "S"
//on enleve le "S"
semaine_en_string=onglet.substring(1);
// on converti le nom de l'onglet en Int
onglet_conv = Integer.parseInt(semaine_en_string);
System.out.println("onglet: "+onglet_conv);
if(onglet_conv == semaine){//si la semaine de l'onglet du doc
"extract.xls"
//correspond à la semaine de l'année trouver dans le fichier
"Projection_2012_Eq2.xls"
System.out.println("onglet de la semaine "+onglet_conv+" TROUVEE !");
for (Iterator rowIt = sheet2.rowIterator();
rowIt.hasNext();){//boucle de parcours du fichier de ligne en ligne
row2 = (XSSFRow) rowIt.next();
if(row2.getCell(0)!= null && row2.getRowNum() <= 19){// si la ligne
est différent de null et inférieur ou égale à la 20ème ligne
if(!row2.getCell(0).getStringCellValue().equals("Noms") &&
!row2.getCell(0).getStringCellValue().equals("EQUIPE") &&
!row2.getCell(0).getStringCellValue().equals("")){
//on récupère le 1er prénom du fichier Projection
prenom_proj=row2.getCell(0).getStringCellValue().toUpperCase();
for (int d=0; d<tab.length;d++){
if(tab[0][d] != null || tab[1][d]!=null){//si l'une des cellules
n'est pas null
//System.out.println("Tab"+tab[0][d]);
//System.out.println("prénom: "+prenom_proj);
//Get the Collator for US English and set its strength to
PRIMARY
usCollator = Collator.getInstance(Locale.FRANCE);
usCollator.setStrength(Collator.PRIMARY);
if( usCollator.compare(prenom_proj, tab[0][d]) == 0 ){//si le
prénom trouvé dans le fichier 'Projection_2012_Eq2.xlsx' correspond au
prenom de l'extract Hermes
//System.out.println("Strings are equivalent");
System.out.println("Agent: "+tab[0][d]+" Validation:
"+tabvalidation[1][d]);
//on récupère la ligne du prénom
ligne_trouvee= row2.getCell(0).getRowIndex();
ligne_trouvee++;//pour écrire dans la bonne ligne
//System.out.println("ligne TROUVEE: "+ligne_trouvee);
//on écrit dans la colonne I la validation du prénom
trouvé
cell2 = row2.getCell(8);// colonne(I)
cell2 = row2.createCell(8);
cell2.setCellType(Cell.CELL_TYPE_NUMERIC);
cell2.setCellValue(tabvalidation[1][d]);
//on ré-écrit la formule pour la colonne J
cell2 = row2.getCell(9);// colonne(J)
cell2 = row2.createCell(9);
cell2.setCellFormula("I"+ligne_trouvee+"/B"+ligne_trouvee);
//System.out.println("Formule:
I"+ligne_trouvee+"/B"+ligne_trouvee);
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(fmt.getFormat("#,##0.00"));
cell2.setCellStyle(cellStyle);
//on ré-écrit la formule pour la colonne K
cell2 = row2.getCell(10);// colonne(K)
cell2 = row2.createCell(10);
cell2.setCellFormula("I"+ligne_trouvee+"/C"+ligne_trouvee);
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(fmt.getFormat("#,##0.00"));
cell2.setCellStyle(cellStyle);
//on ré-écrit la formule pour la colonne L
cell2 = row2.getCell(11);// colonne(L)
cell2 = row2.createCell(11);
cell2.setCellFormula("J"+ligne_trouvee+"*7");
//System.out.println("Formule: J"+ligne_trouvee+"*7");
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(fmt.getFormat("#,##0.00"));
cell2.setCellStyle(cellStyle);
ligne_trouvee--;//pour retrouver ma ligne
// Write the output to a file
//pour ecrire dans le même fichier
FileOutputStream fileOut = new
FileOutputStream("C:/Users/LZ2059/Desktop/Projection_2012_Eq2.xlsx");
wb2.write(fileOut);
fileOut.close();
}
}
}
}
}
}
//on réécrit la formule du total colonne I
row2.setRowNum(20);
//System.out.println("on doit se fixer à la ligne 20:
"+row2.getRowNum());
cell2 = row2.createCell(8);
String colonneI = "I3:I20";
cell2.setCellFormula("SUM("+colonneI+")");
//format EQUIPE de la colonne I
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));//format 1
000 par ex.
cell2.setCellStyle(cellStyle);
//on réécrit la formule du total colonne J
row2.setRowNum(20);
//System.out.println("on doit se fixer à la ligne 20:
"+row2.getRowNum());
cell2 = row2.createCell(9);//colonne J
String resultatParH = "SUM(I3:I20)/SUM(B3:B20)";
cell2.setCellFormula(resultatParH);
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
cell2.setCellStyle(cellStyle);
//on réécrit la formule du total colonne K
row2.setRowNum(20);
//System.out.println("on doit se fixer à la ligne 20:
"+row2.getRowNum());
cell2 = row2.createCell(10);//colonne K
String txtransfo = "(SUM(I3:I20)/SUM(C3:C20))/100";//j'ai rajouté
/100 pour prendre en compte le % dans l'ecriture du format
cell2.setCellFormula(txtransfo);
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
cell2.setCellStyle(cellStyle);
//on réécrit la formule du total colonne L
row2.setRowNum(20);
//System.out.println("on doit se fixer à la ligne 20:
"+row2.getRowNum());
cell2 = row2.createCell(11);//colonne L
String resultatetp = "J21*7";
cell2.setCellFormula(resultatetp);
//arrondir à 2 chiffre après la virgule
cellStyle = wb2.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell2.setCellStyle(cellStyle);
//pour ecrire dans le même fichier
FileOutputStream fileOut = new
FileOutputStream("C:/Users/LZ2059/Desktop/Projection_2012_Eq2.xlsx");
wb2.write(fileOut);
fileOut.close();
//System.out.println("somme colonne I: "+row2.getCell(8).get);
}
}
}//for
In fact, I open and I write on the same file.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Error-Package-should-contain-a-content-type-part-M1-13-tp5508605p5508605.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: error: XmlValueDisconnectedException
Posted by mouss4rs <ou...@gmail.com>.
It's good.
I put the FileOutPutStream outside of the for.
like this:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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;
public class LireEcrire {
/**
* @param args
*/
public static void main(String[] args) {
try {
//pour ouvrir un .xlsx
InputStream is = new FileInputStream("workbook.xlsx");
OPCPackage opc=OPCPackage.open(is);
XSSFWorkbook wb= new XSSFWorkbook(opc);
XSSFSheet sheet = (XSSFSheet)wb.getSheetAt(0);//onglet 0
XSSFRow row = null;
XSSFCell cell = null;
//pour ouvrir un .xlsx
FormulaEvaluator evaluator =
wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluate(cell);
System.out.println("1");
for(int i=0;i<5;i++){
System.out.println("2");
row = sheet.getRow(i);
System.out.println("4");
cell = row.createCell(i);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("sisi");
System.out.println(cell.getRowIndex() + ":"+ cell.getColumnIndex() + " -
" + cell + " : "+ cell.getCellType());
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/error-XmlValueDisconnectedException-tp5508605p5512069.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org