简体   繁体   English

使用Spring MVC将数据导出到Excel文件

[英]Exporting data to excel file using spring mvc

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 我是初学者Java开发人员,我使用spring mvc框架,我想从jsp页面导出数据(从控制程序发送),我根据本教程http://www.codejava.net/frameworks/spring/spring-mvc-用Excel查看示例Apache POI和jexcelapi

but i get this error: 但我得到这个错误:

HTTP Status 500 - Request processing failed; nested exception is 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 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 with name 'excelView': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Font 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 当我想使用“ excel lib” poi-3.9.jar并将其作为依赖项放在pom.xml中时

When i do "maven install" i got this error: 当我执行“ Maven安装”时,出现此错误:

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 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 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: 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: views.properties:

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

ExcelBuilder.java: 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? 有人可以帮助我吗?

try to use this dependency and execute 尝试使用此依赖关系并执行

mvn clean install mvn全新安装

<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. 并且我强烈建议您开始使用maven作为依赖项解析器时,请继续使用它,不要将自己和maven中的库混在一起,因为将来您会发现问题。

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 尝试使用该版本执行,我们将看到异常,也许我们需要从poi依赖项中排除一些jar,因为已经包含了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM