JAvaはzipにexcelファイルをリアルタイムで書き込み、ブラウザでダウンロード

4100 ワード

@RequestMapping(value = "exportFunderZip", method = RequestMethod.GET)
public void exportFunderZip(HttpServletResponse response,Integer financeId)throws Exception{
        //                 
        response.setContentType("application/ostet-stream");
        response.setHeader("Content-disposition","attachment;filename="+new String("      .zip".getBytes("gbk"),"iso-8859-1"));
        response.setBufferSize(1024);
        //   zip   
        ZipOutputStream zos=new ZipOutputStream(response.getOutputStream());

        exportFunderInvoiceData(financeId,zos);
        exportFunderLogisticsData(financeId,zos);
        exportFunderOrderItemData(financeId,zos);
        exportFunderPurchaseData(financeId,zos);
        exportFunderSaleorderData(financeId,zos);
        exportFunderSupplierCompanyData(financeId,zos);
        //   zip   
        zos.close();
        }

 
 
 
public void exportFunderInvoiceData(Integer financeId, ZipOutputStream zos) throws Exception {
        List funderInvoiceDataInfos = new FinanceDataTransferServiceClient().funderInvoiceDataExport(financeId);

        Map fieldNameMap = new LinkedHashMap();
        fieldNameMap.put("transCode", "    ");
        fieldNameMap.put("clearStatus", "    ");
        fieldNameMap.put("amount", "  ");
        //                  
        ZipEntry zipEntry = new ZipEntry("      .xls");
        //          
        zos.putNextEntry(zipEntry);
        new ExcelFileGeneratorUtils(fieldNameMap, funderInvoiceDataInfos).expordExcel(zos);
        //        
        zos.closeEntry();
        }

 
 
 
 
 
 
 
package com.isuwang.soa.finance.helper

import java.io.{FileInputStream, OutputStream}

import org.apache.poi.hssf.usermodel._
import org.apache.poi.hssf.util.HSSFColor
import org.apache.poi.ss.usermodel.CellType
import org.slf4j.LoggerFactory
import scala.collection.mutable._
import scala.util.control.Breaks._

/**
  * @Description:         
  * @author jiangepng
  * @date 2017/11/30 18:25
  */
object ExcelFileGeneratorUtils {
  lazy val logger = LoggerFactory.getLogger(classOf[HSSFWorkbook])
  private val SPLIT_COUNT = 50000
  private var fieldNameKeys: ListBuffer[String] = new ListBuffer()
  private var fieldNameValues: ListBuffer[String] = new ListBuffer()
  private var fieldData: List[Any] = Nil
  private var workBook: HSSFWorkbook = _

  def ExcelFileGeneratorUtils(fieldNameMap: LinkedHashMap[String, String], fieldData: List[Any], os: OutputStream): Unit = {
    splitFieldNameMapToList(fieldNameMap)
    this.fieldData = fieldData
    expordExcelByte(os)
  }

  private def splitFieldNameMapToList(fieldNameMap: Map[String, String]) {
    val keySet = fieldNameMap.keySet
    fieldNameKeys.clear()
    fieldNameValues.clear()
    for (key = rows)
          break()
        //       ,    excel   
        val row = sheet.createRow(k + 1)
        val rowData = fieldData((i - 1) * SPLIT_COUNT + k)
        //              
        for (n  {
        ex.printStackTrace()
      }
    }
  }

  /**
    * @Description:  excel          ,       
    * @author jiangepng
    * @date 2017/12/1 11:33
    * @param os    
    */
  private def expordExcel(os: OutputStream): Unit = {
    try {
      workBook = createWorkbook()
      workBook.write(os)
      os.close()
    } catch {
      case ex: Exception => {
        logger.error(ex.getMessage, ex)
      }
    }
  }

  /**
    * @Description:  excel            ,       
    * @author jiangepng
    * @date 2017/12/13 16:54
    * @param
    * @return
  */
  private def expordExcelByte(os: OutputStream): Unit = {
    try {
      workBook = createWorkbook()
      workBook.write(os)
    } catch {
      case ex: Exception => {
        logger.error(ex.getMessage, ex)
      }
    }
  }
}