废话不多说 很简单 读取模板的导出 总体思路就是 先读取模板 然后将数据导入到模板中 最后导出
@RequestMapping(value = "/exportInfo", method = RequestMethod.GET)
public void exportInfo(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
boolean flag = false;
String bdid = "2008";
// String path = request.getParameter("path");
String fileName = "11";
String filePath = request.getSession().getServletContext().getRealPath("/uploads/excle/models/aa.xlsx");
//设置响应头 用户点击后形成一个下载窗口,让用户保存
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"),"ISO8859-1")+".xls");
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
OutputStream out = response.getOutputStream();
PrintWriter print = null;
ExportExcel xportExcel = new ExportExcel();
//弹出对话框
// String adr = exportExcel.selectSavePath();
ImportParam persons = new ImportParam();
String bdids[] = bdid.split(",");
List<Import> list = iequipment.getAll(bdids);//获取详细设备信息
ExportExcel<Import> ex = new ExportExcel<Import>();
String examname = list.get(0).getBdid();
ex.ExportInfo(filePath,list,out);
try {
out.close();
toClient.flush();
toClient.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void ExportInfo(String filePath,List<Import> list, OutputStream out){
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(new FileInputStream(filePath));//读取excel模板
XSSFSheet sheet = workbook.getSheetAt(0); //读取第一个工作簿
XSSFRow row;
XSSFCell cell = null;
int rownum = 3; //添加的起始行
XSSFCellStyle style = this.getStyle(workbook);
Iterator<Import> it = list.iterator();
Import order = null;
while(it.hasNext()) {
order = new Import();
order = it.next();
row = sheet.createRow(rownum);
myCreateCell(0, String.valueOf(rownum-2), row, cell, style); //列1
myCreateCell(1, order.getTheater(), row, cell, style); //列1
myCreateCell(2, order.getSubplot(),row, cell, style); //列2
myCreateCell(3, order.getMilitary(), row, cell, style); //列3
myCreateCell(4, order.getDivisions(), row, cell, style);
myCreateCell(5, order.getMilitary(), row, cell, style);
myCreateCell(6, order.getRegimental(), row, cell, style);
myCreateCell(7, order.getBattalion(), row, cell, style);
myCreateCell(8, order.getCompany(), row, cell, style);
myCreateCell(9, order.getPlatoon(), row, cell, style);
myCreateCell(10, order.getClasss(), row, cell, style);
myCreateCell(11, order.getBdid(), row, cell, style);
rownum++;
}
workbook.write(out);
// os.flush();
// os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}