Package com.lgx8.management.action

Source Code of com.lgx8.management.action.ExportExcelAction

package com.lgx8.management.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx8.common.servlet.BaseController;
import com.lgx8.management.entities.RebateContent;
import com.lgx8.management.service.RebateContentService;
import com.lgx8.util.ImageUtils;

/**
*
* @author pengdeyi
*
*/
@Controller
public class ExportExcelAction extends BaseController {

  @Autowired
  private RebateContentService rebateContentService;

  @RequestMapping("rebate/listrebatecontent")
  public String listAllRabate(HttpServletRequest request,
      HttpServletResponse response) {
    String year = request.getParameter("year");
    String month = request.getParameter("month");
    String name = request.getParameter("name");
    String fllx = request.getParameter("fllx");
    // String pageSize = request.getParameter("pageSize");
    String curPage = request.getParameter("curPage");
    @SuppressWarnings("null")
    List<RebateContent> list = rebateContentService
        .listRebateContentByCondition(
            year,
            month,
            fllx,
            name,
            8,
            curPage == null && curPage.length() == 0 ? 0 : Integer
                .parseInt(curPage) - 1);
    int count = rebateContentService.getTotal(year, month, fllx, name);
    request.setAttribute("list", list);
    request.setAttribute("curPage", curPage == null
        && curPage.length() == 0 ? 0 : Integer.parseInt(curPage));
    request.setAttribute("counts", count);
    request.setAttribute("year", year);
    request.setAttribute("month", month);
    request.setAttribute("name", name);

    request.setAttribute("fllx", fllx);
    // request.setAttribute("size", count % Integer.parseInt(pageSize));
    return "/management/rebate/list_rebatecontent";
  }

  /**
   * 导出excel
   *
   * @param request
   * @param response
   * @throws IOException
   */
  @RequestMapping("rebate/exportrebatecontent")
  public void exportAllFRabate(HttpServletRequest request,
      HttpServletResponse response) throws IOException {
    String year = request.getParameter("year");
    String month = request.getParameter("month");
    String name = request.getParameter("name");
    String fllx = request.getParameter("fllx");
    int count = rebateContentService.getTotal(year, month, fllx, name);
    List<RebateContent> list = rebateContentService
        .listRebateContentByCondition(year, month, fllx, name, count,
            0);

    String[] headers = { "机构类型","机构编号", "机构名称", "积分"};

    String filename = year + "年"+month+"月"+"返利汇总信息";

    int size = list.size();

    if (size > 0) {
      String uploadPath = this.getUploadPath();
      String sourcePath = uploadPath
          + System.getProperty("file.separator");

      sourcePath = sourcePath.replace("%20", " ") + filename + ".xls";

      OutputStream out = new FileOutputStream(new File(sourcePath));

      HSSFWorkbook workbook = new HSSFWorkbook();

      HSSFCellStyle style = workbook.createCellStyle();
      // 设置这些样式
      style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
      style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
      style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
      style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
      style.setBorderRight(HSSFCellStyle.BORDER_THIN);
      style.setBorderTop(HSSFCellStyle.BORDER_THIN);
      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
      // 生成一个字体
      HSSFFont font = workbook.createFont();
      font.setFontHeightInPoints((short) 12);
      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
      // 把字体应用到当前的样式
      style.setFont(font);

      HSSFCellStyle styledata = workbook.createCellStyle();
      // 设置这些样式
      styledata.setBorderBottom(HSSFCellStyle.BORDER_THIN);
      styledata.setBorderLeft(HSSFCellStyle.BORDER_THIN);
      styledata.setBorderRight(HSSFCellStyle.BORDER_THIN);
      styledata.setBorderTop(HSSFCellStyle.BORDER_THIN);
      // styledata.setAlignment(HSSFCellStyle.ALIGN_CENTER);

      HSSFSheet sheet = workbook.createSheet( year + "年"+month+"月");
      sheet.setDefaultColumnWidth(15);
      sheet.createFreezePane(1, 1);
      // 产生表格标题行
      HSSFRow row = sheet.createRow(0);

      for (int i = 0; i < headers.length; i++) {
        HSSFCell cell = row.createCell(i);
        cell.setCellStyle(style);
        HSSFRichTextString text = new HSSFRichTextString(headers[i]);
        cell.setCellValue(text);
      }

      for (int n = 1; n <= size; n++) {
        HSSFRow rowdata = sheet.createRow(n);
        // int i = (n - 1) + ((t - 1) * number);
        RebateContent rc = list.get(n-1);
        HSSFCell cell0 = rowdata.createCell(0);
        HSSFRichTextString text0 = new HSSFRichTextString(rc.getFllx());
        cell0.setCellValue(text0);
        cell0.setCellStyle(styledata);

        HSSFCell cell1 = rowdata.createCell(1);
        // HSSFRichTextString text1 = new
        // HSSFRichTextString(card.getPassword());
        cell1.setCellValue(rc.getOid());
        cell1.setCellStyle(styledata);

        HSSFCell cell2 = rowdata.createCell(2);
        // HSSFRichTextString text2 = new
        // HSSFRichTextString(card.getScore().toString());
        cell2.setCellValue(rc.getName());
        cell2.setCellStyle(styledata);

        HSSFCell cell3 = rowdata.createCell(3);
        cell3.setCellValue(rc.getJf_count());
        cell3.setCellStyle(styledata);
       

      }

      workbook.write(out);

      File file = new File(sourcePath);

      if (file.exists()) {
        // filename = Escape.escape(filename);

        filename = URLEncoder.encode(file.getName(), "utf-8");

        response.reset();
        response.setContentType("application/x-msdownload");
        response.addHeader("Content-Disposition",
            "attachment; filename=\"" + filename + "\"");

        int fileLength = (int) file.length();
        response.setContentLength(fileLength);
        /* 如果文件长度大于0 */
        if (fileLength != 0) {
          /* 创建输入流 */
          InputStream is = new FileInputStream(file);
          byte[] buf = new byte[4096];
          /* 创建输出流 */
          ServletOutputStream sos = response.getOutputStream();
          int readLength;
          while (((readLength = is.read(buf)) != -1)) {
            sos.write(buf, 0, readLength);
          }
          is.close();
          sos.flush();
          sos.close();
        }
      }

      if (file.exists()) {
        file.delete();
      }
    }
  }

  private String getUploadPath() {
    File file = new File(ImageUtils.class.getClassLoader().getResource("/")
        .getPath());
    file = file.getParentFile().getParentFile();
    file = new File(file.getAbsolutePath()
        + System.getProperty("file.separator") + "upload");
    if (!file.exists()) {
      file.mkdir();
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    Calendar c = Calendar.getInstance();
    String directoryName = sdf.format(c.getTime());
    file = new File(file.getAbsoluteFile()
        + System.getProperty("file.separator") + directoryName);
    if (!file.exists()) {
      file.mkdir();
    }
    return file.getAbsolutePath();
  }
}
TOP

Related Classes of com.lgx8.management.action.ExportExcelAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.