JAva実装excelエクスポートインスタンス
2589 ワード
:list 。
String path = request.getSession().getServletContext().getRealPath("/");
String name = " _" + Tools.getDateStr(new Date(), "YYYYMMddHHmmss") + ".xls";
XLSTransformer transformer = new XLSTransformer();
String sfrom = path + "/xlstemplet/" +(1 == salvationType?" 1-":" 2-")+ " .xls";//
Workbook workbook;
int pageSize = (!Tools.isEmpty(request.getParameter("pageSize"))) ? Integer.parseInt(request.getParameter("pageSize"))
: 100;// sheet
try {
if ("mutiSheet".equals(request.getParameter("mutiSheet"))&& list.size() / pageSize > 1) {
List sheetObjects = new ArrayList(); // sheet list
List sheetNames = new ArrayList(); // sheet
if (list != null) {
for (int i = 0; i < list.size() / pageSize; i++) {
Map subMap = new HashMap();
subMap.put("pageSize", pageSize);
subMap.put("currentPage", (i+1));
int k = ((i + 1) * pageSize < list.size()) ? (i + 1) * pageSize : list.size();
List resultList = list.subList(i * pageSize, k);
subMap.put("resultList", resultList);
sheetObjects.add(subMap);
sheetNames.add("page" + (i + 1));
}
}
workbook = transformer.transformMultipleSheetsList(new FileInputStream(sfrom), sheetObjects, sheetNames, "map",
new HashMap(), 0);
} else {
Map beanMap = new HashMap();
map.put("pageSize", 0);
map.put("currentPage",0);
beanMap.put("map", map);
workbook = transformer.transformXLS(new FileInputStream(sfrom), beanMap);
}
response.reset();
response.setHeader("Content-disposition", "attachment;success=true;filename =" + URLEncoder.encode(name, "utf-8"));
OutputStream fos = null;
fos = response.getOutputStream();
workbook.write(fos);
fos.flush();
fos.close();
} catch (ParsePropertyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
また3つのjarバッグを導入する必要があります:この卵が痛いブログは添付ファイルを伝えることができません.の
3つのjarファイルのダウンロードアドレス:http://download.csdn.net/detail/sky_xin/8444825