Springmvc匯出excel

ZHOU_VIP發表於2017-03-03

新增到pom.xml檔案中的:


package com.xwtech.util;  
  
import java.util.List;  
import java.util.Map;  
  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.CellStyle;  
import org.apache.poi.ss.usermodel.Font;  
import org.apache.poi.ss.usermodel.IndexedColors;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;  
  
public class ExcelUtil {  
     public static Workbook createWorkBook(List<Map<String, Object>> list,String []keys,String columnNames[]) {  
            // 建立excel工作簿  
            Workbook wb = new HSSFWorkbook();  
            // 建立第一個sheet(頁),並命名  
            Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString());  
            // 手動設定列寬。第一個參數列示要為第幾列設;,第二個參數列示列的寬度,n為列高的畫素數。  
            for(int i=0;i<keys.length;i++){  
                sheet.setColumnWidth((short) i, (short) (45.7 * 150));  
            }  
  
            // 建立第一行  
            Row row = sheet.createRow((short) 0);  
  
            // 建立兩種單元格格式  
            CellStyle cs = wb.createCellStyle();  
            CellStyle cs2 = wb.createCellStyle();  
  
            // 建立兩種字型  
            Font f = wb.createFont();  
            Font f2 = wb.createFont();  
  
            // 建立第一種字型樣式(用於列名)  
            f.setFontHeightInPoints((short) 10);  
            f.setColor(IndexedColors.BLACK.getIndex());  
            f.setBoldweight(Font.BOLDWEIGHT_BOLD);  
  
            // 建立第二種字型樣式(用於值)  
            f2.setFontHeightInPoints((short) 10);  
            f2.setColor(IndexedColors.BLACK.getIndex());  
  
//            Font f3=wb.createFont();  
//            f3.setFontHeightInPoints((short) 10);  
//            f3.setColor(IndexedColors.RED.getIndex());  
  
            // 設定第一種單元格的樣式(用於列名)  
            cs.setFont(f);  
            cs.setBorderLeft(CellStyle.BORDER_THIN);  
            cs.setBorderRight(CellStyle.BORDER_THIN);  
            cs.setBorderTop(CellStyle.BORDER_THIN);  
            cs.setBorderBottom(CellStyle.BORDER_THIN);  
            cs.setAlignment(CellStyle.ALIGN_CENTER);  
  
            // 設定第二種單元格的樣式(用於值)  
            cs2.setFont(f2);  
            cs2.setBorderLeft(CellStyle.BORDER_THIN);  
            cs2.setBorderRight(CellStyle.BORDER_THIN);  
            cs2.setBorderTop(CellStyle.BORDER_THIN);  
            cs2.setBorderBottom(CellStyle.BORDER_THIN);  
            cs2.setAlignment(CellStyle.ALIGN_CENTER);  
            //設定列名  
            for(int i=0;i<columnNames.length;i++){  
                Cell cell = row.createCell(i);  
                cell.setCellValue(columnNames[i]);  
                cell.setCellStyle(cs);  
            }  
            //設定每行每列的值  
            for (short i = 1; i < list.size(); i++) {  
                // Row 行,Cell 方格 , Row 和 Cell 都是從0開始計數的  
                // 建立一行,在頁sheet上  
                Row row1 = sheet.createRow((short) i);  
                // 在row行上建立一個方格  
                for(short j=0;j<keys.length;j++){  
                    Cell cell = row1.createCell(j);  
                    cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString());  
                    cell.setCellStyle(cs2);  
                }  
            }  
            return wb;  
        }  
}

總結:

/*
程式碼是從網上找的  
先在http://search.maven.org/搜poi  
把  
<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi</artifactId>  
    <version>3.13</version>  
</dependency>  
放到專案的pom.xml的檔案中    
注意只能用button <button onclick="download()">下載</button>  
取到引數,通過url把引數帶到後臺,還有一種就是ajax的方式,寫url:XXX  param:引數名字  
*/
function download(){  
        starttime=$("#stime").val();  
        endtime=$("#etime").val();  
        method=$("#weidu").val();  
        var url="${ctx}/history/download?starttime="+starttime+"&endtime="+endtime+"&areaType="+areaType+"&method="+method+"&areaId="+areaId;  
        /* alert(url); */  
        window.open(url);  
  
    }  
在後臺接引數  
String areaType = request.getParameter("areaType");//areaType 為2是城市維度,為1是景區維度  
String starttime = request.getParameter("starttime");//查詢的開始時間  
String endtime = request.getParameter("endtime");//查詢的結束時間  
String method = request.getParameter("method");//呼叫的方法  
String areaId = request.getParameter("areaId");//景區維度時,調的方法多一個景區id引數  
  
//城市維度  
if(areaType.equals("2")){  
                //1.遊客來源  
                if(method.equals("getFromCountC")){  
                     String fileName="excel檔案";  
                     //填充projects資料  
                    /* List<History> projects=getcount("6216","20160111","20160118");*/  
                     List<History> projects=getcountC(starttime,endtime);  
                     List<Map<String,Object>> list=createExcelRecord(projects);  
                     String columnNames[]={"日期","省份","遊客數"};//列名  
                     String keys[]    =   {"date","name","count"};//map中的key  
                     ByteArrayOutputStream os = new ByteArrayOutputStream();
參考:https://my.oschina.net/aptx4869/blog/298507


相關文章