Read Excel file from Java Tomcat

1

I'm running a web application with Java and using Tomcat 8.5 to run it locally. My application has to read an Excel file and save the data in variables. I use the POI library, and when I execute the code without going through the server from the main method, it reads it perfectly, but when I do it through a servlet throw an exception.

This is the code that works:

package excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class LeerXLSX {
    /*public LeerXLSX(){

}*/

public LeerXLSX(File fileName) throws ParseException{
    if (fileName.exists()) {
        System.out.println("Existe");
    }
    ArrayList cellDataList = new ArrayList();
    try{
        FileInputStream fileInputStream = new FileInputStream(fileName);
        XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
        XSSFSheet hssfSheet = workBook.getSheetAt(0);
        Iterator rowIterator = hssfSheet.rowIterator();
        while (rowIterator.hasNext()){
            XSSFRow hssfRow = (XSSFRow) rowIterator.next();
            Iterator iterator = hssfRow.cellIterator();
            ArrayList cellTempList = new ArrayList();
            while (iterator.hasNext()){
                XSSFCell hssfCell = (XSSFCell) iterator.next();
                cellTempList.add(hssfCell);
            }
            cellDataList.add(cellTempList);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    Leer(cellDataList);
}

private void Leer(ArrayList cellDataList) {
    int nFilas = cellDataList.size();
    ArrayList<String> fila[] = new ArrayList[nFilas];

    for (int i = 0; i < cellDataList.size(); i++) {
        ArrayList cellTempList = (ArrayList) cellDataList.get(i);
        fila[i]= new ArrayList();
        for (int j = 0; j < cellTempList.size(); j++) {
            XSSFCell hssfCell = (XSSFCell) cellTempList.get(j);
            String stringCellValue = hssfCell.toString();
            //System.out.print(stringCellValue + " ");
            fila[i].add(stringCellValue);
        }
        System.out.println("fila" + i +fila[i]);
    }

}

public static void main(String[] args) throws ParseException {
        File f = new File("test.xlsx");
        if (f.exists()) {
            LeerXLSX pb = new LeerXLSX(f);
        }
    }
}

When I run it through Tomcat I simply remove the main and call it from the servlet .

package excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class LeerXLSX {
    /*public LeerXLSX(){

    }*/

    public LeerXLSX(File fileName) throws ParseException{
        if (fileName.exists()) {
            System.out.println("Existe");
        }
        ArrayList cellDataList = new ArrayList();
        try{
            FileInputStream fileInputStream = new FileInputStream(fileName);
            XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
            XSSFSheet hssfSheet = workBook.getSheetAt(0);
            Iterator rowIterator = hssfSheet.rowIterator();
            while (rowIterator.hasNext()){
                XSSFRow hssfRow = (XSSFRow) rowIterator.next();
                Iterator iterator = hssfRow.cellIterator();
                ArrayList cellTempList = new ArrayList();
                while (iterator.hasNext()){
                    XSSFCell hssfCell = (XSSFCell) iterator.next();
                    cellTempList.add(hssfCell);
                }
                cellDataList.add(cellTempList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Leer(cellDataList);
    }

    private void Leer(ArrayList cellDataList) {
        int nFilas = cellDataList.size();
        ArrayList<String> fila[] = new ArrayList[nFilas];

        for (int i = 0; i < cellDataList.size(); i++) {
            ArrayList cellTempList = (ArrayList) cellDataList.get(i);
            fila[i]= new ArrayList();
            for (int j = 0; j < cellTempList.size(); j++) {
                XSSFCell hssfCell = (XSSFCell) cellTempList.get(j);
                String stringCellValue = hssfCell.toString();
                //System.out.print(stringCellValue + " ");
                fila[i].add(stringCellValue);
            }
            System.out.println("fila" + i +fila[i]);
        }

    }

    /*public static void main(String[] args) throws ParseException {
        File f = new File("test.xlsx");
        if (f.exists()) {
            LeerXLSX pb = new LeerXLSX(f);
        }
    }*/
}

... and the servlet :

package controlador;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletExcel
 */
@WebServlet("/ServletExcel")
public class ServletExcel extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletExcel() {
        super();

        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File f = new File("C:/Users/CrisH/workspace/Proyectos/test.xlsx");
        try {
            LeerXLSX l = new LeerXLSX(f);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

The error it sends me is:

GRAVE: El Servlet.service() para el servlet [controlador.ServletExcel] en el contexto con ruta [/Proyectos] lanzó la excepción [La ejecución del Servlet lanzó una excepción] con causa raíz
java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at excel.LeerXLSX.<init>(LeerXLSX.java:35)
    at controlador.ServletExcel.doGet(ServletExcel.java:43)

Debuying in XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream); I skip class ApplicationFilterChain.class :

  

Class File Editor
  Source not found
  The source attachment does not contain the source for the file ApplicationFilterChain.class .
  You can change the source attachment by clicking Change Attached Source below:

Previously I skipped the library cataliana.jar , downloaded it and put it back into the project.

    
asked by sally 17.05.2017 в 10:15
source

1 answer

1

You have to have the library added to the lib folder, within WEB-INF of your project. It says that you can not find the class org.apache.poi.xssf.usermodel.XSSFWorkbook if it is that way and even then it does not work for you, try adding .jar to the libraries of tomcat

    
answered by 17.05.2017 в 10:55