excel 표 생 성 조립 내 보 내기---자바
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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.