im developing one tool for uploading excel and validate it, but excel file is large and the row count is about 65536.
here is my code used for uploading and reading the excel sheet values
int firstColNo = 1;
int rowcount = sheet.getRows();
int colCount = sheet.getColumns();
int row = 0;
String comp = "";
for (row = 1; row < rowcount; row++) {
if (labelCell != null) {
cell = sheet.getCell(firstColNo, row);
if (cell.getContents() != null && cell.getContents().length() > 0){
String compoundId = cell.getContents();
System.out.println(compoundId);
} else {
System.out.println("-");
}
}
}
by reading the row values it takes more to time to read, is there any way to make it faster else any code modifications need to be done in my code?
can anybody help me to overcome this issue.
Here is an example for you
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
Related
I am using this ...code but it will read index wise
FileInputStream fis = new FileInputStream(new File("PILOT.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
Iterator < Row > rowIterator = spreadsheet.iterator();
while (rowIterator.hasNext())
{
row = (XSSFRow) rowIterator.next();
Iterator < Cell > cellIterator = row.cellIterator();
while ( cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
}
// System.out.print(cell.getNumericCellValue() + " \t\t " );
break;
case Cell.CELL_TYPE_STRING:
System.out.print(
cell.getStringCellValue() + " \t\t " );
break;
}
}
System.out.println();
}
fis.close();
}
give any shortest way for read all the data at a time.....i am using maven POI dependency .....help me
You say the workbook has 5 sheets, but you are only reading one sheet in your code. Therefore in order to read the data from all the sheets in the workbook, you need to wrap your code in a loop. So where you currently have:
XSSFSheet spreadsheet = workbook.getSheetAt(0);
replace that with the loop below, and put the remaining code inside the loop.
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
XSSFSheet spreadsheet = workbook.getSheetAt(i);
// Your remaining code here
}
That will enable you to extract the data from all 5 sheets.
I am trying to read excel file cell by cell.The problem i am facing is ,my program skip one cell value while reading the excel file. Below is the code.
private void ReadExcel {
try
{
FileInputStream file = new FileInputStream(new File("C:\\Users\\Desktop\\abc.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Create a blank sheet
XSSFSheet spreadsheet = workbook.getSheetAt(0);
//Create row object
HashMap<Integer,List> hm = new HashMap<Integer,List>();
Integer index = 0;
//This data needs to be written (Object[])
Iterator<Row> rowIterator = spreadsheet.iterator();
while (rowIterator.hasNext())
{
List cellDataList = new ArrayList();
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
//System.out.println(cell.getDateCellValue());
cellDataList.add(cell.getDateCellValue());
} else {
//System.out.println(cell.getNumericCellValue());
cellDataList.add(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_STRING:
//System.out.print(cell.getStringCellValue() + "\t");
cellDataList.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
//System.out.println(cell.getStringCellValue() + "\t");
cellDataList.add(cell.getStringCellValue());
}
}
hm.put(index, cellDataList);
System.out.println("Size of hashmap is " +hm.size());
List al =hm.get(index);
System.out.println("List Size is " +al.size());
for(int i = 0;i<al.size();i++)
{
//System.out.println(" " +al.get(i));
}
System.out.println();
index++;
System.out.println("");
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Exception " +e);
}
}
Input data file i am passing to method is
I shared the input file screen shot. The problem i am facing is , i am able to read all cell value of first row and get the size of 4 but when i read second row , value of coloumn "D" is getting skip and getting size of 3. I dont why its skip the second row "D" Coloumn.
use normal for instead
do something like this
Row oRow = sheet.createRow(rowCounter);
for (short cell = 0; cell < row.getLastCellNum(); cell++) {
System.out.println("Reading Cell number: "+cell);
Cell cell0 = oRow.createCell(cell);
cell0.setCellValue(row.getCell(cell).toString());
}
This question already has answers here:
get number of columns of a particular row in given excel using Java
(3 answers)
Closed 8 years ago.
In my java application I am trying to convert excel to pdf using apache-poi.In my excel file,the number of columns are different in some rows.First and second rows contain one column,remaining rows contains 8 columns.Here is my code
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int cellNumber = 0;
if (flag) {
table = new PdfPTable(row.getLastCellNum());
flag = false;
}
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
if (temp == 0) {
numberOfColumns = row.getLastCellNum();
PdfPCell c1 = new PdfPCell(new Phrase(
cell.getStringCellValue()));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
}else{
cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
}
cellNumber++;
break;
case Cell.CELL_TYPE_NUMERIC:
cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
cellNumber++;
break;
}
}
temp = 1;
if(numberOfColumns != cellNumber){
for(int i=0;i<(numberOfColumns-cellNumber);i++){
table.addCell(" ");
}
}
}
So when I execute this,I will get the pdf file with only one column.But I want same as my excel format.How can I achieve this in my code dynamically(I have to apply this logic for other excel files also)?
You can use this approach to iterate each cell of every row .
Sheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if(isRowEmpty(row)){
System.out.println("Row "+row.getRowNum()+"is a empty row");
continue;
}
Iterator<Cell> cellIterator = row.cellIterator();
Cell cell =null;
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
String value = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
Double d = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
String value = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_BLANK:
break;
}
}
}
I need to read excel file which more than 300 rows. I need to extract value from particular cell only.
Code
Workbook workbook = Workbook.getWorkbook(new File(excelFile));
Sheet sheet = workbook.getSheet(0);
Cell emp_name = sheet.getCell(1,2);
Cell emp_dpt = sheet.getCell(2,2);
Cell emp_pdpt = sheet.getCell(3,2);
Cell emp_no = sheet.getCell(4,2);
Cell emp_desn = sheet.getCell(5,2);
Cell emp_dj = sheet.getCell(6,2);
Cell emp_lvl = sheet.getCell(7,2);
Cell emp_eval = sheet.getCell(8,2);
String name = emp_name.getContents();
String dpartment = emp_dpt.getContents();
String pre_department = emp_pdpt.getContents();
String employee_no = emp_no.getContents();
String designation = emp_desn.getContents();
String datejoined = emp_dj.getContents();
String evalution = emp_eval.getContents();
System.out.println(name);
System.out.println(dpartment);
System.out.println(pre_department);
System.out.println(employee_no);
System.out.println(designation);
System.out.println(datejoined);
System.out.println(evalution);
Above code helps me to fetch data from the excel but only one value extracted. How do I fetch all the data from excel.
Do it in a loop
When you say , sheet.getCell(1,2), you read the cell with column 1 and row 2.
Support if you want to read , column 1 and row 3, then do this sheet.getCell(1,3);
sheet.getRows() ->Returns the number of rows in this sheet
pseudo code:
for (int rowNum = 5; rowNum < sheet.getRows(); rowNum++) {
int column = 4;
sheet.getCell(column ++, rowNum).getContents(); //read 4th column and 5th row into a variable or object as per your logic;
sheet.getCell(column ++, rowNum).getContents(); //read 5th column and 5th row;
......
}
you can use iterator that allows you to read each and every cell
see example here
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
I'm having a problem in excel while using Apache POI. I can read across rows, but sometimes I'm in a situation where I would like to read a particular column only.
So is it possible to read any particular column like only the 'A' column only or the column 'C' only.
I'm using the Java language for this.
heikkim is right, here is some sample code adapted from some code I have:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
// break; ???
}
}
}
For the colCount use something like row.getPhysicalNumberOfCells()
Sheet sheet = workBook.getSheetAt(0); // Get Your Sheet.
for (Row row : sheet) { // For each Row.
Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
}
My solution, a bit simpler code wise.
Okay, from your question, you just simply want to read a particular column. So, while iterating over a row and then on its cells, your can simply check the index of the column.
Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next(); // For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
println "column index"+cell.getColumnIndex()//You will have your columns fixed in Excel file
if(cell.getColumnIndex()==3)//for example of c
{
print "done"
}
}
}
I am using POI 3.12-- 'org.apache.poi:poi:3.12'
Hope it helps. Cheers!
You could just loop the rows and read the same cell from each row (doesn't this comprise a column?).
import java.io.*;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.*;
import java.text.*;
public class XSLXReader {
static DecimalFormat df = new DecimalFormat("#####0");
public static void main(String[] args) {
FileWriter fostream;
PrintWriter out = null;
String strOutputPath = "H:\\BLR_Team\\Kavitha\\Excel-to-xml\\";
String strFilePrefix = "Master_5.2-B";
try {
InputStream inputStream = new FileInputStream(new File("H:\\BLR_Team\\Kavitha\\Excel-to-xml\\Stack-up 20L pure storage 11-0039-01 ISU_USA-A 1-30-17-Rev_exm.xls"));
Workbook wb = WorkbookFactory.create(inputStream);
// Sheet sheet = wb.getSheet(0);
Sheet sheet =null;
Integer noOfSheets= wb.getNumberOfSheets();
for(int i=0;i<noOfSheets;i++){
sheet = wb.getSheetAt(i);
System.out.println("Sheet : "+i + " " + sheet.getSheetName());
System.out.println("Sheet : "+i + " " + sheet.getFirstRowNum());
System.out.println("Sheet : "+i + " " + sheet.getLastRowNum());
//Column 29
fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+i+ ".xml");
out = new PrintWriter(new BufferedWriter(fostream));
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<Bin-code>");
boolean firstRow = true;
for (Row row : sheet) {
if (firstRow == true) {
firstRow = false;
continue;
}
out.println("\t<DCT>");
out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0))));
out.println(formatElement("\t\t", "Table_name", formatCell(row.getCell(1))));
out.println(formatElement("\t\t", "isProddaten", formatCell(row.getCell(2))));
out.println(formatElement("\t\t", "isR3P01Data", formatCell(row.getCell(3))));
out.println(formatElement("\t\t", "LayerNo", formatCell(row.getCell(29))));
out.println("\t</DCT>");
}
CellReference ref = new CellReference("A13");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
Cell c = r.getCell(ref.getCol());
System.out.println(c.getRichStringCellValue().getString());
}
for (Row row : sheet) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
System.out.println();
break;
default:
System.out.println();
}
}
}
out.write("</Bin-code>");
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String formatCell(Cell cell)
{
if (cell == null) {
return "";
}
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
return "";
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR:
return "*error*";
case Cell.CELL_TYPE_NUMERIC:
return XSLXReader.df.format(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "<unknown value>";
}
}
private static String formatElement(String prefix, String tag, String value) {
StringBuilder sb = new StringBuilder(prefix);
sb.append("<");
sb.append(tag);
if (value != null && value.length() > 0) {
sb.append(">");
sb.append(value);
sb.append("</");
sb.append(tag);
sb.append(">");
} else {
sb.append("/>");
}
return sb.toString();
}
}
This code does 3 things:
Excel to XML file generation. Eng. Name Dong Kim
Prints the content of a particular cell : A13
Also print the excel content into normal text format. Jars to be imported: poi-3.9.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar,xbean-2.3.0.jar,xmlbeans-xmlpublic-2.4.0.jar,dom4j-1.5.jar
Here is the code to read the excel data by column.
public ArrayList<String> extractExcelContentByColumnIndex(int columnIndex){
ArrayList<String> columndata = null;
try {
File f = new File("sample.xlsx")
FileInputStream ios = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(ios);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > 0){ //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(columndata);
} catch (Exception e) {
e.printStackTrace();
}
return columndata;
}
Please be aware, that iterating through the columns using row cell iterator ( Iterator<Cell> cellIterator = row.cellIterator();) may lead to silent skipping columns. I have just encountered a document that was exposing such behaviour.
Iterating using indexes in a for loop and using row.getCell(i) was not skipping columns and was returning values at the correct column indexes.