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
| package com.maSociete.MonSoft.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.maSociete.monSoft.monSoftProgram;
import com.maSociete.monSoft.Util;
/**
* @author leminipouce
*
* The goal of this class is to parse Excel file, in order to have them readable
* in monSoft.
*
* The way this class is implemented is to parse the XLS file and build equivalent
* XML stream, respecting the XML format defined for monSoft.
*/
public final class MonSoftXLSToXML
{
/**
* display the stream
*/
public static boolean display(String stream) {
String options[] = new String[] {"Agree", "Exit"};
int chosen = JOptionPane.showOptionDialog(null,
stream,
"",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
switch(chosen) {
case 0:
return true;
case JOptionPane.CLOSED_OPTION:
return false;
}
return false;
}
/**
* Builds a String, containing the XML stream corresponding to the XLS file given
* in param.
* This XML stream is well formatted, respecting the Dashbaord DTD.
*/
public static String buildXmlFromXls(File xlsFile){
String xmlStream=null;
StringBuffer buildingXmlStream=new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tree>\n");
POIFSFileSystem fs=null; //Neccessary to open the Excel file
HSSFWorkbook workBook=null; //Reference to the Excel workbook
HSSFSheet mapDataSheet=null; //Reference to the primary Sheet, containing the data set
if(display("Coucou 1")){};
try{
if(display("xlsFile "+xlsFile.getName()+" path "+xlsFile.getPath())){};
if(display("FIS "+new FileInputStream(xlsFile).toString())){};
fs = new POIFSFileSystem(new FileInputStream(xlsFile));
if(display("FS "+fs.toString())){};
}catch (FileNotFoundException e){
if(display("FNFException ")){};
e.printStackTrace();
return null;
}catch (IOException e){
if(display("IOException ")){};
e.printStackTrace();
return null;
}catch(Exception e){
if(display("Exception ")){};
e.printStackTrace();
return null;
}
if(display("FS récupéré ")){}; |
Partager