Java - problemilla en java al actualizar un excel

 
Vista:

problemilla en java al actualizar un excel

Publicado por the_pas (2 intervenciones) el 16/06/2008 20:03:12
Tengo el siguiente programa en Java, que lo que me hace es actualizar un fichero excel, pero el problema que tengo es que cuadno abro el excel als formulas que me ejecuta se queda en #¡VALOR! pero si le doy F2 y luego intro se me pone el valor correcto,cual puede ser mi problema en el codigo??

os adjunto el codigo:

public static void main(String[] args) {
// TODO Auto-generated method stub
RentaVariable rv=new RentaVariable(args[0]);
rv.generaFichero();
}

public RentaVariable()
{
this.rutaProperty="";
}

public RentaVariable(String rutaProperty)
{
//Almaceno el fichero de entrada
this.rutaProperty=rutaProperty;
this.cargaConfiguracion(this.rutaProperty);
}

public void generaFichero()
{
String cadenaEntrada; //Linea auxiliar para recoger la cadena de entrada
int row; //Contador que me dice porque linea voy
String token=","; //Token con el que separaremos los archivos de entrada
String[] partirCadena; //Array que almacena la cadena guardada.
HSSFSheet hoja; //Hoja Excel


//Primero abrimos el fichero de entrada
try
{
//Primero abrimos la hoja excel
FileInputStream fileIn = new FileInputStream(this.ficheroSalida);
//Abro el ficheros excel
HSSFWorkbook wb = new HSSFWorkbook(fileIn);
//Ahora cojo la hoja que quería
hoja = wb.getSheet(this.nombreHoja);
//Abro el fichero de entrada
entrada =new BufferedReader(new FileReader(this.ficheroEntrada));
//Inicializo la fila inicial
row=this.filaInicio;
//Recorro las lineas del fichero
while((cadenaEntrada=entrada.readLine())!=null)
{
partirCadena=cadenaEntrada.split(token);
for (int j=0;j<partirCadena.length;j++)
{
if (partirCadena[j]!=null)
{
if (row>=this.filaInicio+this.numFilaCabecera) //Si estamos fuera de la cabecera miro la configuración
{
//Grabamos la celda
if (this.tipoDatos[j]!=null && this.tipoDatos[j].compareTo("string")==0)
hoja.createRow(row).createCell((short)j).setCellValue(new HSSFRichTextString(partirCadena[j]));
else if (this.tipoDatos[j]!=null && this.tipoDatos[j].compareTo("number")==0)
hoja.createRow(row).createCell((short)j).setCellValue(Double.parseDouble(partirCadena[j]));
}
else //Sino lo meto como Texto enriquecido.
hoja.createRow(row).createCell((short)j).setCellValue(new HSSFRichTextString(partirCadena[j]));
}
}
row++;
}
hoja.setForceFormulaRecalculation(true);
wb.removeName("Inputs");
HSSFName nombre = wb.createName();
nombre.setNameName("Inputs");
nombre.setReference("Input!$A$4:$E$1001");

wb.getSheet("FRP").setForceFormulaRecalculation(true);
fileIn.close();
wb.write(new FileOutputStream(this.ficheroSalida));
entrada.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
private void cargaConfiguracion(String fichero) {
Properties props = new Properties();
try {
props.loadFromXML(new FileInputStream(new File(fichero)));


//int filas=enumerator
this.separador=props.getProperty("Separador");
this.ficheroEntrada=props.getProperty("ficheroEntrada");
this.ficheroSalida=props.getProperty("ficheroSalida");
this.nombreHoja=props.getProperty("nombreHoja");
this.numColumnas=Integer.parseInt(props.getProperty("numColumnas"));
this.filaInicio=Integer.parseInt(props.getProperty("filaInicio"));
this.numFilaCabecera=Integer.parseInt(props.getProperty("filasCabecera"));
//Dimensiono el array
this.tipoDatos=new String[this.numColumnas];
//Ahora relleno el array con el tipo de datos que necesito
for (int i=0;i<this.numColumnas;i++)
this.tipoDatos[i]=props.getProperty("col"+i);
} catch (InvalidPropertiesFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:problemilla en java al actualizar un excel

Publicado por ribery (1 intervención) el 19/06/2008 18:31:58
Hola the_pas, el problema que puedes tener es de actualización de fórmulas.
Prueba actualizar todas las celdas que contengan fórmulas justamente antes del write.
El ejemplo de código es este:

HSSFWorkbook wb = new HSSFWorkbook(fis);
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
HSSFSheet sheet = wb.getSheetAt(sheetNum);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);

for(Iterator rit = sheet.rowIterator(); rit.hasNext();) {
HSSFRow r = (HSSFRow)rit.next();
evaluator.setCurrentRow(r);

for(Iterator cit = r.cellIterator(); cit.hasNext();) {
HSSFCell c = (HSSFCell)cit.next();
if(c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
evaluator.evaluateFormulaCell(c);
}
}
}
}


Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar