I have a requirement where I have to read hindi text from an excel sheet in java. when I read the hindi text from excel and When I try to print the hindi string , only a set of ??? are displayed.
Can anyone please help me with this.
It will be really helpful. Thanks.
my code for reading --
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
//System.out.println("nextrow"+nextRow);
Iterator<Cell> cellIterator = nextRow.cellIterator();
Cell cell = cellIterator.next();
System.out.println("Cell value =="+cell.getStringCellValue());
//System.out.print("##"+cell.getCellType());
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
Cell cell1 = cell.getRow().getCell(0);
Cell cell2 = cell.getRow().getCell(1);
System.out.println(cell1);
System.out.println(cell2);
if(cell1 != null && cell2 != null){
String col1 = cell1.getStringCellValue();
String col2 = cell2.getStringCellValue();
sentences.add(col1+"_###_"+col2);
}
break;
but when I try to print the readed data it displays ??????. please refer below -
??? ???? ????????? ?? ???????? ??? ?????? ?? ??????
Related
I am trying to parse an Excel file using Apache POI version 3.17. And based on cell background color, I need to get value of cell before and after it (or some location in excel file).
Code snippet:
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(SAMPLE_XLSX_FILE_PATH)));
Sheet sheet = workbook.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
for (Row row: sheet) {
for(Cell cell: row) {
String cellValue = dataFormatter.formatCellValue(cell);
XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle();
XSSFColor cellColor = cellStyle.getFillForegroundXSSFColor();
if( cellColor != null && cellColor.getARGBHex().equals("FFCCFFCC") ){
System.out.println(cellValue);
System.out.println(cell.getAddress());
}
}
}
Is it possible to get value of cell from sheet based on cell address. Something like: sheet.getValueOfCell(cell.getAddress()).
I'm created an excel(xlsx) file with data.
Map<String, List<Object>> dataXlsx = new HashMap<>();
Workbook workBook = new WorkBook();
final XSSFFont blackFont = (XSSFFont) workBook.createFont();
blackFont.setFontHeightInPoints((short) 11);
blackFont.setFontName(CALIBRI);
blackFont.setBold(true);
final XSSFCellStyle blackCellStyle = (XSSFCellStyle) workBook.createCellStyle();
blackCellStyle.setFont(blackFont);
final XSSFFont redFont = (XSSFFont) workBook.createFont();
redFont.setFontHeightInPoints((short) 11);
redFont.setFontName(CALIBRI);
redFont.setBold(true);
redFont.setColor(IndexedColors.RED.getIndex());
final XSSFCellStyle redCellStyle = (XSSFCellStyle) workBook.createCellStyle();
redCellStyle.setFont(redFont);
redCellStyle.setLocked(true);
final XSSFFont blueFont = (XSSFFont) workBook.createFont();
blueFont.setFontHeightInPoints((short) 11);
blueFont.setFontName(CALIBRI);
blueFont.setBold(true);
blueFont.setColor(IndexedColors.BLUE.getIndex());
final XSSFCellStyle blueCellStyle = (XSSFCellStyle) workBook.createCellStyle();
blueCellStyle.setFont(blueFont);
final XSSFSheet newSheet = (XSSFSheet) workBook.createSheet();
final Set<String> rows = dataXlsx.keySet();
for (final String key : rows) {
...
}
}
I want read it.
When I wrote in XLSX, e.g. 3453453453 I get 3.453453453E.
https://i.stack.imgur.com/cmTgj.png
final Iterator<Row> rowIterator = sheet.iterator();
for (int i = 0; rowIterator.hasNext(); i++) {
final Row row = rowIterator.next();
final Iterator<Cell> cellIterator = row.cellIterator();
for (int k = 0; cellIterator.hasNext(); k++) {
final Cell cell = cellIterator.next();
String cellValue = cell.toString(); //set 3453453453, but get 3.45345345E
...
How can I set general format for all cells?
May be are there others any solutions?
Best regards.
I write in XLSX file the value, e.g. 'some string'(first column, first cell), 3453453453(second column, first cell) and save it on disc.
Then I reading this file in Java code with help WorkBook API. When I take first cell, get 'some string' value. When I take second cell, I get 3.453453453E value(wrong format). I Hope this help.
Best regards.
I have a piece of code which throws me (I have made the line bold)
Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFCell cannot be cast to java.lang.String
at com.codi.excel.ExcelRead.main(ExcelRead.java:36)
My code is as follows -
HSSFWorkbook wb = new HSSFWorkbook(input);
HSSFSheet sheet = wb.getSheetAt(0);
List MobileSeries=new ArrayList();
MobileSeries = findRow(sheet, cellContent);
if(MobileSeries !=null){
for(Iterator iter=MobileSeries.iterator();iter.hasNext();){
**String mobileSeries=(String)iter.next();**
String LidPattern=extractNumber(mobileSeries);
if (lid.startsWith(LidPattern)) {
System.out.println("This is a mobile number");
Could you please help me out.
Apache POI provides a handy class for you to do just that - DataFormatter
Using DataFormatter, for string cells you'll get the current contents, and for numeric or date cells the value will be formatted based on the formatting / styling rules applied to the cell, then returned as a string with that applied.
To loop over all the rows and cells in a workbook, getting their values, following the pattern in the docs, just do something like:
Workbook wb = WorkbookFactory.create(input);
Sheet sheet = wb.getSheetAt(0);
DataFormatter formatter = new DataFormatter();
for (Row r : sheet) {
for (Cell c : r) {
String value = formatter.formatCellValue(c);
}
}
Easy!
While you are iterating the rows of a worksheet, check whether the HSSFCell cell is of type String or not.
InputStream fileInputStream = null;
HSSFWorkbook hssfWorkbook;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
Iterator rowIterator, cellIterator;
// Put these loc in a try-catch block
fileInputStream = new FileInputStream("/path/to/TestExcel.xls");
hssfWorkbook = new HSSFWorkbook(fileInputStream);
sheet = hssfWorkbook.getSheetAt(0);
rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
row = (HSSFRow) rowIterator.next();
cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = (HSSFCell) cellIterator.next();
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
String someVariable = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
// Handle numeric type
} else {
// Handle other types
}
}
// Other code
}
Try to access cell and extract value out of it. The below snippet should help you:
Cell cell = iter.next();
cell.getStringCellValue()
in excel the value of column is 10101101010000000000 but when im reading it in java using POI the value is changed to 10101101009999999000, can anyone give me an idea on whats going on and how can i get the exact values from the excel.
i've tried setting the celltype as string and use cell.getStringCellValue() and also this new BigDecimal(cell.getNumericCellValue()).toPlainString() but im still not getting the same value as with the excel
here's my code
List<BankClassVo> data = new ArrayList<BankClassVo>();
FileInputStream fis = new FileInputStream(new File(Constant.VALIDATION_REFERENCE_FILE_PATH + Constant.BANK_CLASSIFICATION_REF_FILE + ".xlsx"));
XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(1);
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
BankClassVo vo = new BankClassVo ();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 0) {
vo.setsClass(new BigDecimal(cell.getNumericCellValue()).toPlainString());
}
else if (cell.getColumnIndex() == 1) {
vo.setClassification(cell.getStringCellValue());
}
}
data.add(vo);
}
myWorkBook.close();
return data;
Use MathContext and RoundingMode. Ref
BigDecimal value = new BigDecimal(cell.getNumericCellValue(), new MathContext(10 , RoundingMode.CEILING));
System.out.println(value.toPlainString());
I'm creating a Java application that I need it to read specific cells, the cells on the document will contain Strings and some Integers. Can you please assist, I found this sample code below, but now I'm stuck. I need to persist the data on the spreadsheet to the database. Thank You
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
I changed your code to a working sample:
InputStream inp = new FileInputStream("workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.println("string: " + cell.getStringCellValue());
}
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.println("numeric: " + cell.getNumericCellValue());
}
System.out.println("any: " + cell.toString());
I hope it helps!