I have a Excel file in .xlsx format. I have stored data by merging cells to form various columns. I am reading the Excel file via a Java web application and saving its data to a database (MySQL). But when I read from merged cells I get null values along with what are stored in the columns as well as the headers. I am using Apache POI. My code is:
public static void excelToDBLogIN() {
FileInputStream file = null;
Boolean flag = true;
ArrayList<String> rows = new ArrayList<String>();
try {
// here uploadFolder contains the path to the Login 3.xlsx file
file = new FileInputStream(new File(uploadFolder + "Login 3.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
String tuple = "";
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
//int value = new BigDecimal(cell.getNumericCellValue()).setScale(0, RoundingMode.HALF_UP).intValue();
//tuple = tuple + String.valueOf(value) + "+";
DataFormatter objDefaultFormat = new DataFormatter();
String str = objDefaultFormat.formatCellValue(cell);
tuple = tuple + str + "+";
break;
case Cell.CELL_TYPE_STRING:
tuple = tuple + cell.getStringCellValue() + "+";
break;
case Cell.CELL_TYPE_BLANK:
tuple = tuple + "" + "+";
break;
}
}
rows.add(tuple);
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
file = null;
} catch (Exception e) {
System.out.println("File closing operation failed");
e.printStackTrace();
}
}
}
}
}
I searched for answers in the web but did not find anything relevant.
Following code of snippet might help.
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
outer:
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//will iterate over the Merged cells
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress region = sheet.getMergedRegion(i); //Region of merged cells
int colIndex = region.getFirstColumn(); //number of columns merged
int rowNum = region.getFirstRow(); //number of rows merged
//check first cell of the region
if (rowNum == cell.getRowIndex() && colIndex == cell.getColumnIndex()) {
System.out.println(sheet.getRow(rowNum).getCell(colIndex).getStringCellValue());
continue outer;
}
}
//the data in merge cells is always present on the first cell. All other cells(in merged region) are considered blank
if (cell.getCellType() == Cell.CELL_TYPE_BLANK || cell == null) {
continue;
}
System.out.println(cell.getStringCellValue());
}
}
This method can read a specific cell (including merged cell):
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public static void readCell(String excelFilePath, int rowIndex, int columnIndex) throws FileNotFoundException, IOException {
try (InputStream inp = new FileInputStream(excelFilePath)) {
XSSFWorkbook wb = new XSSFWorkbook(inp);
XSSFCell cell = wb.getSheetAt(0).getRow(rowIndex).getCell(columnIndex);
switch (cell.getCellType()) {
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(cell.getCellFormula());
break;
case BLANK:
System.out.println();
break;
default:
System.out.println();
}
wb.close();
}
}
Dependencies: POI 5.0.0, JDK 1.8.0
I have the excel file the code works fine, but how can I get each column and row value differently so that I can store the value in database. Thank you in advance
public class excel_demo {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("C:\\Users\\Admin\\Downloads\\ExcelDemosWithPOI\\howtodoinjava_demo.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
download JEXCEL api,and use this code,
import jxl.*;//import jxl package.
File excelSheet = null;
Workbook workbook = null;
Workbook wb = Workbook.getWorkbook(new File(destFile));//destFile is excel file
Sheet sheet = wb.getSheet(sheetNo);
columns = sheet.getColumns();
rows = sheet.getRows();
for(int row = 0;row <rows;row++)
{
for(int col =0;col <columns;col++)
{
a[row][col] =Integer.parseInt( sheet.getCell(col,row).getContents());
}
}
Hope this helps..
My question is based on this Question. This is my build so far:
package converters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class XlsxtoCSV {
public static void main(String[] args) {
File inputFile = new File(
"C:\\Users\\USR\\Desktop\\testExport.xlsx");
File outputFile = new File("C:\\Users\\USR\\Desktop\\output.csv");
xlsx(inputFile, outputFile);
}
static void xlsx(File inputFile, File outputFile) {
String seperator = ";";
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(
new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
int i = 0; //I need 11 columns which can be empty
while (i < 11) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + seperator);
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + seperator);
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + seperator);
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + seperator);
break;
default:
data.append(cell + seperator);
}
}
data.append("\r\n");
i++;
}
fos.write(data.toString().getBytes());
fos.close();
System.out.println(data.toString());
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
but my problem is that it won't work properly if there is an line break in a cell, which is important for me.
I need it to be exactly like when you "save as csv" in Excel.
I just tried to get each cell values from excel file but i get each cell already merged but i want each cell different
import java.io.File;import java.io.FileInputStream;import java.util.Iterator;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Read {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
FileInputStream file = new FileInputStream(new File("D://new/excelnew/student_usr_mst_dtls.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
}
}
System.out.println("");
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
and OutPut is like as string
IDNAMELASTNAME
1.0tAmitShukla
2.0tLokeshGupta
I want each cell uniquely so how to do it. if possible give an example please.
Put a comma after each cell value you print.
System.out.print(cell.getNumericCellValue() + "t,");
and
System.out.print(cell.getStringCellValue() + ",");
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.