java 导出CSV格式文件 EXCEL打开 防止乱码

package net.joystart.excelTask;

import java.io.BufferedWriter;
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.io.OutputStreamWriter;
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 com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

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();
			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() {

		try {
			String savePath = ConfigUtil.pro.get("excelPath").toString();
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String newDate = dateFormat.format(date);

			String str = "北京巴歌汽车租赁有限公司,001615,北京,北京市";
			byte[] data = str.getBytes("utf-8");
			String url = new StringBuffer(savePath).append("/COL_DEP_").append(newDate).append("_").append(data.length)
					.append(".csv").toString();
			fileOutupload(data, url);

			/// 添加压缩功能/////////////////////////////////////////////////////////////////////
			String zipName = new StringBuffer().append("/COL_DEP_").append(newDate).append(".zip").toString();
			String zipUrl = new StringBuffer(savePath).append(zipName).toString();
			ZipUtil zip = new ZipUtil();
			zip.zip(zipUrl, url);
			File csvFile = new File(url);
			File zipFile = new File(zipUrl);
			if (csvFile.exists())
				csvFile.delete();// 删除旧压缩包

			byte[] file = fileInupload(zipUrl);
			String zipNameNew = new StringBuffer(savePath).append("/COL_DEP_").append(newDate).append("_")
					.append(file.length).append(".zip").toString();
			fileOutupload(file, zipNameNew);
			zipFile.delete();
			// 调用sftp开始上传文件//////////////////////////////////////////////////////////////
			VehicleController vehicleController = new VehicleController();
			SftpUtil channel = vehicleController.getSFTPChannel();
			ChannelSftp chSftp = channel.getChannel(60000);
			String strSaveUrl = new StringBuffer("/COLLECTION/").append(zipNameNew).toString();
			chSftp.put(zipNameNew, strSaveUrl, ChannelSftp.OVERWRITE);
			chSftp.quit();
			channel.closeChannel();
		} catch (JSchException e) {
			e.printStackTrace();
		} catch (SftpException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		ExcelMethod1();
		ExcelMethod2();
		ExcelMethod3();
		ExcelMethod4();
		ExcelMethod5();
	}

	private void ExcelMethod1(){
		FileOutputStream out=null;
	    OutputStreamWriter osw=null;
	    BufferedWriter bw=null;
	    String urlNew="";
		String savePath = ConfigUtil.pro.get("excelPath").toString();
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String newDate = dateFormat.format(date);
	    String url  = new StringBuffer(savePath).append("/COL_DEP_").append(newDate).append(".csv").toString();
	    File csvFile = new File(url);
	    try {
            out = new FileOutputStream(csvFile);
            osw = new OutputStreamWriter(out,"UTF-8");       //识别UTF-8 加上就能识别出来,不加还是乱码  add cjl            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
            List<Parkinglot> parklotList = parkinglotService.findAllParkingLotAndBranch();
            if(parklotList!=null && !parklotList.isEmpty()){
                for(Parkinglot parking : parklotList){
                	String strData = new StringBuffer().append(parking.getName()).append(",").append("北京巴歌汽车租赁有限公司").append(",").
                			append(parking.getLongitude()).append(",").append(parking.getLatitude()).append(",").append("null").append(",").append(parking.getPosition())
                			.append(",").append("null").append(",").append("null").toString();
                    bw.append(strData).append("\r");
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            long fileSize = csvFile.length();
            urlNew  = new StringBuffer(savePath).append("/COL_DEP_").append(newDate).append("_").append(fileSize).append(".csv").toString();
            fileupload(url,urlNew);
            csvFile.delete();
        }
	    zipUpload(newDate,savePath,urlNew,"/COL_DEP_");
	}

	private void ExcelMethod2() {
		FileOutputStream out=null;
	    OutputStreamWriter osw=null;
	    BufferedWriter bw=null;
	    String urlNew="";
		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);
	    String url  = new StringBuffer(savePath).append("/COL_CAR_").append(newDate).append(".csv").toString();
	    File csvFile = new File(url);

	    try {
            out = new FileOutputStream(csvFile);
            osw = new OutputStreamWriter(out,"UTF-8");
            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
			/// 租赁门店信息////////////////////////////////////////////////////////////////
			List<Vehicle> vehicleList = vehicleService.selectAllVehicle();
            if(vehicleList!=null && !vehicleList.isEmpty()){
                for(Vehicle vehicle : vehicleList){
                	String strData = new StringBuffer().append(vehicle.getPlatenumber()).append(",").append(vehicle.getEngineno()).append(",").
                			append(vehicle.getVin()).append(",").append(vehicle.getUploadImgUrl()).append(",").append(vehicle.getUploadImgUrlTwo()).append(",").append(vehicle.getCarcolor())
                			.append(",").append(vehicle.getDrivinglicenseurl()).append(",").append("null").append(",")
                			.append("null").append(",").append("5").append(",").append("null").append(",").append(vehicle.getCreatedate() == null ? "" : sdf.format(vehicle.getCreatedate())).append(",")
                			.append("null").append(",").append("null").toString();
                    bw.append(strData).append("\r");
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            long fileSize = csvFile.length();
            urlNew  = new StringBuffer(savePath).append("/COL_CAR_").append(newDate).append("_").append(fileSize).append(".csv").toString();
            fileupload(url,urlNew);
            csvFile.delete();
        }
	    zipUpload(newDate,savePath,urlNew,"/COL_CAR_");
	}

	private void ExcelMethod3() {
		FileOutputStream out=null;
	    OutputStreamWriter osw=null;
	    BufferedWriter bw=null;
	    String urlNew="";
		String savePath = ConfigUtil.pro.get("excelPath").toString();
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String newDate = dateFormat.format(date);
	    String url  = new StringBuffer(savePath).append("/COL_ASS_").append(newDate).append(".csv").toString();
	    File csvFile = new File(url);

	    try {
            out = new FileOutputStream(csvFile);
            osw = new OutputStreamWriter(out,"UTF-8");
            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
            List<Vehicle> vehicleList = vehicleService.selectAllVehicleSite();
            if(vehicleList!=null && !vehicleList.isEmpty()){
                for(Vehicle vehicle : vehicleList){
                	String strData = new StringBuffer().append(vehicle.getPlatenumber()).append(",").append("null").append(
                			StringUtils.isBlank(vehicle.getUploadImgUrlTwo())? vehicle.getUploadImgUrl() : vehicle.getUploadImgUrlTwo())
                			.append(",").append(vehicle.getDrivinglicenseurl()).toString();
                    bw.append(strData).append("\r");
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            long fileSize = csvFile.length();
            urlNew  = new StringBuffer(savePath).append("/COL_CAR_").append(newDate).append("_").append(fileSize).append(".csv").toString();
            fileupload(url,urlNew);
            csvFile.delete();
        }
	    zipUpload(newDate,savePath,urlNew,"/COL_CAR_");
	}
	private void ExcelMethod4() {
		FileOutputStream out=null;
	    OutputStreamWriter osw=null;
	    BufferedWriter bw=null;
	    String urlNew="";
		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);
	    String url  = new StringBuffer(savePath).append("/COL_CON_").append(newDate).append(".csv").toString();
	    File csvFile = new File(url);

	    try {
            out = new FileOutputStream(csvFile);
            osw = new OutputStreamWriter(out,"UTF-8");
            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
			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);
            if(orderList!=null && !orderList.isEmpty()){
                for(Order order : orderList){
                	String strData = new StringBuffer().append(order.getOrderno()).append(",").append(order.getPlatenumber()).append(",")
                			.append("null,null,null,null,null,").append(order.getCreatedate() == null ? " " : sdf.format(order.getCreatedate()))
                			.append(",").append(order.getReturnvehicledate() == null ? " " : sdf.format(order.getReturnvehicledate()))
                			.append(",").append(StringUtils.isBlank(order.getRentBranchName()) ? order.getRentParkName()
            						: order.getRentBranchName()).append(",").append("null").append(order.getCreatedate() == null ? "" : sdf.format(order.getCreatedate()))
                			.append(",").append(order.getCustomerName()).append(",").append(order.getInvitationCode())
                			.append(",null,null,null").toString();
                    bw.append(strData).append("\r");
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            long fileSize = csvFile.length();
            urlNew  = new StringBuffer(savePath).append("/COL_CON_").append(newDate).append("_").append(fileSize).append(".csv").toString();
            fileupload(url,urlNew);
            csvFile.delete();
        }
	    zipUpload(newDate,savePath,urlNew,"/COL_CON_");
	}

	private void ExcelMethod5() {
		FileOutputStream out=null;
	    OutputStreamWriter osw=null;
	    BufferedWriter bw=null;
	    String urlNew="";
		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);
	    String url  = new StringBuffer(savePath).append("/COL_WAY_").append(newDate).append(".csv").toString();
	    File csvFile = new File(url);

	    try {
            out = new FileOutputStream(csvFile);
            osw = new OutputStreamWriter(out,"UTF-8");
            //识别UTF-8 加上就能识别出来,不加还是乱码  add cjl
            osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
            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);
            if(orderList!=null && !orderList.isEmpty()){
                for(Order order : orderList){
                	String strData = new StringBuffer().append(order.getOrderno()).append(",").append(order.getOrderno()).append(",")
                			.append("null,null,null,null,null,").append(order.getCreatedate() == null ? " " : sdf.format(order.getCreatedate()))
                			.append(order.getPlatenumber()).append(",null,null,null,null,null,").append(order.getChargingstartdate() == null ? "" : sdf.format(order.getChargingstartdate()))
                			.append(",").append(order.getReturnvehicledate() == null ? " " : sdf.format(order.getReturnvehicledate()))
                			.append(",").append("null").append(",").append("null").append(",").append(order.getMileage()== null ? "0" : order.getMileage().toString())
                			.append(",").append(order.getActualpaymentcost() == null ? "0" : order.getActualpaymentcost().toString()).toString();
                    bw.append(strData).append("\r");
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            long fileSize = csvFile.length();
            urlNew  = new StringBuffer(savePath).append("/COL_WAY_").append(newDate).append("_").append(fileSize).append(".csv").toString();
            fileupload(url,urlNew);
            csvFile.delete();
        }
	    zipUpload(newDate,savePath,urlNew,"/COL_WAY_");
	}

	/**
	 * 获取文件大小 重新写入磁盘
	 *
	 * @author:cuijinlong
	 * @date:2017年5月18日 下午1:58:37
	 * @param url1
	 * @param url2
	 */
	private void fileOutupload(byte[] b, String url2) {
		FileOutputStream out = null;
		try {
			File f2 = new File(url2);//新文件路径  add 崔金龙

			out = new FileOutputStream(f2);//输出文件
			out.write(b);//写入文件内容 旧文件内容 写入新的里面去  add 崔金龙
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

	/**
	 * 根据路径读取旧文件路径下内容,写入新文件路径下
	 * @author:cuijinlong
	 * @date:2017年5月19日 上午11:26:31
	 * @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);//输入流读出就文件内容  add 崔金龙
			int tempbyte;
			System.out.println(in.available());
			byte[] b = new byte[in.available()]; //读出内容 获取大小  add 崔金龙
			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);//把旧路径内容写入新地址文件去  add 崔金龙
			out.flush();
		} 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();
			}

		}

	}

	/**
	 * 根据URL路径获取当面文件大小
	 * @author:cuijinlong
	 * @date:2017年5月19日 上午11:18:59
	 * @param url1
	 * @return
	 */
	private byte[] fileInupload(String url1) {
		File f = new File(url1);//根据URL路径获取文件信息
		InputStream in = null;
		try {
			in = new FileInputStream(f);
			int tempbyte;
			System.out.println(in.available());
			byte[] b = new byte[in.available()];//读出文件里面内容并获取文件大小  注释:b:为内容  b.length() 获取文件大小 add 崔金龙
			while ((tempbyte = in.read(b, 0, b.length)) != -1) {
				System.out.write(tempbyte);
			}
			return b;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return null;
	}
	/**
	 * 上传压缩包文件变并且替换
	 * @author:cuijinlong
	 * @date:2017年5月19日 上午10:35:34
	 * @param newDate
	 * @param savePath//上传文件路径
	 * @param urlNew//新的上传文件地址加上CSV文件名称
	 * @param flag
	 */
	private void zipUpload(String newDate,String savePath,String urlNew,String flag){
		try {
			//拼接压缩包名称 add cjl
			String zipName = new StringBuffer().append(flag).append(newDate).append(".zip").toString();
			String zipUrl = new StringBuffer(savePath).append(zipName).toString();
			ZipUtil zip = new ZipUtil();
			zip.zip(zipUrl, urlNew);
			File zipFile = new File(zipUrl);//生成压缩包路径带上压缩包名称  add 崔金龙
			File csvFileNew = new File(urlNew);//为了删除CSV文件时候建立的FILE为了删除使用,如果不删除不需要使用
			if(csvFileNew.exists())
				csvFileNew.delete();
			byte[] file = fileInupload(zipUrl);//获取该路径下文件大小,注意此处一定带着要获取文件的文件名称  add 崔金龙
			String zipNameNew = new StringBuffer(savePath).append(flag).append(newDate).append("_")
					.append(file.length).append(".zip").toString();//file.length 可以获取文件大小,生成一个新的路径带着文件大小的  add 崔金龙
			fileOutupload(file, zipNameNew);//把旧压缩包内容 写入新的压缩包里面去  add 崔金龙
			zipFile.delete();
			// 调用sftp开始上传文件//////////////////////////////////////////////////////////////
			VehicleController vehicleController = new VehicleController();
			SftpUtil channel = vehicleController.getSFTPChannel();
			ChannelSftp chSftp = channel.getChannel(60000);
			String strSaveUrl = new StringBuffer("/COLLECTION/").append(zipNameNew).toString();
			chSftp.put(zipNameNew, strSaveUrl, ChannelSftp.OVERWRITE);
			chSftp.quit();
			channel.closeChannel();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
时间: 2024-10-26 02:33:46

java 导出CSV格式文件 EXCEL打开 防止乱码的相关文章

Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法

测试例子: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 <b>Allerleirauh</b> (2012) <i>dTV - Das Erste - 20. Januar 2013</i> 2 00:00:10,280 --> 00:00:12,680 Was geh?rt zu einer guten Suppe?

JAVA 导出Excel2003格式文件实现代码

1 public String exportExcel(String name, String name2, String name3, 2 String name4, String name5, HttpServletRequest request, 3 HttpServletResponse response){ 4 5 String path = request.getRealPath("/"); 6 List list = this.findVsendList(name,nam

JAVA之编码---- CSV在文本下是正常的,用EXCEL打开是乱码的问题

JAVA之编码---->CSV在文本下是正常的,用EXCEL打开是乱码的问题 在JAVA下输出文件流,保存成CSV(用UTF-8)文件,怎么处理用EXCEL下是乱码,但是在记事本等其他软件都是正常的,同时显示也是UTF-8的编码,经过测试发现如下结果: 1.EXCEL只能打开ANSI的编码,而ANSI需要当前操作系统是什么编码,就用什么编码.如中文系统下,则只能认识GBK的编码,不可能认识UTF-8的编码,因此网上说的增加16进制下的EF BB BF,根本不是解决之道2.我们知道了EXCEL只能

关于CSV文件 Excel打开乱码问题的解决方案

近日写java程序中,将数据输出到csv文件中,发现Excel打开之后,中文均为乱码 于是寻找解决方案,发现最简单的方式还是如此了 1. 将输出的csv文件用记事本打开 2. 另存为将文件编码格式改为UTF-8 3. 再用Excel打开,乱码问题得以解决 ,

我也太牛了,解决了浏览器中,前台导出csv格式,UTF-8编码,且excek打开不乱码!

ExcellentExport.js的方法,利用base64下载文件.支持chrome ,opera,firefox. 于是决定拿来为我所用! 说明一下,这个js的好处是:一句js脚本,就能前台下载,完全无须后台. 但外国人不了解中文的csv用excel打开直接乱码. 但用记事本打开,再直接保存,或另存为ansi都可以让中文不乱码. js里默认应该是utf-8,昨天试了用utf-8转gb2312,失败了! 于是找到这个: utf-8保存的csv格式要让Excel正常打开的话,必须加入在文件最前面

CSVHelper 导出CSV 格式

public class CSVHelper { System.Windows.Forms.SaveFileDialog saveFileDialog1;//保存 private string header = string.Empty;//标题 /// <summary> /// 初始化打印设置 /// </summary> /// <param name="printID"></param> protected void InitEx

PHP导入与导出xml格式的Excel

1 简介 1.1 导出 在实际的工作项目中,经常需要将一些重要的数据库中存的数据导出成Excel,比如导出考勤报表,导出财务报表,导出业绩报表,导出销售报表等.CleverCode以前使用了两年的PHPExcel来制作Excel导出数据,但发现用PHPExcel生成Excel实在是太麻烦了,特别是控制单元格的颜色,合并单元格,给单元格设置长度等.这些设计一个Excel通常会需要花费一天的时间.后来CleverCode发现了一个简便的方法PHP导出xml格式的Excel,以前需要一天的工作量,现在

Python数据写入csv格式文件

(只是传递,基础知识也是根基) Python读取数据,并存入Excel打开的CSV格式文件内! 这里需要用到bs4,csv,codecs,os模块. 废话不多说,直接写代码!该重要的内容都已经注释了,剩下不懂的可以自己查询一下,或者QQ群内问我.QQ群在以往的博客中! 1 #coding:utf-8 2 from bs4 import BeautifulSoup 3 import bs4 4 import os 5 import time 6 import csv 7 import codecs

javacsv生成的csv用excel打开中文乱码

在SourceForge上找到读写csv文件的开源代码. 但是生成的csv文件,如果用excel打开时,中文全部会乱码.原因是excel在解析csv文件时,默认以utf-8带BOM格式去解析的. utf-8保存的csv格式文件要让Excel正常打开的话,必须加入在文件最前面加入BOM(Byte order). 主要修改代码如下: private void checkInit() throws IOException { if (!initialized) { if (fileName != nu