1. 程式人生 > >隨筆5

隨筆5

pfile service arch for -s and star utf-8 big

這次完成了一個以excel文件導出相應數據統計內容的功能,選取其中的一個放在下面:

1.首先在資源文件夾下創建一個excel的package,並創建一個xlsx文件

技術分享圖片

技術分享圖片

創建的時候要註意版本兼容問題,2007和2003

2.chatStics.ftl

在前端頁面添加導出:

<a href="javascript:void(0);" onclick="javascript:exportData();" class="easyui-linkbutton" iconCls="icon-search">導出</a>

添加js事件:

    function exportData() {
        $(
‘#queryForm‘).form(‘submit‘, { url:‘${request.contextPath}/examine/exportChatStaticsList‘, success:function () { console.log("success"); } }) }

3.StatisticsController

 @RequestMapping(value = "/exportChatStaticsList")
    public void exportChatStaticsList(@RequestParam(value = "date") String date, HttpServletResponse response)throws
IOException { statisticsService.exportChatStaticsList(date,response); } }

4.StatisticsService

public void  exportChatStaticsList(String date, HttpServletResponse response) throws IOException {
        String tempFilePath = StatisticsService.class.getResource("/excel/chat.xlsx").getPath();
        String filename
="chat-"+date; List<Map<Integer, Object>> dataList = new ArrayList<>(); List<TChatStatistics> list = chatStatisticsMapper.selectChatStics(date); for (TChatStatistics chatDTO : list){ Map<Integer,Object> data = new HashMap<Integer, Object>(); data.put(1,chatDTO.getUid()); data.put(2,chatDTO.getNickname()); data.put(3,chatDTO.getTotalNum()); data.put(4,chatDTO.getConnectNum()); double value =new BigDecimal((Double.valueOf(chatDTO.getConnectNum())/Double.valueOf(chatDTO.getTotalNum()))*100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); data.put(5,value); Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = ((SimpleDateFormat) format).format(chatDTO.getDate()); data.put(6,time); dataList.add(data); } String[] heads = new String[]{"A2","B2","C2","D2","E2","F2"}; ExcelUtil.writeDateList(tempFilePath,heads,dataList,0); response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); if("2007".equals("2007")){ response.setHeader("Content-Disposition", "inline;fileName="+filename+".xlsx"); }else{ response.setHeader("Content-Disposition", "inline;fileName="+filename+".xls"); } OutputStream out = response.getOutputStream(); ExcelUtil.writeAndClose(tempFilePath, out); out.close();

5.TChatStaticsMapper接口

public interface TChatStatisticsMapper extends BaseMapper<TChatStatistics> {

    /**
     *
     * @param startTime
     * @param endTime
     * @return
     */
    List<TChatRecord> selectChatRecordDate(@Param(value = "startTime")String startTime, @Param(value = "endTime")String endTime);

    List<TChatStatistics> selectChatStics(@Param(value = "date")String date);

}

6.TChatStatisticsMapper.xml

查詢語句:

    <select id="selectChatStics" resultMap="BaseResultMap">
        select cs.*,ai.nickname from t_chat_statistics cs LEFT JOIN t_anchor_info ai on cs.uid=ai.uid where cs.date=#{date} and ai.type=3 and ai.status=0 ORDER BY cs.connect_num desc
    </select>

隨筆5