You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by ~ Mail4U~ <vm...@hotmail.com> on 2004/03/22 22:35:58 UTC
Problem retrieving updated value from formulated cell.
Hi all,
I am updating the excel file with new cell value in R1C3 and R2C3 for Rows 1
and 2 respectively.This should give a calculated value (with formula) in
R3C3 for Row 3.
When i try to retreive the value from Cell R3C3, it still gives the old
calculated value existed before updating the excel file.
I am using the HSSF document for the first time and trying to retrieve the
calculated cell values from the excel file by passing the input values to
R1C3 and R2C3.
The problem when i try to open the updated excel file and close, it prompts
for saving the file.Only after I save the excel file manually, i could able
to retrieve the calculated cell value.
Below is the code.Please let me know if there is any work arround way to
resolve this issue.
Thanks in advance.
//Handles different events generated in the process of reading and writing
Excel documents.
import org.apache.poi.hssf.eventmodel.*;
//Provides classes to read Excel documents.*;
import org.apache.poi.hssf.eventusermodel.*;
//Contains classes to handle FORMULA used in Excel document.
import org.apache.poi.hssf.record.formula.*;
//Contains classes to generate Excel documents.*;
import org.apache.poi.hssf.usermodel.*;
//Contains utility classes to handle different attributes of the Excel
document.
import org.apache.poi.hssf.util.*;
import org.apache.poi.hssf.*;
import org.apache.poi.poifs.filesystem.*;
import java.io.*;
// This Progam retrieves the cell value from the formualated Cell of an
Excel File.
public class InteractExcel
{
public static void main(String args[])
{
try
{
//create a POIFSFileSystem object to read the data
FileInputStream fin = new FileInputStream("calculate.xls");
POIFSFileSystem fs = new POIFSFileSystem(fin);
// create a workbook out of the input stream
HSSFWorkbook wb = new HSSFWorkbook(fs);
// get a reference to the worksheet
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow Row1 = sheet.getRow(0);
HSSFRow Row2 = sheet.getRow(1);
HSSFRow Row3 = sheet.getRow(2);
HSSFCell c1 = Row1.getCell((short) 2);
HSSFCell c2 = Row2.getCell((short) 2);
HSSFCell c3 = Row3.getCell((short) 2);
// Updating the cells
c1.setCellValue(35);
c2.setCellValue(35);
// Update the Excel File.
FileOutputStream fout = new FileOutputStream("calculate.xls");
wb.write(fout);
fout.flush();
fout.close();
fin.close();
wb = null;
sheet = null;
// Read the calculated Cell Value from updtaed Excel File.
fin = new FileInputStream("calculate.xls");
POIFSFileSystem fis = new POIFSFileSystem( fin );
// create a workbook out of the input stream
wb = new HSSFWorkbook(fis);
// get a reference to the worksheet
sheet = wb.getSheetAt(0);
Row3 = sheet.getRow(2);
HSSFCell Risk1 = Row3.getCell((short) 2);
fin.close();
}
catch(Exception ex)
{
System.out.println("Exception raised in InteractExcel Main Function "+ ex);
}
}
}
_________________________________________________________________
Check out MSN PC Safety & Security to help ensure your PC is protected and
safe. http://specials.msn.com/msn/security.asp
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org