我想在我的基于Spring-Boot的应用程序中实现将数据导出到Excel文件。
当我打算使用AbstractExcelView时,我知道AbstractExcelView
已被弃用。导出Excel文件的替代解决方案是什么?
答案 0 :(得分:6)
从Spring 4.2开始,您可以尝试AbstractExcelView
及其AbstractXlsView
和AbstractXlsxView
变种
AbstractXlsxStreamingView
下面给出了一个例子
import com.example.Course;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.List;
import java.util.Map;
public class XlsView extends AbstractXlsView {
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);
@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-xls-file.xls\"");
@SuppressWarnings("unchecked")
List<Course> courses = (List<Course>) model.get("courses");
// create excel xls sheet
Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsView");
// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Date");
// Create data cells
int rowCount = 1;
for (Course course : courses){
Row courseRow = sheet.createRow(rowCount++);
courseRow.createCell(0).setCellValue(course.getId());
courseRow.createCell(1).setCellValue(course.getName());
courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
}
}
}
另一个例子
import com.example.Course;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.List;
import java.util.Map;
public class XlsxStreamingView extends AbstractXlsxStreamingView {
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);
@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-xlsxStreaming-file.xlsx\"");
@SuppressWarnings("unchecked")
List<Course> courses = (List<Course>) model.get("courses");
// create excel xls sheet
Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsxStreamingView");
// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Date");
// Create data cells
int rowCount = 1;
for (Course course : courses){
Row courseRow = sheet.createRow(rowCount++);
courseRow.createCell(0).setCellValue(course.getId());
courseRow.createCell(1).setCellValue(course.getName());
courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
}
}
}
答案 1 :(得分:1)
将以下内容添加到Spring MVC项目中。
文件:src / views.properties
excelView2.(class)=YourPackageName.XlsxStreamingView
文件:WEB-INF / your_Spring_Config.xml
添加一个&#39; ResourceBundleViewResolver&#39;参考&#39; views.properties&#39;
<bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="order" value="1" />
<property name="basename" value="views" />
</bean>
<bean id="viewResolver2"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
文件:src / YourPackageName / Course.java
public class Course {
int Id;
String Name;
Date Date;
.. getter and setter
}
文件:src / YourPackageName / MyController.java
package YourPackageName.MyController;
@Controller
@RequestMapping("/example")
public class CustomerController {
...
@GetMapping("downloadExcel")
public ModelAndView downloadExcel() {
System.out.println("inside downloadExcel");
// create some sample data
List<Course> listCourses = new ArrayList<Course>();
listCourses.add(new Course(1, "Polarfrosch100", new Date()));
listCourses.add(new Course(2, "Polarfrosch101", new Date()));
listCourses.add(new Course(3, "Polarfrosch102", new Date()));
// return a view which will be resolved by an excel view resolver
return new ModelAndView("excelView2", "courses", listCourses);
}
}
文件:WEB-INF / view / your.jsp
...
<a href="${pageContext.request.contextPath}/example/downloadExcel">Download Excel Document</a>