I have searched stackoverflow but I didn't find a clear answer. How can I read data from particular rows and columns of XLS file to my Android application? How can I read XLS file? I don't want to convert it to CSV because I get errors when I try to convert them.
Maybe I could use this http://www.andykhan.com/jexcelapi/tutorial.html#reading but I even don't know how could I import it into my project. Please help.
In this article, you’ll learn how to read data from Excel xls or xlsx file formats into R. This can be done either by: The readxl package, developed by Hadley Wickham, can be used to easily import Excel files (xls|xlsx) into R without any external dependencies. The readxl package comes with the function read_excel () to read xls and xlsx files
Here is a simple method to read all the data in the excel sheet. Read all, meaning empty cells are included too. First, we created an XSSFWorkbook object and named it workbook. Passing to its constructor the path to the location of our .xlsx file. Note that the XSSFWorkbook constructor throws an IOException.
The readxl package comes with the function read_excel () to read xls and xlsx files Read both xls and xlsx files library("readxl") my_data <- read_excel("my_file.xls") my_data <- read_excel("my_file.xlsx") The above R code, assumes that the file “my_file.xls” and “my_file.xlsx” is in your current working directory.
To read data from .xlsx file, we will be using poi-ooxml provided by Apache Poi. To add this to your maven project, check their maven repository. Below are the classes we will use, linked to their documentations. Here is a simple method to read all the data in the excel sheet. Read all, meaning empty cells are included too.
private void printlnToUser(Cell str) {
final Cell string = str;
if (output.length() > 8000) {
CharSequence fullOutput = output.getText();
fullOutput = fullOutput.subSequence(5000, fullOutput.length());
output.setText(fullOutput);
}
output.append(string + "\n");
}
public void ReadXLSX(File path) {
try {
FileInputStream fis = new FileInputStream(path);
XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
break;
case Cell.CELL_TYPE_NUMERIC:
break;
case Cell.CELL_TYPE_BOOLEAN:
break;
default:
}
printlnToUser(cell);
}
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Hi you just need to include an external jxl jar and you can go through the same tutorial which will help you understand the process of reading excel files.. for your referance i am pasting some ref. code which reads very first sheet of excel and creates a resultset.
public List<String> read(String key) throws IOException {
List<String> resultSet = new ArrayList<String>();
File inputWorkbook = new File(inputFile);
if(inputWorkbook.exists()){
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over column and lines
for (int j = 0; j < sheet.getRows(); j++) {
Cell cell = sheet.getCell(0, j);
if(cell.getContents().equalsIgnoreCase(key)){
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cel = sheet.getCell(i, j);
resultSet.add(cel.getContents());
}
}
continue;
}
} catch (BiffException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
resultSet.add("File not found..!");
}
if(resultSet.size()==0){
resultSet.add("Data not found..!");
}
return resultSet;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With