0

I am begginer java developer and I use spring mvc framework I want to export data from jsp page (that was sent from controler) I work according to this tutorial http://www.codejava.net/frameworks/spring/spring-mvc-with-excel-view-example-apache-poi-and-jexcelapi

but i get this error:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'excelView': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Font

when i want to use the "excel lib" poi-3.9.jar and put it as dependency in pom.xml

When i do "maven install" i got this error:

Failed to execute goal on project SpringMvcJdbcTemplate: Could not resolve dependencies for project net.codejava.spring:SpringMvcJdbcTemplate:war:1.0: Failed to collect dependencies at org.apache.poi:poi:jar:3.9: Failed to read artifact descriptor for org.apache.poi:poi:jar:3.9: Could not transfer artifact

org.apache.poi:poi:pom:3.9 from/to central 
(https://repo.maven.apache.org/maven2): 
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]

so i add the library in regular way and its seems good but when i run the app i got the error i put in the begining

This is my code:

controler:

@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
    // create some sample data
   // return a view which will be resolved by an excel view resolver
    return new ModelAndView("excelView", "listContactings", listContact);
}

MvcConfigiration:

@Bean
    public ViewResolver getViewResolver(){
        ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();
        resolver.setOrder(1);
        resolver.setBasename("views");
        return resolver;
    }
    @Bean
    public ViewResolver getViewResolver2(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setOrder(2);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

views.proprties:

excelView.(class)=net.codejava.spring.ExcelBuilder

ExcelBuilder.java:

package net.codejava.spring;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import net.codejava.spring.model.Contacting;

public class ExcelBuilder extends AbstractExcelView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // get data model which is passed by the Spring container
        List<Contacting> listContactings = (List<Contacting>) model.get("listContactings");

        // create a new Excel sheet
        HSSFSheet sheet = workbook.createSheet("Java Books");
        sheet.setDefaultColumnWidth(30);

        // create style for header cells
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("Arial");
        style.setFillForegroundColor(HSSFColor.BLUE.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
       font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.WHITE.index);
        style.setFont(font);

        // create header row

        HSSFRow header = sheet.createRow(0);

        header.createCell(0).setCellValue("contacting_id");
        header.getCell(0).setCellStyle(style);

        header.createCell(1).setCellValue("user_id");
        header.getCell(1).setCellStyle(style);

        header.createCell(2).setCellValue("subject");
        header.getCell(2).setCellStyle(style);

        header.createCell(3).setCellValue("location");
        header.getCell(3).setCellStyle(style);

        header.createCell(4).setCellValue("content");
        header.getCell(4).setCellStyle(style);

        header.createCell(5).setCellValue("department");
        header.getCell(5).setCellStyle(style);



        header.createCell(6).setCellValue("status");
        header.getCell(6).setCellStyle(style);




        header.createCell(7).setCellValue("contacting_date");
        header.getCell(7).setCellStyle(style);


        header.createCell(8).setCellValue("house_Number");
        header.getCell(8).setCellStyle(style);

        header.createCell(9).setCellValue("Urgency");
        header.getCell(9).setCellStyle(style);

        header.createCell(10).setCellValue("is_inspector");
        header.getCell(10).setCellStyle(style);

        header.createCell(11).setCellValue("inspectorStatus");
        header.getCell(11).setCellStyle(style);



        header.createCell(12).setCellValue("stringDate");
        header.getCell(12).setCellStyle(style);

        header.createCell(13).setCellValue("openedBy");
        header.getCell(13).setCellStyle(style);

        // create data rows
        int rowCount = 1;

        for (Contacting Contacting : listContactings) {
            HSSFRow aRow = sheet.createRow(rowCount++);
            aRow.createCell(0).setCellValue(Contacting.getContacting_id());
            aRow.createCell(1).setCellValue(Contacting.getUser_id());
            aRow.createCell(2).setCellValue(Contacting.getSubject());
            aRow.createCell(3).setCellValue(Contacting.getLocation());
            aRow.createCell(4).setCellValue(Contacting.getContent());
            aRow.createCell(5).setCellValue(Contacting.getdepartment());
            aRow.createCell(6).setCellValue(Contacting.getStatus());
            aRow.createCell(7).setCellValue(Contacting.getContacting_date());
            aRow.createCell(8).setCellValue(Contacting.getHouse_Number());
            aRow.createCell(9).setCellValue(Contacting.getUrgency());
            aRow.createCell(10).setCellValue(Contacting.getIs_inspector());
            aRow.createCell(11).setCellValue(Contacting.getInspectorStatus());
            aRow.createCell(12).setCellValue(Contacting.getStringDate());
            aRow.createCell(13).setCellValue(Contacting.getOpenedBy());



        }
    }
}

