mirror of https://github.com/apache/poi.git
toString() method for HSSFCell to return
a simple string representation of the cell, irrespective of its type. Originally submitted by Wes Gilster as bug 19294 made some modifications, added testcase. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353693 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ba496912f
commit
faf02ec8d9
|
@ -30,6 +30,9 @@ import org.apache.poi.hssf.record.*;
|
|||
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
|
||||
import org.apache.poi.hssf.record.formula.Ptg;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -38,13 +41,12 @@ import java.util.Date;
|
|||
* Cells can be numeric, formula-based or string-based (text). The cell type
|
||||
* specifies this. String cells cannot conatin numbers and numeric cells cannot
|
||||
* contain strings (at least according to our model). Client apps should do the
|
||||
* conversions themselves. Formula cells are treated like string cells, simply
|
||||
* containing a formula string. They'll be rendered differently.
|
||||
* conversions themselves. Formula cells have the formula string, as well as
|
||||
* the formula result, which can be numeric or string.
|
||||
* <p>
|
||||
* Cells should have their number (0 based) before being added to a row. Only
|
||||
* cells that have values should be added.
|
||||
* <p>
|
||||
* NOTE: the alpha won't be implementing formulas
|
||||
*
|
||||
* @author Andrew C. Oliver (acoliver at apache dot org)
|
||||
* @author Dan Sherman (dsherman at isisph.com)
|
||||
|
@ -942,4 +944,41 @@ public class HSSFCell
|
|||
this.sheet.setActiveCellRow(this.row);
|
||||
this.sheet.setActiveCellCol(this.cellNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the cell
|
||||
*
|
||||
* This method returns a simple representation,
|
||||
* anthing more complex should be in user code, with
|
||||
* knowledge of the semantics of the sheet being processed.
|
||||
*
|
||||
* Formula cells return the formula string,
|
||||
* rather than the formula result.
|
||||
* Dates are displayed in dd-MMM-yyyy format
|
||||
* Errors are displayed as #ERR<errIdx>
|
||||
*/
|
||||
public String toString() {
|
||||
switch (getCellType()) {
|
||||
case CELL_TYPE_BLANK:
|
||||
return "";
|
||||
case CELL_TYPE_BOOLEAN:
|
||||
return getBooleanCellValue()?"TRUE":"FALSE";
|
||||
case CELL_TYPE_ERROR:
|
||||
return "#ERR"+getErrorCellValue();
|
||||
case CELL_TYPE_FORMULA:
|
||||
return getCellFormula();
|
||||
case CELL_TYPE_NUMERIC:
|
||||
//TODO apply the dataformat for this cell
|
||||
if (HSSFDateUtil.isCellDateFormatted(this)) {
|
||||
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
|
||||
return sdf.format(getDateCellValue());
|
||||
}else {
|
||||
return getNumericCellValue() + "";
|
||||
}
|
||||
case CELL_TYPE_STRING:
|
||||
return getStringCellValue();
|
||||
default:
|
||||
return "Unknown Cell Type: " + getCellType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -244,6 +244,42 @@ extends TestCase {
|
|||
in.close();
|
||||
}
|
||||
|
||||
/*tests the toString() method of HSSFCell*/
|
||||
public void testToString() throws Exception {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
HSSFSheet s = wb.createSheet("Sheet1");
|
||||
HSSFRow r = s.createRow(0);
|
||||
HSSFCell c;
|
||||
c=r.createCell((short) 0); c.setCellValue(true);
|
||||
assertEquals("Boolean", "TRUE", c.toString());
|
||||
c=r.createCell((short) 1); c.setCellValue(1.5);
|
||||
assertEquals("Numeric", "1.5", c.toString());
|
||||
c=r.createCell((short)(2)); c.setCellValue("Astring");
|
||||
assertEquals("String", "Astring", c.toString());
|
||||
c=r.createCell((short) 3); c.setCellErrorValue((byte) 7);
|
||||
assertEquals("Error", "#ERR7", c.toString());
|
||||
c=r.createCell((short)4); c.setCellFormula("A1+B1");
|
||||
assertEquals("Formula", "A1+B1", c.toString());
|
||||
|
||||
//Write out the file, read it in, and then check cell values
|
||||
File f = File.createTempFile("testCellToString",".xls");
|
||||
wb.write(new FileOutputStream(f));
|
||||
wb = new HSSFWorkbook(new FileInputStream(f));
|
||||
assertTrue("File exists and can be read", f.canRead());
|
||||
|
||||
s = wb.getSheetAt(0);r=s.getRow(0);
|
||||
c=r.getCell((short) 0);
|
||||
assertEquals("Boolean", "TRUE", c.toString());
|
||||
c=r.getCell((short) 1);
|
||||
assertEquals("Numeric", "1.5", c.toString());
|
||||
c=r.getCell((short)(2));
|
||||
assertEquals("String", "Astring", c.toString());
|
||||
c=r.getCell((short) 3);
|
||||
assertEquals("Error", "#ERR7", c.toString());
|
||||
c=r.getCell((short)4);
|
||||
assertEquals("Formula", "A1+B1", c.toString());
|
||||
}
|
||||
|
||||
public static void main(String [] args) {
|
||||
System.out
|
||||
.println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");
|
||||
|
|
Loading…
Reference in New Issue