poi导出EXCEL经典实现()

来源:互联网 发布:qq飞车b车 大黄蜂数据 编辑:程序博客网 时间:2024/06/05 21:50

第一次发    请多关照

excel 导出是日常生活中经常用到的功能  本人针对此功能进行优化 

JSP

 <form id="importForm" method="post" enctype="multipart/form-data">
<div class="btngroupSection pull-right">
<a role="button" class="btn btn-zdy" href="group/toaddanswer.do">新增会话</a>
<input id="importFile" style="display: none;" type="file"
name="file" onchange="imExcel(this)" multiple="multiple" /> <a
role="button" id="upload" class="btn btn-default">批量导入</a>
  <a role="button" onclick="exp2()" class="btn btn-default">批量导出</a> 
</div>
</form>

JS   

function exp2() {

var form = $("#importForm");

if (confirm("是否要导出数据?")) {

alert(3);

var options = {

url : "group/export.do",

type : 'post',

dataType : 'json',

success : function(data) {

var jsondata = eval("(" + data + ")");

if (jsondata.state == 0) {

}

}

};

form.ajaxSubmit(options);

}

}
 

 @RequestMapping("/export.do")
    public void export(HttpServletRequest request, HttpServletResponse response) {


        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String fileName = "NLU" + sdf.format(date);
        String export = UtilConstants.RoBOT_EXPORT_PATH;
        String filePath = export + "/" + fileName + ".xls";
        System.out.println(filePath);
        String[] titles = { "问题", "回答" };


        List<Tgroup> GroupList = groupService.exportList();
        List<Map<Integer, String>> lists = new ArrayList<Map<Integer, String>>();
        for (int i = 0; i < GroupList.size(); i++) {
            Tgroup tgroup = GroupList.get(i);
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(0, tgroup.getQuestion());
            map.put(1, tgroup.getAnswer());
            lists.add(map);
        }
        try {
            ExcelUtil.writeExcel(filePath, titles, lists);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("state", 0);
        JSONObject json = JSONObject.fromObject(map);
        try {
            response.getWriter().print(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//ExcelUtil 工具类

public static void writeExcel(String filePath, String[] titles,List<Map<Integer, String>> lists) throws IOException {
File file = new File(filePath);
File fileParent = file.getParentFile();
if(!fileParent.exists() && !fileParent.isDirectory()){
fileParent.mkdirs();
}
OutputStream fos = new FileOutputStream(filePath);
HSSFWorkbook xls = new HSSFWorkbook();
HSSFSheet sheet = xls.createSheet();
HSSFRow row = sheet.createRow(0);// 第一行


for (int i = 0; i < titles.length; i++) {
row.createCell(i).setCellValue(titles[i]);
}
// 内容
int rowNum = 1;
for (Map<Integer, String> map : lists) {
HSSFRow rowTmp = sheet.createRow(rowNum);
int cols = map.size();
for (int i = 0; i < cols; i++) {
rowTmp.createCell(i).setCellValue(map.get(i));
}
rowNum++;
}
xls.write(fos);
fos.close();

}

pom.xml 文件配置

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>