java—使用springmvc将数据导出到excel文件

rryofs0p  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(323)

我是begginerjava开发人员,我使用springmvc框架,我想从jsp页面(从controller发送)导出数据,我根据本教程工作http://www.codejava.net/frameworks/spring/spring-mvc-with-excel-view-example-apache-poi-and-jexcelapi
但我有个错误:
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 当我想使用“excel lib”poi-3.9.jar并将其作为pom.xml中的依赖项时
当我执行“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 ```
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]

所以我以常规的方式添加了这个库,它看起来很好,但是当我运行这个应用程序时,我在开始时就遇到了错误
这是我的密码:
控制器:

@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);
}

MVC配置:

@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;
}

视图.属性:

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());

    }
}

}

我认为这个问题在图书馆使用,但我尝试了许多版本和许多方法来使用它,但我总是得到错误。
有人能帮我吗?
xoefb8l8

xoefb8l81#

尝试使用此依赖项并执行
mvn清洁安装

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

我强烈建议当您开始使用maven作为依赖解析程序时,继续使用它,并且不要将来自您自己的库和maven的库混合使用,因为您将在将来发现问题。
尝试使用该版本执行,我们将看到异常,可能我们需要从poi依赖项中排除一些jar,因为它已经包含在内了

相关问题