导出EXCEL上传 服务器 添加压缩包,获取文件 大小
Posted JLCUI
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出EXCEL上传 服务器 添加压缩包,获取文件 大小相关的知识,希望对你有一定的参考价值。
package net.joystart.excelTask; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import com.jcraft.jsch.ChannelSftp; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Colour; import jxl.format.UnderlineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFeatures; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import net.joystart.common.util.ConfigUtil; import net.joystart.common.util.SftpUtil; import net.joystart.common.util.ZipUtil; import net.joystart.customer.entity.Customer; import net.joystart.customer.service.ICustomerService; import net.joystart.excelTask.entity.CownExcel; import net.joystart.order.entity.Order; import net.joystart.order.service.IOrderService; import net.joystart.vehicle.controller.VehicleController; import net.joystart.vehicle.entity.Parkinglot; import net.joystart.vehicle.entity.Vehicle; import net.joystart.vehicle.service.IParkinglotService; import net.joystart.vehicle.service.IVehicleService; @Component("ExcelController") public class ExcelController { @Resource private ICustomerService customerService; @Resource private IParkinglotService parkinglotService; @Resource IVehicleService vehicleService; @Resource IOrderService orderService; /** * 订单自动服务 */ public void customerExcel() { Map<String, Object> params = new HashMap<String, Object>(); List<Customer> customerList = customerService.list(params); exportExcel("会员信息.xls", customerList); } /** */ /** * 导出数据为XLS格式 * * @param fileName * 文件的名称,可以设为绝对路径,也可以设为相对路径 * @param content * 数据的内容 */ private void exportExcel(String fileName, List<Customer> customers) { WritableWorkbook wwb; FileOutputStream fos; try { String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, newDate+".xls"); //f.createNewFile(); fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableSheet ws = wwb.createSheet("会员列表", 0); // 创建一个工作表 // 设置单元格的文字格式 WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); ws.setRowView(1, 500); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 // 设置标题行 int rowsCount = customers.size(); // 填充数据的内容 Customer customer; for (int i = 0; i < rowsCount; i++) { customer = customers.get(i); WritableCellFeatures x; ws.addCell(new Label(0, i + 0, customer.getName(), wcf)); ws.addCell(new Label(1, i + 0, customer.getMobile(), wcf)); double orderCost = customer.getRemainmoney() == null ? 0.00 : customer.getRemainmoney().doubleValue(); jxl.write.Number labelOrderCost = new jxl.write.Number(2, i + 1, orderCost, wcfN); // 格式化数值 ws.addCell(labelOrderCost); } wwb.write(); wwb.close(); FileInputStream fileInputStream = new FileInputStream(new File(fileName)); Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 设置显示格式 String nowTime = ""; nowTime = df.format(dt);// 用DateFormat的format()方法在dt中获取并以yyyy/MM/dd fileName = "用户信息" + nowTime + ".xls"; // 保存EXCEL内容 CownExcel ce = new CownExcel(); ce.setDownloaddate(new Date()); ce.setUrl(f.getPath()); ce.setFileName(newDate+".xls"); customerService.insertSelective(ce); } catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } } public SftpUtil getSFTPChannel() { return new SftpUtil(); } // 导出运管需要的数据 public void exportTransportationExcel() { WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_DEP_"+newDate+".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁企业信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 添加企业信息//////////////////////////////////////////////////////////////// // 设置标题行 WritableCellFeatures x; ws.addCell(new Label(0, 0, "北京巴歌汽车租赁有限公司", wcf)); ws.addCell(new Label(1, 0, "001615", wcf)); ws.addCell(new Label(2, 0, "北京", wcf)); ws.addCell(new Label(3, 0, "北京市", wcf)); //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); exportTransportationExcelNew(lsize); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { } catch (Exception e) { e.printStackTrace(); } } private void exportTransportationExcelNew(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_DEP_"+newDate+"_"+lsize+"" + ".csv"); try { // f.createNewFile(); fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁企业信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 添加企业信息//////////////////////////////////////////////////////////////// WritableCellFeatures x; ws.addCell(new Label(0, 0, "北京巴歌汽车租赁有限公司", wcf)); ws.addCell(new Label(1, 0, "001615", wcf)); ws.addCell(new Label(2, 0, "北京", wcf)); ws.addCell(new Label(3, 0, "北京市", wcf)); wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } ///获取文件大小///////////////////////////////////////////////////////////////////// ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_DEP_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String zipNameNew = "COL_DEP_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath());
fZ.delete();//删除旧压缩包 // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //////////////////////////////////////////////////////////////////////////////// ExcelMethod1(); ExcelMethod2(); ExcelMethod3(); ExcelMethod4(); ExcelMethod5(); } private void ExcelMethod1() { WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_DEP_"+newDate+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁门店信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Parkinglot> parklotList = parkinglotService.findAllParkingLotAndBranch(); for (int i = 0; i < parklotList.size(); i++) { Parkinglot parking = parklotList.get(i); ws.addCell(new Label(0, i + 0, parking.getName(), wcf)); ws.addCell(new Label(1, i + 0, "北京巴歌汽车租赁有限公司", wcf)); ws.addCell(new Label(2, i + 0, parking.getLongitude(), wcf)); ws.addCell(new Label(3, i + 0, parking.getLatitude(), wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, parking.getPosition(), wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); ExcelMethod1New(lsize); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } } private void ExcelMethod1New(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_DEP_"+newDate+"_"+lsize+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁门店信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Parkinglot> parklotList = parkinglotService.findAllParkingLotAndBranch(); for (int i = 0; i < parklotList.size(); i++) { Parkinglot parking = parklotList.get(i); ws.addCell(new Label(0, i + 0, parking.getName(), wcf)); ws.addCell(new Label(1, i + 0, "北京巴歌汽车租赁有限公司", wcf)); ws.addCell(new Label(2, i + 0, parking.getLongitude(), wcf)); ws.addCell(new Label(3, i + 0, parking.getLatitude(), wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, parking.getPosition(), wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); } wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_DEP_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { e1.printStackTrace(); } String zipNameNew = "COL_DEP_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath());
fZ.delete();//删除旧压缩包 // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //////////////////////////////////////////////////////////////////////////////// } private void ExcelMethod2() { WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_CAR_"+newDate+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); WritableSheet ws = wwb.createSheet("租赁车辆基本信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Vehicle> vehicleList = vehicleService.selectAllVehicle(); for (int i = 0; i < vehicleList.size(); i++) { Vehicle vehicle = vehicleList.get(i); ws.addCell(new Label(0, i + 0, vehicle.getPlatenumber(), wcf)); ws.addCell(new Label(1, i + 0, vehicle.getEngineno(), wcf)); ws.addCell(new Label(2, i + 0, vehicle.getVin(), wcf)); ws.addCell(new Label(3, i + 0, vehicle.getUploadImgUrl(), wcf)); ws.addCell(new Label(4, i + 0, vehicle.getUploadImgUrlTwo(), wcf)); ws.addCell(new Label(5, i + 0, vehicle.getCarcolor(), wcf)); ws.addCell(new Label(6, i + 0, vehicle.getDrivinglicenseurl(), wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); ws.addCell(new Label(8, i + 0, " ", wcf)); ws.addCell(new Label(9, i + 0, "5", wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, vehicle.getCreatedate()==null?"":sdf.format(vehicle.getCreatedate()), wcf)); ws.addCell(new Label(12, i + 0, " ", wcf)); ws.addCell(new Label(13, i + 0, " ", wcf)); } wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); ExcelMethod2New(lsize); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } } private void ExcelMethod2New(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_CAR_"+newDate+"_"+lsize+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁车辆基本信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Vehicle> vehicleList = vehicleService.selectAllVehicle(); for (int i = 0; i < vehicleList.size(); i++) { Vehicle vehicle = vehicleList.get(i); ws.addCell(new Label(0, i + 0, vehicle.getPlatenumber(), wcf)); ws.addCell(new Label(1, i + 0, vehicle.getEngineno(), wcf)); ws.addCell(new Label(2, i + 0, vehicle.getVin(), wcf)); ws.addCell(new Label(3, i + 0, vehicle.getUploadImgUrl(), wcf)); ws.addCell(new Label(4, i + 0, vehicle.getUploadImgUrlTwo(), wcf)); ws.addCell(new Label(5, i + 0, vehicle.getCarcolor(), wcf)); ws.addCell(new Label(6, i + 0, vehicle.getDrivinglicenseurl(), wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); ws.addCell(new Label(8, i + 0, " ", wcf)); ws.addCell(new Label(9, i + 0, "5", wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, vehicle.getCreatedate()==null?"":sdf.format(vehicle.getCreatedate()), wcf)); ws.addCell(new Label(12, i + 0, " ", wcf)); ws.addCell(new Label(13, i + 0, " ", wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_CAR_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //新压缩包名称 String zipNameNew = "COL_CAR_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath()); // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //////////////////////////////////////////////////////////////////////////////// } private void ExcelMethod3() { WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_ASS_"+newDate+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁车辆归属信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Vehicle> vehicleList = vehicleService.selectAllVehicleSite(); for (int i = 0; i < vehicleList.size(); i++) { Vehicle vehicle = vehicleList.get(i); ws.addCell(new Label(0, i + 0, vehicle.getPlatenumber(), wcf)); ws.addCell(new Label(1, i + 0, " ", wcf)); ws.addCell(new Label(2, i + 0, StringUtils.isBlank(vehicle.getUploadImgUrlTwo()) ? vehicle.getUploadImgUrl() : vehicle.getUploadImgUrlTwo(), wcf)); ws.addCell(new Label(3, i + 0, vehicle.getDrivinglicenseurl(), wcf)); } wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); ExcelMethod3New(lsize); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } } private void ExcelMethod3New(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_ASS_"+newDate+"_"+lsize+"" + ".csv"); try { // f.createNewFile(); fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁车辆归属信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// List<Vehicle> vehicleList = vehicleService.selectAllVehicleSite(); for (int i = 0; i < vehicleList.size(); i++) { Vehicle vehicle = vehicleList.get(i); ws.addCell(new Label(0, i + 0, vehicle.getPlatenumber(), wcf)); ws.addCell(new Label(1, i + 0, " ", wcf)); ws.addCell(new Label(2, i + 0, StringUtils.isBlank(vehicle.getUploadImgUrlTwo()) ? vehicle.getUploadImgUrl() : vehicle.getUploadImgUrlTwo(), wcf)); ws.addCell(new Label(3, i + 0, vehicle.getDrivinglicenseurl(), wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_ASS_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //新压缩包名称 String zipNameNew = "COL_ASS_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath()); // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void ExcelMethod4() { WritableWorkbook wwb; FileOutputStream fos; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_CON_"+newDate+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁合同信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// Map<String, Object> params = new HashMap<String, Object>(); Calendar calendar_as = Calendar.getInstance(); calendar_as.setTime(new Date()); Calendar calendar_ST = Calendar.getInstance(); calendar_ST.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE) - 1, 0, 0, 0); calendar_ST.set(Calendar.MILLISECOND, 0); Calendar calendar_ET = Calendar.getInstance(); calendar_ET.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE), 0, 0, 0); calendar_ET.set(Calendar.MILLISECOND, 0); SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); params.put("_startDate", dft.format(calendar_ST.getTime())); params.put("_endDate", dft.format(calendar_ET.getTime())); List<Order> orderList = orderService.selectAllOrders(params); for (int i = 0; i < orderList.size(); i++) { Order order = orderList.get(i); ws.addCell(new Label(0, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(1, i + 0, order.getPlatenumber(), wcf)); ws.addCell(new Label(2, i + 0, " ", wcf)); ws.addCell(new Label(3, i + 0, " ", wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, " ", wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, order.getCreatedate()==null? " ":sdf.format(order.getCreatedate()), wcf)); ws.addCell(new Label(8, i + 0, order.getReturnvehicledate() == null ? " " : sdf.format(order.getReturnvehicledate()), wcf)); ws.addCell(new Label(9, i + 0, StringUtils.isBlank(order.getRentBranchName()) ? order.getRentParkName() : order.getRentBranchName(), wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, order.getCreatedate()==null?"":sdf.format(order.getCreatedate()), wcf)); ws.addCell(new Label(12, i + 0, order.getCustomerName(), wcf)); ws.addCell(new Label(13, i + 0, order.getInvitationCode(), wcf)); ws.addCell(new Label(14, i + 0, " ", wcf)); ws.addCell(new Label(15, i + 0, " ", wcf)); ws.addCell(new Label(16, i + 0, " ", wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); ExcelMethod4New(lsize); } catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } catch (Exception e1) { e1.printStackTrace(); } } private void ExcelMethod4New(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_CON_"+newDate+"_"+lsize+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁合同信息", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// Map<String, Object> params = new HashMap<String, Object>(); Calendar calendar_as = Calendar.getInstance(); calendar_as.setTime(new Date()); Calendar calendar_ST = Calendar.getInstance(); calendar_ST.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE) - 1, 0, 0, 0); calendar_ST.set(Calendar.MILLISECOND, 0); Calendar calendar_ET = Calendar.getInstance(); calendar_ET.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE), 0, 0, 0); calendar_ET.set(Calendar.MILLISECOND, 0); SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); params.put("_startDate", dft.format(calendar_ST.getTime())); params.put("_endDate", dft.format(calendar_ET.getTime())); List<Order> orderList = orderService.selectAllOrders(params); for (int i = 0; i < orderList.size(); i++) { Order order = orderList.get(i); ws.addCell(new Label(0, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(1, i + 0, order.getPlatenumber(), wcf)); ws.addCell(new Label(2, i + 0, " ", wcf)); ws.addCell(new Label(3, i + 0, " ", wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, " ", wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, order.getCreatedate()==null? " ":sdf.format(order.getCreatedate()), wcf)); ws.addCell(new Label(8, i + 0, order.getReturnvehicledate() == null ? " " : sdf.format(order.getReturnvehicledate()), wcf)); ws.addCell(new Label(9, i + 0, StringUtils.isBlank(order.getRentBranchName()) ? order.getRentParkName() : order.getRentBranchName(), wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, order.getCreatedate()==null?"":sdf.format(order.getCreatedate()), wcf)); ws.addCell(new Label(12, i + 0, order.getCustomerName(), wcf)); ws.addCell(new Label(13, i + 0, order.getInvitationCode(), wcf)); ws.addCell(new Label(14, i + 0, " ", wcf)); ws.addCell(new Label(15, i + 0, " ", wcf)); ws.addCell(new Label(16, i + 0, " ", wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_CON_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //新压缩包名称 String zipNameNew = "COL_CON_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath()); // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void ExcelMethod5() { WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_WAY_"+newDate+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁结算单", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// Map<String, Object> params = new HashMap<String, Object>(); Calendar calendar_as = Calendar.getInstance(); calendar_as.setTime(new Date()); Calendar calendar_ST = Calendar.getInstance(); calendar_ST.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE) - 1, 0, 0, 0); calendar_ST.set(Calendar.MILLISECOND, 0); Calendar calendar_ET = Calendar.getInstance(); calendar_ET.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE), 0, 0, 0); calendar_ET.set(Calendar.MILLISECOND, 0); SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); params.put("_startDate", dft.format(calendar_ST.getTime())); params.put("_endDate", dft.format(calendar_ET.getTime())); List<Order> orderList = orderService.selectAllOrders(params); for (int i = 0; i < orderList.size(); i++) { Order order = orderList.get(i); ws.addCell(new Label(0, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(1, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(2, i + 0, order.getPlatenumber(), wcf)); ws.addCell(new Label(3, i + 0, " ", wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, " ", wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); ws.addCell(new Label(8, i + 0, order.getChargingstartdate()==null?"":sdf.format(order.getChargingstartdate()), wcf)); ws.addCell(new Label(9, i + 0, order.getReturnvehicledate() == null ? " " :sdf.format(order.getReturnvehicledate()), wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, " ", wcf)); ws.addCell(new Label(12, i + 0, order.getMileage() == null ? "0" : order.getMileage().toString(), wcf)); ws.addCell(new Label(13, i + 0, order.getActualpaymentcost() == null ? "0" : order.getActualpaymentcost().toString(), wcf)); } wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } String lsize = String.valueOf(f.length()); f.delete(); ExcelMethod5New(lsize); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } } private void ExcelMethod5New(String lsize){ WritableWorkbook wwb; FileOutputStream fos; String savePath = ConfigUtil.pro.get("excelPath").toString(); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDate = dateFormat.format(date); File f = new File(savePath); f = new File(f, "COL_WAY_"+newDate+"_"+lsize+"" + ".csv"); try { fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos); WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcf = new WritableCellFormat(wf); wcf.setVerticalAlignment(VerticalAlignment.CENTRE); wcf.setAlignment(Alignment.CENTRE); jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00"); // 设置数字格式 jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf); // 设置格式 WritableSheet ws = wwb.createSheet("租赁结算单", 0); // 创建一个工作表 ws.setRowView(1, 500); /// 租赁门店信息//////////////////////////////////////////////////////////////// Map<String, Object> params = new HashMap<String, Object>(); Calendar calendar_as = Calendar.getInstance(); calendar_as.setTime(new Date()); Calendar calendar_ST = Calendar.getInstance(); calendar_ST.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE) - 1, 0, 0, 0); calendar_ST.set(Calendar.MILLISECOND, 0); Calendar calendar_ET = Calendar.getInstance(); calendar_ET.set(calendar_as.get(Calendar.YEAR), calendar_as.get(Calendar.MONTH), calendar_as.get(Calendar.DATE), 0, 0, 0); calendar_ET.set(Calendar.MILLISECOND, 0); SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); params.put("_startDate", dft.format(calendar_ST.getTime())); params.put("_endDate", dft.format(calendar_ET.getTime())); List<Order> orderList = orderService.selectAllOrders(params); for (int i = 0; i < orderList.size(); i++) { Order order = orderList.get(i); ws.addCell(new Label(0, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(1, i + 0, order.getOrderno(), wcf)); ws.addCell(new Label(2, i + 0, order.getPlatenumber(), wcf)); ws.addCell(new Label(3, i + 0, " ", wcf)); ws.addCell(new Label(4, i + 0, " ", wcf)); ws.addCell(new Label(5, i + 0, " ", wcf)); ws.addCell(new Label(6, i + 0, " ", wcf)); ws.addCell(new Label(7, i + 0, " ", wcf)); ws.addCell(new Label(8, i + 0, order.getChargingstartdate()==null?"":sdf.format(order.getChargingstartdate()), wcf)); ws.addCell(new Label(9, i + 0, order.getReturnvehicledate() == null ? " " : sdf.format(order.getReturnvehicledate()), wcf)); ws.addCell(new Label(10, i + 0, " ", wcf)); ws.addCell(new Label(11, i + 0, " ", wcf)); ws.addCell(new Label(12, i + 0, order.getMileage() == null ? "0" : order.getMileage().toString(), wcf)); ws.addCell(new Label(13, i + 0, order.getActualpaymentcost() == null ? "0" : order.getActualpaymentcost().toString(), wcf)); } //////////////////////////////////////////////////////////////////////////// wwb.write(); wwb.close(); fos.flush(); if(fos!=null){ fos.close(); } } catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } ///添加压缩功能///////////////////////////////////////////////////////////////////// String zipName = "COL_WAY_"+newDate+"_"+lsize+".zip"; File fZ = new File(savePath); fZ = new File(fZ, zipName); ZipUtil zip =new ZipUtil(); try { zip.zip(fZ.getPath(), f.getPath()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //新压缩包名称 String zipNameNew = "COL_WAY_"+newDate+"_"+fZ.length()+".zip"; File fZNew = new File(savePath); fZNew = new File(fZNew, zipNameNew); fileupload(fZ.getPath(),fZNew.getPath()); // 调用sftp开始上传文件////////////////////////////////////////////////////////////// VehicleController vehicleController = new VehicleController(); SftpUtil channel = vehicleController.getSFTPChannel(); try { ChannelSftp chSftp = channel.getChannel(60000); String strSaveUrl = "/COLLECTION/" + fZ.getName(); chSftp.put(fZ.getPath(), strSaveUrl, ChannelSftp.OVERWRITE); chSftp.quit(); channel.closeChannel(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取文件大小 重新写入磁盘 * @author:cuijinlong * @date:2017年5月18日 下午1:58:37 * @param url1 * @param url2 */ private void fileupload(String url1,String url2){ File f = new File(url1);//旧文件路径 InputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(f);//输入流把旧文件写到流里面 int tempbyte; System.out.println(in.available()); byte[] b = new byte[in.available()]; while ((tempbyte = in.read(b, 0,b.length)) != -1) { System.out.write(tempbyte);/写入文件内容 } File f2 = new File(url2);/新文件路径 out = new FileOutputStream(f2); out.write(b); out.flush(); f.delete();/删除旧文件 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } if(out != null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
以上是关于导出EXCEL上传 服务器 添加压缩包,获取文件 大小的主要内容,如果未能解决你的问题,请参考以下文章