You need to apply a style to the cell. The style can include a number format.
DataFormat format = workbook.createDataFormat();
short fmt = format.getFormat("#0.0");
CellStyle style = workbook.createCellStyle();
style.setDataFormat(fmt);
…
cell.setCellStyle(style);
From: Syed Mudassir Ahmed [mailto:[email protected]]
Sent: Wednesday, May 16, 2018 11:56 PM
To: POI Users List <[email protected]>
Subject: Re: Is it POI error starting 3.14 version onwards
And one more thing, this is my source code:
public static void main(String str[]) throws Exception {
Map<String, Object> input = new LinkedHashMap<>();
SXSSFWorkbook workbook = new SXSSFWorkbook();
OutputStream os = new FileOutputStream(
new File("/home/gaian/Desktop/Sal.xlsx"));
workbook.setCompressTempFiles(true);
SXSSFSheet sheet = workbook.createSheet("firstSheet");
SXSSFRow row = sheet.createRow(0);
SXSSFCell cell = row.createCell(0);
cell.setCellValue("salary");
cell.setCellType(CellType.STRING);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue(new Double("4.0"));
cell.setCellType(CellType.NUMERIC);
workbook.write(os);
os.close();
workbook.close();
System.out.println("done creating excel workbook...");
}
I am creating a workbook and writing a value (4.0) as double in a sheet. But
when I open the sheet, I see the integral value 4 instead of decimal 4.0.
Any settings I need to do in my code to get the value 4.0?
Thanks.
Thanks,
[cid:ii_14fd4e82bb9756bc]
On Thu, May 17, 2018 at 9:07 AM, Syed Mudassir Ahmed
<[email protected]<mailto:[email protected]>>
wrote:
Thanks so much Tim and Fanning. Both of your suggestions are working out. I
would suggest better to have such example in the test case section of your
source folder.
Thanks,
[cid:ii_14fd4e82bb9756bc]
On Wed, May 16, 2018 at 6:49 PM, Tim Allison
<[email protected]<mailto:[email protected]>> wrote:
You need to make your SAXReader namespace aware:
saxFactory.setNamespaceAware(true);
On Wed, May 16, 2018 at 8:59 AM, Tim Allison
<[email protected]<mailto:[email protected]>> wrote:
> Sorry for my delay. I just tested your file with Apache Tika 1.18 which
> uses POI 3.17..., and I got:
>
>
> <body><div><h1>Sheet1</h1>
> <table><tbody><tr> <td>Salary</td></tr>
> <tr> <td>99.965432</td></tr>
> </tbody></table>
> </div>
> <div><h1>Sheet2</h1>
> <table><tbody/></table>
> </div>
> <div><h1>Sheet3</h1>
> <table><tbody/></table>
> </div>
> </body></html>
>
> That's promising... Let me take a look at your example code.
>
> On Wed, May 16, 2018 at 1:31 AM, Syed Mudassir Ahmed <syed.mudassir@
<mailto:syed.mudassir@%0b>> gaianconsultants.com<http://gaianconsultants.com>>
wrote:
>
>> any update on this pls? This is blocking me.
>>
>> Thanks,
>>
>>
>> On Tue, May 15, 2018 at 3:45 PM, Syed Mudassir Ahmed <
>> [email protected]<mailto:[email protected]>>
>> wrote:
>>
>>> Yes, pls find the file attached here.
>>>
>>> Thanks,
>>>
>>>
>>> On Tue, May 15, 2018 at 3:43 PM, Tim Allison
>>> <[email protected]<mailto:[email protected]>>
>>> wrote:
>>>
>>>> Any chanc you can share the file?
>>>>
>>>> On Tue, May 15, 2018 at 3:19 AM Syed Mudassir Ahmed <
>>>> [email protected]<mailto:[email protected]>>
>>>> wrote:
>>>>
>>>> > Hi,
>>>> > I am trying to read data from a XLSX sheet via
>>>> XSSFSheetXMLHandler. The
>>>> > source code is below.
>>>> >
>>>> > public static void main(String str[]) throws Exception {
>>>> > String filePath
>>>> > = "/home/gaian/Desktop/salary.xlsx";
>>>> > File file = new File(filePath);
>>>> > InputStream inputStream = new FileInputStream(file);
>>>> > OPCPackage pkg = OPCPackage.open(inputStream);
>>>> >
>>>> > SheetContentsHandler sheetContentsHandler = new
>>>> > SheetContentsHandler() {
>>>> > @Override
>>>> > public void startRow(int rowIndex) {
>>>> > }
>>>> >
>>>> > @Override
>>>> > public void endRow(int i) {
>>>> > }
>>>> >
>>>> > @Override
>>>> > public void cell(String cell, String formattedValue,
>>>> > XSSFComment c) {
>>>> > System.out.println("cell encountered with addess:<" +
>>>> cell
>>>> > + "> and value:<" + formattedValue + ">");
>>>> > }
>>>> >
>>>> > @Override
>>>> > public void headerFooter(String text, boolean isHeader,
>>>> String
>>>> > tagName) {
>>>> > System.out.println("headerFooter()");
>>>> > }
>>>> > };
>>>> >
>>>> > ReadOnlySharedStringsTable strings = new
>>>> > ReadOnlySharedStringsTable(pkg);
>>>> > XSSFReader xssfReader = new XSSFReader(pkg);
>>>> > StylesTable styles = xssfReader.getStylesTable();
>>>> > XSSFReader.SheetIterator worksheets =
>>>> (XSSFReader.SheetIterator)
>>>> > xssfReader.getSheetsData();
>>>> > InputStream stream = worksheets.next();
>>>> > SAXParserFactory saxFactory = SAXParserFactory.newInstance();
>>>> > XMLReader sheetParser = saxFactory.newSAXParser().getX
>>>> MLReader();
>>>> >
>>>> > ContentHandler handler
>>>> > = new XSSFSheetXMLHandler(styles, strings,
>>>> > sheetContentsHandler, false);
>>>> >
>>>> > sheetParser.setContentHandler(handler);
>>>> > sheetParser.parse(new InputSource(stream));
>>>> > }
>>>> >
>>>> > When I use the POI version 3.13, I am getting the following output:
>>>> >
>>>> > cell encountered with addess:<A1> and value:<Salary>
>>>> > cell encountered with addess:<A2> and value:<99.965432>
>>>> >
>>>> > The moment I switch to version 3.14 or higher, I am no longer
>>>> getting
>>>> > any output.
>>>> >
>>>> > Can someone pls let me know if any more code changes needed if I
>>>> switch
>>>> > to 3.14 or higher? I even checked the test cases in Apache POI 3.17
>>>> > sources but was shocked not to find any there. Any
>>>> example/references that
>>>> > I can go through pls? This is blocker for one of my applications.
>>>> >
>>>> >
>>>> > Thanks,
>>>> >
>>>> >
>>>>
>>>
>>>
>>
>