eclipse 如何在 Spring Boot 中创建 Apache POI Excel 视图配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34783015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Apache POI Excel View Configuration in Spring Boot
提问by Frans Filasta Pratama
I have problem when i want to export my data to excel using spring boot web.
当我想使用 spring boot web 将数据导出到 excel 时遇到问题。
I am using Thymeleaf as template engine (auto configured by spring boot). But when I add XmlViewResolver in my additional configuration, my thymeleaf view resolved by XmlViewResolver which is absolutely will fail to resolve. I try fix that problem by create new class thant extends WebMvcConfigurerAdapter and reconfigure thymeleaf template resolver there. But my template cant be resolved, becase my template location not found. I put it in :
我使用 Thymeleaf 作为模板引擎(由 spring boot 自动配置)。但是当我在附加配置中添加 XmlViewResolver 时,我的 thymeleaf 视图由 XmlViewResolver 解析,这绝对无法解析。我尝试通过创建新类而不是扩展 WebMvcConfigurerAdapter 并在那里重新配置 thymeleaf 模板解析器来解决该问题。但是我的模板无法解析,因为找不到我的模板位置。我把它放在:
/resources/template/
Please help me.
请帮我。
回答by selvinsource
With Spring Boot you don't need any extra configuration to generate an excel file.
使用 Spring Boot,你不需要任何额外的配置来生成一个 excel 文件。
Create a controller returning a ModelAndView with an AbstractExcelView:
创建一个控制器,返回一个带有 AbstractExcelView 的 ModelAndView:
@Controller
public class MyController {
@RequestMapping(value="/myexcel", method=RequestMethod.GET)
public ModelAndView getMyData(HttpServletRequest request, HttpServletResponse response) throws SQLException{
Map<String, Object> model = new HashMap<String, Object>();
//Sheet Name
model.put("sheetname", "TestSheetName");
//Headers List
List<String> headers = new ArrayList<String>();
headers.add("Column1");
headers.add("Column2");
headers.add("Column3");
model.put("headers", headers);
//Results Table (List<Object[]>)
List<List<String>> results = new ArrayList<List<String>>();
List<String> l1 = new ArrayList<String>();
l1.add("A1");
l1.add("B1");
l1.add("C1");
results.add(l1);
List<String> l2 = new ArrayList<String>();
l2.add("A2");
l2.add("B2");
l2.add("C2");
results.add(l2);
model.put("results",results);
response.setContentType( "application/ms-excel" );
response.setHeader( "Content-disposition", "attachment; filename=myfile.xls" );
return new ModelAndView(new MyExcelView(), model);
}
Then build your AbstractExcelView like:
然后构建你的 AbstractExcelView 像:
public class MyExcelView extends AbstractExcelView
{
@SuppressWarnings("unchecked")
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook,
HttpServletRequest request,
HttpServletResponse response)
{
//VARIABLES REQUIRED IN MODEL
String sheetName = (String)model.get("sheetname");
List<String> headers = (List<String>)model.get("headers");
List<List<String>> results = (List<List<String>>)model.get("results");
List<String> numericColumns = new ArrayList<String>();
if (model.containsKey("numericcolumns"))
numericColumns = (List<String>)model.get("numericcolumns");
//BUILD DOC
HSSFSheet sheet = workbook.createSheet(sheetName);
sheet.setDefaultColumnWidth((short) 12);
int currentRow = 0;
short currentColumn = 0;
//CREATE STYLE FOR HEADER
HSSFCellStyle headerStyle = workbook.createCellStyle();
HSSFFont headerFont = workbook.createFont();
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
//POPULATE HEADER COLUMNS
HSSFRow headerRow = sheet.createRow(currentRow);
for(String header:headers){
HSSFRichTextString text = new HSSFRichTextString(header);
HSSFCell cell = headerRow.createCell(currentColumn);
cell.setCellStyle(headerStyle);
cell.setCellValue(text);
currentColumn++;
}
//POPULATE VALUE ROWS/COLUMNS
currentRow++;//exclude header
for(List<String> result: results){
currentColumn = 0;
HSSFRow row = sheet.createRow(currentRow);
for(String value : result){//used to count number of columns
HSSFCell cell = row.createCell(currentColumn);
if (numericColumns.contains(headers.get(currentColumn))){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(NumUtils.extractDoubleOrZero(value));
} else {
HSSFRichTextString text = new HSSFRichTextString(value);
cell.setCellValue(text);
}
currentColumn++;
}
currentRow++;
}
}
}