/******************************************************************strtus2 导出Excel***************************************************************/
private String fileName;
/**
* 订单的Excel导出
*/
public InputStream getExportOrderList() throws ParsePropertyException,
InvalidFormatException, IOException {
if (data == null) {
data = new HashMap<String, String>();
}
Map<String, Object> beans = new HashMap<String, Object>();
// 查询订单信息
List<Map> orderInfoList = busOrderService.selectExcelOrderList(data);
beans.put("one", orderInfoList);
XLSTransformer transformer = new XLSTransformer();
fileName = ServletActionContext.getServletContext().getRealPath("downfile")
+ "/ly_order_list" + "_" + System.currentTimeMillis() + ".xls";
transformer.transformXLS(ServletActionContext.getServletContext()
.getRealPath("include/download/export_order_list.xls"), beans, fileName);
return new FileInputStream(fileName);
}
Struts2配置方法
<action name="getExportOrderList_export" class="com.ly.bus.reception.action.BusCarNoAction">
<result name="success" type="stream">
<param name="bufferSize">1024</param>
<param name="contentDisposition">attachment;fileName=${fileName}</param>
<param name="inputName">exportOrderList</param>
</result>
</action>
/*********************************************************************Spring MVC**********************************************************************************/
/**
* 临时性费用Excel导出
* @param response
* @param request
* @param ysgl
* @throws ParsePropertyException
* @throws InvalidFormatException
* @throws IOException
*/
@RequestMapping(value="/exportLsxfxList")
public void exportLsxfxList(HttpServletResponse response, HttpServletRequest request, @ModelAttribute("csEnt") CsEnt csEnt) throws ParsePropertyException, InvalidFormatException, IOException {
try{
Map<String, Object> beans = new HashMap<String, Object>();
//生成周期性费项数据
List<Ysgl> zqxfyysgl=sfbzbService.excelLsxfyysgl(csEnt.getCs2(),csEnt.getCs4(),"1002");
beans.put("one", zqxfyysgl);
String path=request.getSession().getServletContext().getRealPath("/include/download/fx_export_jszqxfx.xls");
InputStream is = new BufferedInputStream(new FileInputStream(path));
XLSTransformer transformer = new XLSTransformer();
HSSFWorkbook workbook = (HSSFWorkbook)transformer.transformXLS(is,beans);
response.setContentType("application/binary;charset=ISO8859_1");
ServletOutputStream out = response.getOutputStream();
String fileName = new String(("临时性费用报表").getBytes(), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");// 组装附件名称和格式
workbook.write(out);
out.flush();
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
//导入Excel
/**
* 文件上传
* @param request
* @param model
* @param mfile
* @param uploadPath
* @return
* @throws Exception
*/
@RequestMapping(value = "/uploadfile")
public void uploadFile(HttpServletRequest request, HttpServletResponse response, Model model,
@RequestParam(value = "importExcel") MultipartFile mfile) throws Exception {
ReturnInfo returnInfo = new ReturnInfo(request);
response.setCharacterEncoding("UTF-8");//中文转码
response.setContentType("text/html");//表头
try {
HttpSession session = request.getSession();
if(!mfile.isEmpty()) {
String storeFilePath = session.getServletContext().getRealPath("WEB-INF/upload_file");
String storeFile = "yzzl" + String.valueOf(System.currentTimeMillis());
String fileExt = FilenameUtils.getExtension(mfile.getOriginalFilename());
String filePath = storeFilePath + "/" + storeFile + "." + fileExt;
if(!"xls".equalsIgnoreCase(fileExt)) {
returnInfo.setStatusCode("300");
returnInfo.setMessage("请上传文件类型为【.xls】的文件");
response.getWriter().write(JSONObject.toJSONString(returnInfo));
}
File directory = new File(storeFilePath);
if(!directory.exists()) {
directory.mkdirs();
}
mfile.transferTo(new File(filePath));
InputStream is = new FileInputStream(filePath);
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
//第一个sheet 房产资料
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
//int colNum = row.getPhysicalNumberOfCells();
int rowNum = sheet.getPhysicalNumberOfRows();
if(rowNum<2){
returnInfo.setStatusCode("300");
returnInfo.setMessage("Excel文件中没有房产资料数据");
response.getWriter().write(JSONObject.toJSONString(returnInfo));
}
List<FjLb> listfj = new ArrayList<FjLb>();
for (int i=1; i<rowNum; i++) {
try {
row = sheet.getRow(i);
FjLb item = new FjLb();
String fjbh = ExcelUtils.getCastStringValue(row.getCell(0));
if(StringUtils.isBlank(fjbh))
continue;
/* item.setLcbh(lcbh);
item.setLcmc(ExcelUtils.getCastStringValue(row.getCell(1)));*/
/*item.setFjbhmc(ExcelUtils.getCastStringValue(row.getCell(3)));*/
item.setFjbh(fjbh);
item.setFjmc(ExcelUtils.getCastStringValue(row.getCell(1)));
item.setFjgn(ExcelUtils.getCastStringValue(row.getCell(2)));
item.setFjzt(ExcelUtils.getCastStringValue(row.getCell(3)));
item.setJzmj(ExcelUtils.getCastStringValue(row.getCell(4)));
item.setHtmj(ExcelUtils.getCastStringValue(row.getCell(5)));
item.setTnmj(ExcelUtils.getCastStringValue(row.getCell(6)));
item.setGtmj(ExcelUtils.getCastStringValue(row.getCell(7)));
item.setCqmj(ExcelUtils.getCastStringValue(row.getCell(8)));
item.setJfmj(ExcelUtils.getCastStringValue(row.getCell(9)));
item.setSymj(ExcelUtils.getCastStringValue(row.getCell(10)));
item.setPtmj(ExcelUtils.getCastStringValue(row.getCell(11)));
if(null!=row.getCell(12)) {
Double dGnxs = Double.parseDouble(row.getCell(12).toString());
item.setGnxs(dGnxs.intValue());
}
if(null!=row.getCell(13)) {
Double dFtxs = Double.parseDouble(row.getCell(13).toString());
item.setFtxs(dFtxs.intValue());
}
item.setGjxm(ExcelUtils.getCastStringValue(row.getCell(14)));
item.setGjdh(ExcelUtils.getCastStringValue(row.getCell(15)));
item.setSjtt(ExcelUtils.getCastStringValue(row.getCell(16)));
item.setZdy1(ExcelUtils.getCastStringValue(row.getCell(17)));
item.setZdy2(ExcelUtils.getCastStringValue(row.getCell(18)));
item.setZdy3(ExcelUtils.getCastStringValue(row.getCell(19)));
if(null!=row.getCell(20)) {
Double djxh = Double.parseDouble(row.getCell(20).toString());
item.setFjxh(djxh.intValue());
}
listfj.add(item); //通过房间编号修改 楼层房间信息
}catch(Exception e) {
continue;
}
}
//第二个sheet 业主资料
HSSFSheet sheet1 = wb.getSheetAt(1);
HSSFRow row1 = sheet1.getRow(0);
//int colNum = row.getPhysicalNumberOfCells();
int rowNum1 = sheet1.getPhysicalNumberOfRows();
if(rowNum1<2){
returnInfo.setStatusCode("300");
returnInfo.setMessage("Excel文件中没有业主资料数据");
response.getWriter().write(JSONObject.toJSONString(returnInfo));
}
List<YzZl> listyz = new ArrayList<YzZl>();
for (int i=1; i<rowNum1; i++) {
try {
row1 = sheet1.getRow(i);
YzZl item = new YzZl();
String fjbh = ExcelUtils.getCastStringValue(row1.getCell(0));
if(StringUtils.isBlank(fjbh))
continue;
item.setFjbh(fjbh);
item.setYzmc(ExcelUtils.getCastStringValue(row1.getCell(1)));
// item.setYzbh(ExcelUtils.getCastStringValue(row1.getCell(2)));
item.setYzlx(ExcelUtils.getCastStringValue(row1.getCell(2)));
if(null!=row1.getCell(3)) {
item.setCsny(DateProcessTools.stringToDate(row1.getCell(3).toString(), "yyyy-MM-dd"));
}
if(StringUtils.isNotEmpty(ExcelUtils.getCastStringValue(row1.getCell(4)))) {
if(("女").equals(row1.getCell(4))) {
item.setXb("1");
}else{
item.setXb("0");
}
}else{//性别默认为男
item.setXb("0");
}
item.setLxdh(ExcelUtils.getCastStringValue(row1.getCell(5)));
item.setSfzh(ExcelUtils.getCastStringValue(row1.getCell(6)));
item.setGddh(ExcelUtils.getCastStringValue(row1.getCell(7)));
item.setCzhm(ExcelUtils.getCastStringValue(row1.getCell(8)));
item.setYzbm(ExcelUtils.getCastStringValue(row1.getCell(9)));
item.setDzyx(ExcelUtils.getCastStringValue(row1.getCell(10)));
item.setLxdz(ExcelUtils.getCastStringValue(row1.getCell(11)));
item.setGzdw(ExcelUtils.getCastStringValue(row1.getCell(12)));
item.setKhmc(ExcelUtils.getCastStringValue(row1.getCell(13)));
item.setKhyh(ExcelUtils.getCastStringValue(row1.getCell(14)));
item.setYhzh(ExcelUtils.getCastStringValue(row1.getCell(15)));
item.setBz(ExcelUtils.getCastStringValue(row1.getCell(16)));
listyz.add(item);
}catch(Exception e) {
continue;
}
}
//第三个sheet 租户资料
HSSFSheet sheet2 = wb.getSheetAt(2);
HSSFRow row2 = sheet2.getRow(0);
//int colNum = row.getPhysicalNumberOfCells();
int rowNum2 = sheet2.getPhysicalNumberOfRows();
if(rowNum2<2){
returnInfo.setStatusCode("300");
returnInfo.setMessage("Excel文件中没有租户资料数据");
response.getWriter().write(JSONObject.toJSONString(returnInfo));
}
List<ZhLb> listzh = new ArrayList<ZhLb>();
for (int i=1; i<rowNum; i++) {
try {
row2 = sheet2.getRow(i);
ZhLb item = new ZhLb();
String fjbh = ExcelUtils.getCastStringValue(row2.getCell(0));
if(StringUtils.isBlank(fjbh))
continue;
item.setFjbh(fjbh);
item.setZhmc(ExcelUtils.getCastStringValue(row2.getCell(1)));
/*item.setZhbh(ExcelUtils.getCastStringValue(row2.getCell(2)));*/
item.setZhlx(ExcelUtils.getCastStringValue(row2.getCell(2)));
if(null!=row2.getCell(3)) {
item.setCsny(DateProcessTools.stringToDate(row2.getCell(3).toString(), "yyyy-MM-dd"));
}
if(StringUtils.isNotEmpty(ExcelUtils.getCastStringValue(row2.getCell(4)))) {
if(("女").equals(row2.getCell(4))) {
item.setXb("1");
}else{
item.setXb("0");
}
}else{//性别默认为男
item.setXb("0");
}
item.setLxdh(ExcelUtils.getCastStringValue(row2.getCell(5)));
item.setLxdz(ExcelUtils.getCastStringValue(row2.getCell(6)));
item.setSfzh(ExcelUtils.getCastStringValue(row2.getCell(7)));
item.setGddh(ExcelUtils.getCastStringValue(row2.getCell(8)));
item.setCzhm(ExcelUtils.getCastStringValue(row2.getCell(9)));
item.setYzbm(ExcelUtils.getCastStringValue(row2.getCell(10)));
item.setDzyx(ExcelUtils.getCastStringValue(row2.getCell(11)));
if(null!=row2.getCell(12)) {
item.setHtqsrq(DateProcessTools.stringToDate(row2.getCell(12).toString(), "yyyy-MM-dd"));
}
if(null!=row2.getCell(13)) {
item.setHtzzrq(DateProcessTools.stringToDate(row2.getCell(13).toString(), "yyyy-MM-dd"));
}
if(null!=row2.getCell(14)) {
item.setQyrq(DateProcessTools.stringToDate(row2.getCell(14).toString(), "yyyy-MM-dd"));
}
item.setGsh(ExcelUtils.getCastStringValue(row2.getCell(15)));
item.setGzdw(ExcelUtils.getCastStringValue(row2.getCell(16)));
item.setDsh(ExcelUtils.getCastStringValue(row2.getCell(17)));
item.setYyzzh(ExcelUtils.getCastStringValue(row2.getCell(18)));
item.setBz(ExcelUtils.getCastStringValue(row2.getCell(19)));
listzh.add(item);
}
catch(Exception e) {
continue;
}
}
//字节流关闭
is.close();
fjLbService.saveFjLb(listfj, listyz, listzh);
//yzZlService.saveYzZl(list);
returnInfo.setStatusCode("200");
returnInfo.setRel("lplb_ent_save_info");
returnInfo.setMessage("上传成功,共插入"+listfj.size()+listyz.size()+listzh.size()+" 条数据");
// 删除上传文件
FileUtil.delete(filePath);
}
}catch(Exception e) {
returnInfo.setStatusCode("300");
returnInfo.setMessage("上传Excel文件失败!");
e.printStackTrace();
}
response.getWriter().write(JSONObject.toJSONString(returnInfo));
}