Reading 2D double array in Excel Using Apache POI - java

I am trying to make use of Apache POI to read an excel file and convert it to a 2-dimensional object array. Attached is the code section.
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(1);
int numRows = 3;
int cols = 5;
double[][] excelData = new double[numRows][cols];
for (int i = 1; i < numRows; i++) {
Row row = firstSheet.getRow(i);
if (row != null) {
for (int j = 1; j < cols; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
try {
excelData[i][j] = cell.getNumericCellValue();
} catch (IllegalStateException e) {
System.out.println("Cell data is not a double");
}
}
}
}
}
workbook.close();
inputStream.close();
return excelData;
}
enter image description here This is my excel sheet and I want to "just" read the blue part of it as a 2d array, but after running the code the first row and column all are zero and I don't want it, appreciate your help on how can quickly pull out all the zeros enter image description here.

You need to change index in the 1st loop to 0 (first field in excel file)
but in row you must provide index + 1 bc you need to read from the 2nd field.
Same analogy for 2nd loop and cell field
Workbook workbook = new XSSFWorkbook(new File("d:\\book1.xlsx"));
Sheet firstSheet = workbook.getSheetAt(1);
int numRows = 3;
int cols = 5;
double[][] excelData = new double[numRows][cols];
for (int i = 0; i < numRows; i++) {
Row row = firstSheet.getRow(i + 1);
if (row != null) {
for (int j = 0; j < cols; j++) {
Cell cell = row.getCell(j + 1);
if (cell != null) {
try {
if (cell.getCellType().equals(CellType.NUMERIC)) {
excelData[i][j] = Double.valueOf(cell.getNumericCellValue());
}
} catch (IllegalStateException e) {
System.out.println("Cell data is not a double");
}
}
}
}
}
workbook.close();
Returned result for it:
0.041256 0.033079 -0.01138 -0.02138 -0.01138
0.041256 0.033079 -0.01138 -0.01138 -0.01138
0.020628 0.01654 -0.00569 -0.10569 -0.00569

Related

Read merge cell in excel using Apache POI with Spring

I am building a function that reads the data available in the excel file.
I use Apache POI with the Spring project.
However, I am having trouble reading with merge cells. What I need is to get the content highlighted in red as shown in the picture.
Can you suggest me the code?
Thanks !
Update:
`
public void importClassroomByExcel(MultipartFile file) {
try {
Workbook workbook = new HSSFWorkbook(file.getInputStream());
Iterator<Sheet> sheetIterator = workbook.iterator();
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
String title = sheet.getRow(4).getCell(5).toString();
}
workbook.close();
} catch (Exception e) {
throw new BadRequestAlertException(ErrorConstants.DEFAULT_TYPE, "", e.getMessage(), "filestructerror",
Status.BAD_REQUEST);
}
}
`
Not sure I understand your requirement correct. This is what I think you want: In your sheet, the data range starts in row 5 and column A determines the grouping. Now the need is getting the values of columns F and G for each group. Then the need is getting the first date for each group in column N and the last date for each group in column O.
So the need is to get the first row for each group and the last filled row for each group. Following example shows this. Mainly it uses a method int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum) to get the next filled row in a given column of the sheet starting on a given row number.
The code should only get the cells having the red marked contents in your given sheet.
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
class ReadExcelMergedCells {
static int getNextFilledRow(Sheet sheet, int columnNum, int startRowNum) {
int lastRow = sheet.getLastRowNum();
int nextFilledRow = lastRow;
for (int r = startRowNum; r <= lastRow; r++) {
nextFilledRow = r + 1;
Row row = sheet.getRow(nextFilledRow);
Cell cell = (row != null)?cell = row.getCell(columnNum):null;
if (cell != null && cell.getCellType() != CellType.BLANK) {
break;
}
}
return nextFilledRow;
}
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
int lastRow = sheet.getLastRowNum();
int startRow = 4;
Row row = null;
Cell cell = null;
int columnNum = -1;
int columNumA = 0; //column A determines the grouping
for (int r = startRow; r <= lastRow; r = getNextFilledRow(sheet, columNumA, r)) {
//r is the first row in this group
System.out.println("Group starting in row " + r);
//get cell in column F of first row in this group
columnNum = 5;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column G of first row in this group
columnNum = 6;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column N of first row in this group
columnNum = 13;
row = sheet.getRow(r);
cell = (row != null)?cell = row.getCell(columnNum):null;
System.out.println(cell);
//get cell in column O of last filled row in this group
columnNum = 14;
int lastRowInGroup = getNextFilledRow(sheet, columNumA, r) -1;
// get last filled cell in this group
for (int rl = lastRowInGroup; rl >= r; rl--) {
row = sheet.getRow(rl);
cell = (row != null)?cell = row.getCell(columnNum):null;
if (cell != null && cell.getCellType() != CellType.BLANK) {
break;
}
}
System.out.println(cell);
System.out.println("Group ending in row " + lastRowInGroup);
}
workbook.close();
}
}
Code to change color of cell to red:
CellStyle cellStyle = cell.getCellStyle();
Font font = workbook.createFont();
font.setFontName("Times New Roman");
font.setColor(IndexedColors.RED.index);
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.index);
cell.setCellStyle(cellStyle);
To merge from B2 to E2:
sheet.addMergedRegion(new CellRangeAddress(1,1,1,4));
See more in :
Changing cell color using apache poi
How to get the font style of a cell field of excel in Java, Apache POI?
Using custom colors with SXSSF (Apache POI)
Merging cells in Excel using Apache POI

