后端代码实现例子:
在这里我定义一个 content
我获取的数据库值就会从0开始对应
list是数据库调用数据的定义值
具体例子判断和日期格式定义成yyyy--mm-dd例子如:
@ResponseBody
@RequestMapping("/exportExcel")
public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
List<WorkerEntity> list = getWorkerService().getExportWorkers(weParam);
String title = "xxxx";
String[] Mytitle = {"姓名", "性别",''创建日期''};
String sheetName = "xxx档案";
int a = list.size();
String content[][] = new String[a][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[Mytitle.length];
content[i][0] = list.get(i).getChineseName();
content[i][1] = list.get(i).getSex() == 0 ? "男" : "女";
content[i][2] = String.valueOf(DateUtil.format(list.get(i).getCreateDate(), DatePattern.NORM_DATE_FORMATTER));
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(sheetName, Mytitle, title, content, null);
try {
String name = "xxxx.xls"; // 文件名
OutputStream os = response.getOutputStream(); // 取得输出流
response.reset(); // 清空输出流
response.setHeader("Content-Disposition", "attachment;filename=" + name + ";filename*=utf-8''"
+ URLEncoder.encode(name, "UTF-8")); // 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注意:
1.ExportExcelUtil是一个导出的excel格式定义,例子:
<dependency>
<groupId>org.meteor</groupId>
<artifactId>meteor-mgr</artifactId>
<version>0.0.1.2-SNAPSHOT</version>
<