I think that the problem in the library using but i try many versions and many ways to use it but i always get errors.

someone can help me?

user5993555
  • 81
  • 3
  • 12

1 Answers1

0

try to use this dependency and execute

mvn clean install

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>

And i highly recommend that when you start to use maven as dependency resolver, continue using it and don't mix libraries from your own and maven, because you will find problems in the future.

Try to execute with that version, and we will see the exception, maybe we need to exclude some jar from the poi dependency because is already included

cralfaro
  • 5,822
  • 3
  • 20
  • 30
  • I try it first remove the library and than paste this but i do maven clean and after maven install and get the error again – user5993555 May 10 '16 at 09:37
  • @user5993555 where is the error? in your original question? – cralfaro May 10 '16 at 09:38
  • Failed to execute goal on project SpringMvcJdbcTemplate: Could not resolve dependencies for project net.codejava.spring:SpringMvcJdbcTemplate:war:1.0: Failed to collect dependencies at org.apache.poi:poi:jar:3.9: Failed to read artifact descriptor for org.apache.poi:poi:jar:3.9: Could not transfer artifact org.apache.poi:poi:pom:3.9 from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1] – user5993555 May 10 '16 at 09:40
  • @user5993555 I think your problem is just related with your LAN connection, do you have some proxy? firewall? try also to comment all repositories you have defined into your pom.xml. Also have a look to the settings.xml maybe you have some net restriction. Have a look here http://stackoverflow.com/questions/15334394/could-not-transfer-artifact-org-apache-maven-pluginsmaven-surefire-pluginpom2 – cralfaro May 10 '16 at 09:51
  • Error reading file C:\Users\LA\.m2\repository\org\apache\poi\poi\3.9\poi-3.9.jar C:\Users\LA\.m2\repository\org\apache\poi\poi\3.9\poi-3.9.jar (‏‏למערכת אין אפשרות לאתר את הקובץ שצוין) – user5993555 May 10 '16 at 09:52
  • Thats because you are not able to download the jar from maven repositories, check pom.xml and settings.xml file – cralfaro May 10 '16 at 09:53
  • i dont have settings.xml i just have web.xml but i have firewall what i have to check and where? – user5993555 May 10 '16 at 10:23
  • C:\Users\LA\.m2\settings.xml here is your settings of maven. – cralfaro May 10 '16 at 10:30
  • If you use maven you have this file, try to find it with the file search engine, the name is "settings.xml" – cralfaro May 10 '16 at 10:56
  • I dont find it i find just maven-settings file but with jar or pom end – user5993555 May 10 '16 at 11:26
  • If you are using maven, you HAVE this file, maybe is hidden by your OS as hidden files, try to be sure all files are visible, is an option for folders view. But trust me, you have this file, and if you deleted you will have to create again or reinstall maven. – cralfaro May 10 '16 at 11:28
  • i still dont find it can you explain me how to do it – user5993555 May 10 '16 at 12:25
  • you can follow this guide, https://maven.apache.org/guides/mini/guide-configuring-maven.html, is quite complete and original from maven – cralfaro May 10 '16 at 12:30