i am trying to get data based on headers in the below first i am geting first row and storing it has keys in map . but the problem is i want to get the data from 2nd row but i got iterator.how can i get data from second row??
DataFormatter df=new DataFormatter();
Map<String,Integer> m=new HashMap<String,Integer>();
FileInputStream file = new FileInputStream(new File("/root/Documents/xyz.xlsx"));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
Row row=sheet.getRow(0);
XSSFRow rowe = (XSSFRow)row;
int s=row.getLastCellNum();
System.out.println(s);
for(int i=0;i<s-1;i++){
String data= df.formatCellValue(rowe.getCell(i));
System.out.println(data);
m.put(data,i);
}
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
XSSFRow rowa = (XSSFRow) rows.next();
System.out.println(df.formatCellValue(rowa.getCell(m.get("Member.Member Card Number"))));
}
Is this what you are looking for ?
int count =0;
while (rows.hasNext()) {
XSSFRow rowa = (XSSFRow) rows.next();
if(count !=0){
System.out.println(df.formatCellValue(rowa.getCell(m.get("Member.Member Card Number"))));
}
count++;
}
Try something like this
Vector cellVectorHolder = new Vector();
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
List list = new ArrayList();
while(cellIter.hasNext()){
XSSFCell myCell = (XSSFCell) cellIter.next();
list.add(myCell);
}
cellVectorHolder.addElement(list);
}
Related
I'm having a hard time visualizing a good way to perform this. As you can see by the below code, I'm going through every row and getting values for each cell. However, when actually running the query to add them to a table, it's easier if you can input a whole row at a time. There's enough columns/variability though where I don't want to make a separate value for each column in the excel document. Any ideas on how to run this more efficiently?
for (int i=1; i<rowNum; i++) {
XSSFRow row = ws.getRow(i) ;
for (int j = 0; j<colNum; j++) {
XSSFCell cell = row.getCell(j) ;
String value = cell.getStringCellValue();
data[i][j] = value ;
System.out.println("the value is " + value);
stmt.executeUpdate("INSERT INTO databasetable VALUES(" + value + ")");
}
}
You can spawn an iterator on your worksheet to have it more efficient.
Something like the below
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try{
FileInputStream myInput = new FileInputStream(fileName);
XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
List list = new ArrayList();
while(cellIter.hasNext()){
XSSFCell myCell = (XSSFCell) cellIter.next();
list.add(myCell);
}
cellVectorHolder.addElement(list);
}
}catch (Exception e){e.printStackTrace(); }
return cellVectorHolder;
}
I ended up using concentation and just inserted a , between each value in the for loop.
Using Apache POI, I am trying to read an excel file. The file has 1000 rows and 1 column. With this code:
XSSFSheet ws = workbook.getSheetAt(0);
Iterator< Row > rowIt = ws.iterator();
XSSFRow row;
int i = 0;
while ( rowIt.hasNext() ) {
row = (XSSFRow) rowIt.next();
Iterator< Cell > cellIt = row.cellIterator();
while ( cellIt.hasNext() ) {
Cell cell = cellIt.next();
my_array[ i ] = cell.getStringCellValue();
}
++i;
}
It seems that it reads 1001 rows and since the last row is "", my_array get invalid string. Is there any way to fix that? I expect rowIt.hasNext() is responsible for that but it doesn't work as expected.
The file has 1000 rows and 1 column : you must specify what column you are reading.
here an exemple that specify column with this excel file:
public class TestLecture {
public static void main(String[] args) throws IOException{
List<String> mys_list= new ArrayList<String>();
FileInputStream file = new FileInputStream(new File("test.xlsx"));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook (file);
//Get first sheet from the workbook
XSSFSheet ws = workbook.getSheetAt(0);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIt = ws.iterator();
while (rowIt.hasNext()) {
Row row = rowIt.next();
Iterator<Cell> cellIt = row.iterator();
while (cellIt.hasNext()) {
Cell cell = cellIt.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 2:
mys_list.add(cell.getStringCellValue());
break;
}
}
}
System.out.println(mys_list.size());
for(String g :mys_list){
System.out.println(g);
}
}
}
I have an app that reads an excel and gets the information. This is my code that reads the information from column 0 and column 1. I want to know a way to start only on row number 34 and skip row 1 from 33.
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
Iterator<Row> rowIter = sheet.rowIterator();
while(rowIter.hasNext()){
Row myRow =rowIter.next();
Cell cell1 = myRow.getCell(0);
Cell cell2 = myRow.getCell(1);
...
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
Cell myCell = cellIter.next();
}
//bd.addAnimalls(cell1.toString(),cell2.toString());
Toast.makeText(getApplicationContext(), "on inserting", Toast.LENGTH_SHORT).show();
}
You can get specific rows using sheet.getRow(rowNum).
So your code can be changed to this:
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
int rowCtr = 33;
Row myRow = sheet.getRow(rowCtr++);
while (myRow != null) {
Cell cell1 = myRow.getCell(0);
// rest of your loop code
myRow = sheet.getRow(rowCtr++);
}
I have two 'xlsx' files 'sss.xlsx' and 'abc.xlsx'.
In 'sss.xlsx' file there are some Headers (columns only, doesn't contain any data) and in 'abc.xlsx' file there is data.
I wants to copy data from 'abc' file to 'sss' file column by column by checking column names. If column name matches then copy that column data to "sss.xlsx"
In same column (keep column as it is given in sss file).
I tried with below code. I am able to write but while writing header gets deleted. I am not getting where I went wrong.
Code:
public class Read {
private static int flag;
public static void main(String[] args) throws IOException{
//Create blank workbook
HSSFWorkbook workbook1 = new HSSFWorkbook();
//Create a blank sheet
HSSFSheet spreadsheet = workbook1.createSheet(
" Info ");
ArrayList<String> lst = new ArrayList<String>();
Read read = new Read();
File f2 = new File("sss.xlsx");
FileInputStream ios2 = new FileInputStream(f2);
XSSFWorkbook workbook2 = new XSSFWorkbook(ios2);
XSSFSheet sheet2 = workbook2.getSheetAt(0);
int noOfColumns = sheet2.getRow(0).getLastCellNum();
System.out.println(noOfColumns);
//String temp2=f.getSheet();
Iterator<Row> rowIterator = sheet2.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator <Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
String temp2=cell1.getStringCellValue().toString();
System.out.print(cell1.getStringCellValue() + "\n");
lst = read.extractExcelContentByColumnIndex(temp2);
File f3 = new File("sss.xlsx");
FileInputStream ios = new FileInputStream(f3);
XSSFWorkbook workbook3 = new XSSFWorkbook(ios);
String temp1=f3.getName();
//String colName="Mob No";
XSSFSheet sheet3 = workbook3.getSheetAt(0);
//String temp2=f.getSheet();
int columnIndex1=0;
Iterator<Row> rowIterator3 = sheet3.iterator();
//columndata = new ArrayList<String>();
while (rowIterator3.hasNext()) {
Row row3 = rowIterator3.next();
Iterator<Cell> cellIterator3 = row3.cellIterator();
while (cellIterator3.hasNext()) {
Cell cell3 = cellIterator3.next();
String temp5=cell3.getStringCellValue().toString();
//System.out.println(temp);
if(temp5.equals(temp2)){
columnIndex1 = cell1.getColumnIndex();
System.out.println(columnIndex1);
flag=1;
break;
}
}
if(flag==1)
{
flag = 0;
break;
}
}
while (rowIterator.hasNext()) {
Row row6 = rowIterator.next();
// System.out.println(row.getRowNum());
Iterator<Cell> cellIterator6 = row6.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex1){// To match column index
// }
}
}
}
}
for(int RowNum=0; RowNum<lst.size();RowNum++){
HSSFRow row1 = spreadsheet.createRow(RowNum+1);
HSSFCell cell6 = row1.createCell(columnIndex1);
cell6.setCellValue(lst.get(RowNum).toString());
}
}
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("sss.xlsx"));
// System.out.println(lst);
workbook1.write(out);
out.close();
}
}
public ArrayList<String> extractExcelContentByColumnIndex( String colName)
{
ArrayList<String> columndata = null;
int columnIndex= 0;
int flag=0;
try {
File f = new File("abc.xlsx");
FileInputStream ios = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(ios);
String temp1=f.getName();
XSSFSheet sheet = workbook.getSheetAt(0);
//String temp2=f.getSheet();
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<String>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
String temp=cell1.getStringCellValue().toString();
//System.out.println(temp);
if(temp.equals(colName)){
columnIndex=cell1.getColumnIndex();
flag=1;
break;
}
}
if(flag==1)
{
flag = 0;
break;
}
}
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// System.out.println(row.getRowNum());
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > -1){ //To filter column headings
if(cell.getColumnIndex() == columnIndex){// To match column index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}
ios.close();
System.out.println(colName);
for(String ele : columndata)
{
System.out.println(ele);
}
} catch (Exception e) {
e.printStackTrace();
}
return columndata;
}
}
Thanks in advance!
I need to filter my excel spreadsheet for the word "GHH" anywhere in the text of a cell in a specific column. I have managed to do this by I then need to have returned the whole row that this text is found in. This I can't do as there doesnt seem to be a way of using the getRowIndex method to then display the whole row.
Here is my code:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("myfile.xls"));
HSSFWorkbook workBook = new HSSFWorkbook(fis);
HSSFSheet sheet = workBook.getSheetAt(0);
Iterator < Row > rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator < Cell > cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
if (cell.toString().contains("GHH")) {
String key = cell.getStringCellValue();
int RI = cell.getRowIndex();
}
}
}
workBook.close();
}
You could try to use a List<HSSFRow> to save filtered rows as bellow:
List<HSSFRow> filteredRows = new ArrayList<HSSFRow>();
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext ()){
HSSFRow row = (HSSFRow) rows.next ();
Iterator<Cell> cells = row.cellIterator ();
while (cells.hasNext ()){
HSSFCell cell = (HSSFCell) cells.next ();
if (cell.toString().contains("GHH")) {
String key = cell.getStringCellValue();
int RI=cell.getRowIndex();
filteredRows.add(row);
break;
}
}
// then use filteredRows
You probably want to have two bits of logic, one for handling a "matched" row, one for matching. Something like:
DataFormatter formatter = new DataFormatter();
public void matchingRow(Row row) {
System.out.println("Row " + (row.getRowNum()+1) + " matched:");
for (Cell c : row) {
System.out.println(" " + formatter.formatCellValue(cell));
}
}
public void handleFile(File excel) throws Exception {
Workbook wb = WorkbookFactory.create(excel);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
boolean matched = false;
for (Cell cell : row) {
if (matched) continue;
if (formatter.formatCellValue(cell).contains("GHH")) {
matchingRow(row);
matched = true;
}
}
}
}
That will check every cell in the first sheet, and if the text of a cell in a row matches GHH will then print out the row's contents. If a row has that in twice, it'll only print it once