You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Dionisio Ruiz de Zarate <di...@tinieblas.com> on 2003/07/29 16:30:22 UTC
POI, EXCEL, and XML (II)
I have change the code and i have put into the for the row creatin but this
creates the sheet but doesn't creates, or puts the values into the rows,
cells.
please can you help me?
mi pourpose is to create one excell sheet with the values of one xml String.
i have working in this during 7 days and i dont solve the problem.
please i need help
thaks
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-HH-mm-ss");
String strhoy = fechabase.format(new Date());
short rowNum = 0;
short colNum = 0;
int colTot,rowTot;
public String getEXCEL(Document doc) {
System.out.println("numero:
"+doc.getElementsByTagName("registro").getLength());
rowTot = doc.getElementsByTagName("registro").getLength();
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;
HSSFCell c = null;
HSSFSheet sheet = wb.createSheet("Preguntas-soporte, "+strhoy);
NodeList listar = node.getChildNodes();
for (int i=0; i<listar.getLength(); i++) {
Node childNode = listar.item(i);
if (level==3 && childNode.getNodeType()==3) {
row = sheet.createRow((short)rowNum);
for(short cellNum = (short) 0; cellNum<10;cellNum++){
c = row.createCell(cellNum);
c.setCellValue("kk ");
}
sb.append("__ "+childNode.getNodeValue() + delim);
colNum++;
} else if (level==1 && childNode.getNodeType()==1 && sb.length()>1)
{
rowNum++;//rowNum=0;
colNum=0;
sb.setLength(sb.length()-1);
sb.append(nl);
row = sheet.createRow((short)i);
}
rowNum++;
//sumamos para luego crear otra fila
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);
}
}