problem with poi, XML and sorting excel file

Discussion in 'Java' started by [XaToA], Jul 29, 2003.

  1. [XaToA]

    [XaToA] Guest

    i have the bellow code. with this code i want to parse one XML String and to
    convert into one EXCEL sheet using the apache POI classes.
    the code creates the sheet, but it doesn't put the values into the cells.
    the XML String has several <registro> nodes, foreach registro node i want to
    create one row. Each registro node has several nodes (can be 1 or x nodes),
    and foreach node into the registro node i wanto to create one cell into the
    registro row.

    can you help me for solving this problem?

    i am working into this problem 7 days and i dont solve it.
    please help me.
    thanks


    import java.io.*;
    import java.util.*;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;

    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;

    public class convierteXML2EXCEL {
    StringBuffer sb = new StringBuffer();
    String delim = "-";
    String nl = System.getProperty("line.separator");
    SimpleDateFormat fechabase = new SimpleDateFormat("dd-MM-yyyy");
    String strhoy = fechabase.format(new Date());
    short rowNum = 0;
    short colNum = 0;
    public String getEXCEL(Document doc) {
    buscar(doc, 0);
    sb.setLength(sb.length()-1);
    return sb.toString();
    }

    public void buscar(Node node, int level) {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFRow row = null;
    HSSFSheet sheet = wb.createSheet("Preguntas-soporte, "+strhoy);
    NodeList listar = node.getChildNodes();
    row = sheet.createRow(rowNum);

    for (int i=0; i<listar.getLength(); i++) {
    Node childNode = listar.item(i);
    if (level==3 && childNode.getNodeType()==3) {
    row.createCell(colNum).setCellValue(childNode.getNodeValue());
    sb.append("__ "+childNode.getNodeValue() + delim);
    colNum++;
    } else if (level==1 && childNode.getNodeType()==1 && sb.length()>1)
    {
    rowNum++;
    colNum=0;
    sb.setLength(sb.length()-1);
    sb.append(nl);
    }
    buscar(childNode, level+1);
    }
    try{
    FileOutputStream fileOut = new
    FileOutputStream("c:\\"+strhoy+".xls");
    wb.write(fileOut);
    fileOut.close();
    }catch(Exception e){
    System.out.println("sss "+e.getMessage());
    }
    }

    public static void main(String[] args) throws Exception {
    String f ="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+
    "<registros>"+

    "<registro><nombre>nombre1</nombre><apellido>apellido1</apellido><apellido2>
    apellido2</apellido2></registro>"+

    "<registro><nombre>nombre2</nombre><apellido>apellido2</apellido></registro>
    "+
    "</registros>";

    Document doc = null;
    DocumentBuilder db =
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
    if (args.length == 1)
    doc = db.parse( args[0] );
    else
    doc = db.parse(new ByteArrayInputStream(f.getBytes()));
    convierteXML2EXCEL k = new convierteXML2EXCEL();
    f = k.getEXCEL(doc);
    System.out.println("."+f+".");
    }
    }
    [XaToA], Jul 29, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barbara Vernaeve

    POI HSSF generate 2nd excel worksheet.

    Barbara Vernaeve, Feb 1, 2005, in forum: Java
    Replies:
    0
    Views:
    565
    Barbara Vernaeve
    Feb 1, 2005
  2. Bala via JavaKB.com

    POI - Excel cell formatting for negative values

    Bala via JavaKB.com, Mar 5, 2005, in forum: Java
    Replies:
    0
    Views:
    2,664
    Bala via JavaKB.com
    Mar 5, 2005
  3. Replies:
    7
    Views:
    6,714
  4. Tk
    Replies:
    3
    Views:
    7,237
    Oliver Wong
    Jul 11, 2006
  5. Replies:
    0
    Views:
    413
Loading...

Share This Page