JS导出table到Excel

这里有4个title对应4个table,点击导出Excel时,会根据每个导航传来的index进行switch匹配。

JS导出table到Excel_第1张图片

下面是整个页面所有代码,angularjs jquery js混用。

<#include "../common/head.html"/>





游戏名称 道具名称 交易次数 单价 交易总额
{{x.bundleName}} {{x.productName}} {{x.count}} {{}} {{}}
操作人 游戏名称 道具名称 入库个数 单价 入库总额
{{x.producerName}} {{x.bundleName}} {{x.productName}} {{x.count}} {{}} {{}}
操作人     {{x.producerName}} 总入库     ¥{{x.inPrice}}
总计:     ¥{{inPrice}}
操作人 游戏名称 道具名称 交易次数 单价 交易总额
{{x.consumerName}} {{x.bundleName}} {{x.productName}} {{x.count}} {{}} {{}}
操作人     {{x.consumerName}} 总入库     ¥{{x.outPrice}}
总计:     ¥{{outPrice}}
游戏名称 库存总价值
{{x.bundleName}} {{x.totalPrice}}元
总计:     ¥{{totalPrice}}

下面是用JAVA POI实现导出到excel

controller

package com.apply.controller;

import com.apply.model.Goods;
import com.apply.service.GoodsService;
import com.mysql.jdbc.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/29.
 */
@Controller
@RequestMapping("goods")
public class GoodsController {
    @GetMapping("statistics")
    public String statisticsGoods(){
        return "goods/statisticsGoods";
    }

