excel 표 생 성 조립 내 보 내기---자바

15895 단어 자바excel리스트
excl 표 생 성 조립 내 보 내기
excl 표 생 성 조립 내 보 내기
controller 코드:
public void userExport(HttpServletResponse response) {
		List<QUser> qlist = userServiceImpl.getUserListExport();
		//      
    	String fileName="    ";
    	
    	List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, List> excl = new HashMap<>();
        List<String> rowlist = new ArrayList<>();
        rowlist.add("  id");
        rowlist.add("     ");
        rowlist.add("   ");
        rowlist.add("    ");
        rowlist.add("      ");
        excl.put("rowList", rowlist);
        excl.put("dataList", list);
        for (QUser user : qlist) {
        	HashMap<String,String> map=new HashMap<>();
        	map.put("  id",user.getUid().toString());
        	map.put("     ",user.getLoginname());
        	map.put("   ", user.getUname());
        	map.put("    ", sdf.format(user.getRegisterTime()));
        	map.put("      ", user.getTowDomain());
        	list.add(map);
		}
        //    
        HSSFWorkbook excel = ExceUtil.getExcel(excl, fileName);
        
         try {
        	 OutputStream output = response.getOutputStream();
        	 response.reset();
        	 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");//xlsx
        	 response.setContentType("application/msexcel");
        	 excel.write(output);
			 output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

ExceUtil 도구 류 코드
package com.xz.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExceUtil {
	/**
     *   excel  
     *
     * @param tableName sheet  
     * @return HSSFWorkbook
     */
    public static HSSFWorkbook getExcel(Map<String, List> resultMap, String tableName) {

        //  HSSFWorkbook  (excel     )
        HSSFWorkbook wb = new HSSFWorkbook();
        //    sheet  (excel   )
        HSSFSheet sheet = wb.createSheet(tableName);
        //    
        //     ,      (excel  ),   0~65535       
        HSSFRow row = sheet.createRow(0);
        //             

        List rowList = resultMap.get("rowList");
        List dataList = resultMap.get("dataList");

        //         ,    
        for (int i = 0; i < rowList.size(); i++) {
            row.createCell(i).setCellValue(rowList.get(i).toString().trim());
        }
        //     
        for (int i = 0; i < dataList.size(); i++) {
            HSSFRow row1 = sheet.createRow(i + 1);
            Map<String, String> map2 = (Map<String, String>) dataList.get(i);
            for (Map.Entry<String, String> en : map2.entrySet()) {
                for (int j = 0; j < rowList.size(); j++) {
                    if (rowList.get(j).equals(en.getKey())) {
                        if (en.getValue() == "" || en.getValue() == null) {
                            row1.createCell(j).setCellValue("");
                        } else {
                            row1.createCell(j).setCellValue(en.getValue().toString());
                        }
                    }
                }
            }
        }
        return wb;
    }
}

좋은 웹페이지 즐겨찾기