Is there a way to check excel sheet has rows null in POI java library?

I could not find any method to check POI sheet has null rows.
I have tried, Sheet.getLastRowNum(),Sheet.getFirstRowNum(),Sheet.rowIterator(), they all issue NPE.
I am really really confusing right now.
Note: But Sheet object itself is not null though.
int numberOfSheets = wb.getNumberOfSheets();
Sheet sheet;
int maxColumnsCount = 0, headerRowIndex = 0;
Row row;
Member member;
List<Member> members = new ArrayList<>();
for (int i = 0; i < numberOfSheets; i++) {
sheet = wb.getSheetAt(i);
//find the maximum cells in a row, it is assumed that max cells container is only valid data. Otherwise Header or Footer.
//THISE LINE('sheet.getPhysicalNumberOfRows()') GIVE ME NULL POINTER EXCEPTION.
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (maxColumnsCount < row.getPhysicalNumberOfCells()) {
maxColumnsCount = row.getPhysicalNumberOfCells();
headerRowIndex = rowIndex;
} else {
break;
}
}
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (headerRowIndex < rowIndex) {
member = parseMember(row, memberExcelImportType);
member.getTownship().setId(townshipId);
if (StringUtils.isNotEmpty(member.getFullName()))
members.add(member);
}
}
}
Sorry, here's the stack trace.
java.lang.NullPointerException
at org.apache.poi.xssf.usermodel.XSSFSheet.getPhysicalNumberOfRows(XSSFSheet.java:842)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtilPOI.readExcelFileUsingPOI(ImportUtilPOI.java:45)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtils.readExcelFile(ImportUtils.java:32)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.doPersonImport(PersonExportImportHttpServe.java:153)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.serve(PersonExportImportHttpServe.java:86)
at com.taraaung.taraserver.server.MyServer$ValidateSession.serve(MyServer.java:2608)
at com.taraaung.taraserver.server.MyServer.serve(MyServer.java:770)
at com.taraaung.taraserver.server.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:1557)
at com.taraaung.taraserver.server.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:1152)
at java.lang.Thread.run(Thread.java:745)
Umm, just found that the sheet which issues NPE is ChartSheet (i.e, it does not have any kind of rows), then I just check the instance of sheet and skip chart sheets. :D if (sheet instanceof XSSFChartSheet) {
continue;
}
'Thanks for the help'

How to do cell iteration of excel in java