    @Autowired
    private GoodsService goodsService;
    /**
     * 入库统计
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @GetMapping("in")
    public List inGoods(String startTime, String endTime) throws ParseException {
        Date start = null;
        Date end = null;
        if (!StringUtils.isNullOrEmpty(startTime) && !StringUtils.isNullOrEmpty(endTime)){
            start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
            end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        }
        List list = goodsService.inGoods(start, end);
        for (int i = 0;i inPrice(){
        return goodsService.inPrice();
    }

    @ResponseBody
    @GetMapping("outPrice")
    public List outPrice(){
        return goodsService.getOutPrice();
    }

    /**
     * 出库统计
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @GetMapping("out")
    public List outGoods(String startTime, String endTime) throws ParseException {
        Date start = null;
        Date end = null;
        if (!StringUtils.isNullOrEmpty(startTime) && !StringUtils.isNullOrEmpty(endTime)){
            start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
            end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        }
        List list = goodsService.outGoods(start, end);
        for (int i = 0;i transactionGoods(String startTime, String endTime) throws ParseException {
        Date start = null;
        Date end = null;
        if (!StringUtils.isNullOrEmpty(startTime) && !StringUtils.isNullOrEmpty(endTime)){
            start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
            end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        }
        List list = goodsService.transactionGoods(start, end);
        for (int i = 0;i totalGoods(){
        return goodsService.totalGoods();
    }

    /**
     * excel导出
     */
    @GetMapping(value = "toExcel",produces = {"application/json"})
    public void exportExcel(HttpServletResponse response,Integer index) throws IOException {
        switch (index){
            case 0:
                List list = goodsService.transactionGoods(null, null);
                for (int i = 0;i list1 = goodsService.inGoods(null, null);
                for (int i = 0;i innerList1 = goodsService.inPrice();  //table页面最下面的汇总,另行计算
                HSSFWorkbook wb1 = goodsService.export(list1,innerList1,index);
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=inGoods.xls");
                OutputStream ouputStream1 = response.getOutputStream();
                wb1.write(ouputStream1);
                ouputStream1.flush();
                ouputStream1.close();
                break;
            case 2:
                List list2 = goodsService.outGoods(null, null);
                for (int i = 0;i innerList2 = goodsService.getOutPrice();//table页面最下面的汇总,另行计算
                HSSFWorkbook wb2 = goodsService.export(list2,innerList2,index);
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=outGoods.xls");
                OutputStream ouputStream2 = response.getOutputStream();
                wb2.write(ouputStream2);
                ouputStream2.flush();
                ouputStream2.close();
                break;
            case 3:
                List list3 = goodsService.totalGoods();
                HSSFWorkbook wb3 = goodsService.export(list3,null,index);
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=totalGoods.xls");
                OutputStream ouputStream3 = response.getOutputStream();
                wb3.write(ouputStream3);
                ouputStream3.flush();
                ouputStream3.close();
                break;
        }

    }
}

service

package com.apply.service;

import com.apply.mapper.GoodsMapper;
import com.apply.model.Goods;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/29.
 */
@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 入库
     * @param startTime
     * @param endTime
     * @return
     */
    public List inGoods(Date startTime, Date endTime){
        return goodsMapper.inGoods(startTime,endTime);
    }

    public Integer getCount(String product_name){
       return goodsMapper.getCount(product_name);
    }

    public List inPrice(){
        return goodsMapper.getInPrice();
    }
    /**
     * 出库
     * @param startTime
     * @param endTime
     * @return
     */
    public List outGoods(Date startTime, Date endTime){
        return goodsMapper.outGoods(startTime,endTime);
    }
    public Integer getOutCount(String product_name){
        return goodsMapper.getOutCount(product_name);
    }
    public List getOutPrice(){
        return goodsMapper.getOutPrice();
    }


    /**
     * 交易
     * @param startTime
     * @param endTime
     * @return
     */
    public List transactionGoods(Date startTime, Date endTime){
        return goodsMapper.transactionGoods(startTime,endTime);
    }
    public Integer transactionCount(String product_name){
        return goodsMapper.transactionCount(product_name);
    }

    /**
     * 总库存
     * @return
     */
    public List totalGoods(){
        return goodsMapper.totalGoods();
    }

    /**
     * 导出excel
     * @param list
     * @return
     */
    public HSSFWorkbook export(List list,List innerList,Integer index) throws IOException {
        String[] excelHeader0 = { "游戏名称", "道具名称", "交易次数","单价","交易总额"};
        String[] excelHeader1 = { "操作人","游戏名称", "道具名称", "入库个数","单价","交易总额"};
        String[] excelHeader2 = { "操作人","游戏名称", "道具名称", "交易次数","单价","交易总额"};
        String[] excelHeader3 = { "游戏名称", "库存总价值"};

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = null;
        if(index == 0){
            sheet = wb.createSheet("交易统计");
        }else if (index ==1){
            sheet = wb.createSheet("入库统计");
        }else if (index ==2 ){
            sheet = wb.createSheet("出库统计");
        }else if (index ==3 ){
            sheet = wb.createSheet("总库存");
        }
        HSSFRow row = sheet.createRow((int) 0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        switch (index){
            case 0:
                for (int i = 0; i < excelHeader0.length; i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(excelHeader0[i]);
                    cell.setCellStyle(style);
                    sheet.setColumnWidth(i,excelHeader0[i].getBytes().length*2*256);
                }
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow(i + 1);
                    if (null != list.get(i).getBundleName()){
                        row.createCell(0).setCellValue(list.get(i).getBundleName());
                    }
                    if (null != list.get(i).getProductName()){
                        row.createCell(1).setCellValue(list.get(i).getProductName());
                    }
                    if (null != list.get(i).getCount()){
                        row.createCell(2).setCellValue(list.get(i).getCount());
                    }
                    if (null != list.get(i).getTransactionPrice()){
                        row.createCell(3).setCellValue(list.get(i).getTransactionPrice());
                    }
                    row.createCell(4).setCellValue("");

                }
                break;
            case 1:
                for (int i = 0; i < excelHeader1.length; i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(excelHeader1[i]);
                    cell.setCellStyle(style);
                    sheet.setColumnWidth(i,excelHeader1[i].getBytes().length*2*256);
                }
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow(i + 1);
                    if (null != list.get(i).getProducerName()){
                        row.createCell(0).setCellValue(list.get(i).getProducerName());
                    }
                    if (null != list.get(i).getBundleName()){
                        row.createCell(1).setCellValue(list.get(i).getBundleName());
                    }
                    if (null != list.get(i).getProductName()){
                        row.createCell(2).setCellValue(list.get(i).getProductName());
                    }
                    if (null != list.get(i).getCount()){
                        row.createCell(3).setCellValue(list.get(i).getCount());
                    }
                    if (null != list.get(i).getTransactionPrice()){
                        row.createCell(4).setCellValue(list.get(i).getTransactionPrice());
                    }
                    row.createCell(5).setCellValue("");

                }
                /*入库table下面的汇总*/
                int inPrice = 0;
                String ProducerName = "操作人";
                String totalInGoods = "总入库   ¥";
                String total = "总计: ¥";
                for (int i = 0; i < innerList.size(); i++) {
                    inPrice += innerList.get(i).getInPrice();
                    row = sheet.createRow(list.size()+i+1);
                    if (null != innerList.get(i).getProducerName()){
                        row.createCell(0).setCellValue(ProducerName+innerList.get(i).getProducerName());
                    }
                    if (null != innerList.get(i).getInPrice()){
                        row.createCell(1).setCellValue(totalInGoods+innerList.get(i).getInPrice());
                    }
                }
                row = sheet.createRow(list.size()+innerList.size()+1);
                row.createCell(0).setCellValue(total+inPrice);
                break;
            case 2:
                for (int i = 0; i < excelHeader2.length; i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(excelHeader2[i]);
                    cell.setCellStyle(style);
                    sheet.setColumnWidth(i,excelHeader2[i].getBytes().length*2*256);
                }
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow(i + 1);
                    if (null != list.get(i).getConsumerName()){
                        row.createCell(0).setCellValue(list.get(i).getConsumerName());
                    }
                    if (null != list.get(i).getBundleName()){
                        row.createCell(1).setCellValue(list.get(i).getBundleName());
                    }
                    if (null != list.get(i).getProductName()){
                        row.createCell(2).setCellValue(list.get(i).getProductName());
                    }
                    if (null != list.get(i).getCount()){
                        row.createCell(3).setCellValue(list.get(i).getCount());
                    }
                    if (null != list.get(i).getTransactionPrice()){
                        row.createCell(4).setCellValue(list.get(i).getTransactionPrice());
                    }
                    row.createCell(5).setCellValue("");
                }
                /*出库table下面的汇总*/
                int outPrice = 0;
                String ConsumerName = "操作人";
                String totalOutGoods = "总出库   ¥";
                String total2 = "总计: ¥";
                for (int i = 0; i < innerList.size(); i++) {
                    outPrice += innerList.get(i).getOutPrice();
                    row = sheet.createRow(list.size()+i+1);
                    if (null != innerList.get(i).getConsumerName()){
                        row.createCell(0).setCellValue(ConsumerName+innerList.get(i).getConsumerName());
                    }
                    if (null != innerList.get(i).getOutPrice()){
                        row.createCell(1).setCellValue(totalOutGoods+innerList.get(i).getOutPrice());
                    }
                }
                row = sheet.createRow(list.size()+innerList.size()+1);
                row.createCell(0).setCellValue(total2+outPrice);
                break;
            case 3:
                for (int i = 0; i < excelHeader3.length; i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(excelHeader3[i]);
                    cell.setCellStyle(style);
                    sheet.setColumnWidth(i,excelHeader3[i].getBytes().length*2*256);
                }
                /*总库存table下面的汇总*/
                String total3 = "总计   ¥";
                int totalPrice = 0;
                for (int i = 0;i

JS

$scope.toExcel=function(){
            alert($scope.index);
            switch ($scope.index){
                case 0:
                    location.href="/goods/toExcel?index="+$scope.index;
                    break
                case 1:
                    location.href="/goods/toExcel?index="+$scope.index;
                    break
                case 2:
                    location.href="/goods/toExcel?index="+$scope.index;
                    break
                case 3:
                    location.href="/goods/toExcel?index="+$scope.index;
                    break
            }
        }

你可能感兴趣的:(JS导出table到Excel)