I am having an excel with 2 rows and 5 columns. Now I entered code manually to take values from 1st row. How can I iterate the process?
Below is the code for 1st row in excel. From the 2nd row on, I dont know how to do... I want to iterate one row after another.
Workbook workbook = Workbook.getWorkbook(new File(
"\\C:\\users\\a-4935\\Desktop\\DataPool_CA.xls"));
Sheet sheet = workbook.getSheet("Sheet1");
System.out.println("Reached to Sheet");
Cell a = sheet.getCell(2,1);
Cell b = sheet.getCell(3,1);
Cell c = sheet.getCell(4,1);
Cell d = sheet.getCell(5,1);
Cell e = sheet.getCell(6,1);
Cell f = sheet.getCell(7,1);
Cell g = sheet.getCell(8,1);
Cell h = sheet.getCell(9,1);
Cell i = sheet.getCell(10,1);
String uId = a.getContents();
String deptFromDat = b.getContents();
String deptToDate = c.getContents();
String dept1 = d.getContents();
String arrival1 = e.getContents();
String eihon1 = f.getContents();
String branchCode1 = g.getContents();
String userType1 = h.getContents();
String sessionId1 = i.getContents();
Use the code below to iterate over all rows of a datasheet:
Sheet sheet = workbook.getSheet("Sheet1");
for (Row row : sheet) {
for (Cell cell : row) {
//your logic
}
}
Or, alternatively, use the following code:
Sheet sheet = workbook.getSheet("Sheet1");
for (int i = 0; i < 2; i++) {
Row row = sheet.getRow(i);
if(row == null) {
//do something with an empty row
continue;
}
for (int j = 0; j < 5; j++) {
Cell cell = row.getCell(j);
if(cell == null) {
//do something with an empty cell
continue;
}
//your logic
}
}
for (int i = 0; i <= sheet.getLastRowNum (); ++i) {
row=(Row) sheet.getRow(i);
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext())
{ .....
you can use the above code to iterate all rows and all cells
You just need to change 1 to the row number, and use a for loop.
Based on your code you could do the following e.g.
For exmaple:
for(int i=0; i<number; i++)
{
Cell a = sheet.getCell(2,i);
Cell b = sheet.getCell(3,i);
Cell c = sheet.getCell(4,i);
...
}
Or better yet move the Cell declarations out of the for loop which is better practice.
Cell a,b, c...;
for(int i=0; i<number; i++)
{
a = sheet.getCell(2,i);
b = sheet.getCell(3,i);
c = sheet.getCell(4,i);
...
}

fastExcel:a column missing when reading

fastexcel is the newest.
create a xsl file first,then go to read it,the results showed that the last column lost.However, when I saved the XLS documents again,the last column is not last when reading.
Generated code:
java code
Workbook workBook;
workBook = FastExcel.createWriteableWorkbook(xls);
workBook.open();
int rowsLength = rows.length;
int sheets = rowsLength / sheetRows
+ (rowsLength % sheetRows > 0 ? 1 : 0);
int sheetNumber;
for (int i = 0; i < sheets; i++) {
sheetNumber = i + 1;
Sheet sheet = workBook.addStreamSheet(sheetName + sheetNumber);
// sheet.addRow(titles);
for (int j = i * sheetRows; j < rowsLength; j++) {
sheet.addRow(rows[j]);
if (j >= sheetRows * sheetNumber) {
break;
}
}
}
workBook.close();
code by read
java code
Workbook workBook;
workBook = FastExcel.createReadableWorkbook(xls);
workBook.setSSTType(BIFFSetting.SST_TYPE_DEFAULT);
workBook.open();
Sheet s;
s = workBook.getSheet(0);
excelContent = new String[s.getLastRow()-startRow][];
for (int i = startRow; i < s.getLastRow(); i++) {
excelContent[i-startRow] = s.getRow(i);
}
workBook.close();
u may change
if (j >= sheetRows * sheetNumber) {
break;
}
to
if (j >sheetRows * sheetNumber) {
break;
}

Excel is missing some values while writing

I am new to Apache-poi and using 3.8 version of poi. While writing value in the Excel,i check for the column names if they matched, i will write some value in that column and finish it, and again i will start writing on header. The problem is if write three column values only last column values are add or saved and first two values are not saved in the column. Can anyone tell what went wrong.
(sorry, in case any mistake)
Code:
int i = 0;
Row row = sheet.createRow(i);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
String validate_header = null;
while (iterator.hasNext()) {
if (eiterator.hasNext()) {
validate_header = eiterator.next();
}
Cell cell = row.createCell(i);
String col_heading = iterator.next();
cell.setCellValue(col_heading);
if(col_heading.equalsIgnoreCase("Assigned Date"))
{
Add_value(i, col_heading, row, sheet);
row=sheet.getRow(0);
cell=row.getCell(i);
}
else if(col_heading.startsWith("Review"))
{
int count=-1;
int n=Col_values.get("Defect Summary").size();
for (int j = 0; j < n; j++) {
row = sheet.createRow(count);
cell = row.createCell(i);
String s="External QC Defect ";
cell.setCellValue(s);
count++;
}
row=sheet.getRow(0);
cell=row.getCell(i);
}
sheet.autoSizeColumn(i);
i++;
}
private static Sheet Add_value(int k,String name,Row row, Sheet sheet) {
System.out.println("Inside the add method");
if(name.equalsIgnoreCase("Assigned Date")||name.equalsIgnoreCase("Reported Date") )
{
vector = Col_values.get("TargetDate");
int count = 1;
System.out.println("IF Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
else
{
vector = Col_values.get("Defect Summary");
int count = 1;
System.out.println("ELSE Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
return sheet;
}
'
Can u tell what went Wrong?
It seems that Add_value starts creating rows from top. Therefore on the second call the old rows are removed.
Replace
row = sheet.createRow(count);
with
row = k == 0 ? sheet.createRow(count) : sheet.getRow(count);

Categories