【弱智版**别人不要看**哪里有设置自己可见的啊???】导出excel功能~~~~~~弯路

今天被鄙视死了,我笨是我的错,我也没想出来吓人啊啊啊大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭大哭

1、想让用户打开资源管理器对话框去选择保存路径,遇到诡异问题就是,dialog偶发调不出来。

各方百度后,居然发现这么个结论:“JFileChooser一直有bug,很多win7系统不能正常使用。你试试用FileDialog” 绝望之下,我用了FileDialog还是不行,于是放弃。但据我估计,可能我本身程序那时候有问题,代码先贴上,以后再说。

                // JFileChooser jf = new JFileChooser();
// jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
// int result = jf.showSaveDialog(null);
// if(result != JFileChooser.APPROVE_OPTION){
// return "";
// }
// File fi = jf.getSelectedFile();
// String excelExportPath = fi.getAbsolutePath();


// FileDialog fileDialog = new FileDialog(new Frame(), "保存",
// FileDialog.SAVE);
// fileDialog.setVisible(true);
// String excelExportPath = fileDialog.getDirectory();
// if (excelExportPath == "") {
// System.out
// .println("****************ERROR:the excelExportPath should have a value!!**************");
// }

2、于是想直接把excel保存在服务器上,给用户生成一个链接,他点的时候,系统自动会下载到默认下载目录或者弹出对话框。

machine.js部分

		// 导出机器列表到excel
		$("#exportToExcel").live("click", function(event) {
			event.preventDefault();
			var ajaxExportToExcelLink = $('#ajaxExportToExcelLink').val();
			var machineIp = $("#searchMachineIp").val();
			var fuzzyMachineIp = $("input[name=fuzzyMachineIp]").val();
			var environmentTypeId = $("#envType").val();
			var machineGroupId = $("#machineGroupId").val();
			var machineStatus = $("#machineStatus").val();
			var agentStatus = $("#agentStatus").val();
			var agentVersion = $("#searchAgentVersion").val();
			var parentGroupId = $("#parentGroupId").val();
			var appliedNum= $("#searchAppliedNum").val();						
			if (machineIp!=''&&machineIp!=null)
				{
				var isFuzzyMachineIp = $("input[name=isFuzzyMachineIp]").val();
				}
			var params = {};
			params.machineIp = machineIp;
			params.fuzzyMachineIp = fuzzyMachineIp;
			params.environmentTypeId = environmentTypeId;
			params.machineGroupId = machineGroupId;
			params.machineStatus = machineStatus;
			params.agentStatus = agentStatus;
			params.agentVersion = agentVersion;
			params.parentGroupId = parentGroupId;
			params.appliedNum= appliedNum;
			params.isFuzzyMachineIp = isFuzzyMachineIp;
			$.getJSON(ajaxExportToExcelLink, params, function(data) {
				if (data.result == true) {
					showMsgSuccess(data.msg);
				} else {
					showMsgError();
				}
			});
			
			function showMsgSuccess(content){
			    var opts ={
			    title:'导出成功',
			    content:'导出机器信息成功!  <a href="../machine/excel/'+content+'">点此下载</a>',
			    buttons:{
			    '关闭':function(){
			    jQuery.dialog.close();
			    window.location.reload();
			    }
			    }
			    }
			    jQuery.dialog(opts);
			};
						
			function showMsgError(){
			    var opts ={
			    title:'导出失败',
			    content:'非常抱歉,导出失败,请与管理员联系',
			    buttons:{
			    '关闭':function(){
			    jQuery.dialog.close();
			    window.location.reload();
			    }
			    }
			    }
			    jQuery.dialog(opts);
			};
			
			});
ExportToExcel.java部分

package com.alibaba.china.gaea.env.config.module.screen;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.china.gaea.env.dal.query.MachineQuery;
import com.alibaba.china.gaea.env.service.MachineService;
import com.alibaba.china.gaea.env.service.model.MachineBO;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.TurbineRunData;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.turbine.dataresolver.Params;
import com.alibaba.gaea.module.Page;
import com.alibaba.gaea.module.SimpleJsonResult;
import com.alibaba.gaea.web.tools.JsonResponseUtils;

public class ExportToExcel {

	@Autowired
	private MachineService machineService;
	private static final Log logger = LogFactory.getLog(ExportToExcel.class);

	public void execute(@Params MachineQuery query,
			@Param(name = "isFuzzyMachineIp") Boolean isFuzzyMachineIp,
			Context context, TurbineRunData rundata) throws IOException {

		if (null == query.getPageNum()) {
			query.setPageNum(0);
		}
		if (null == query.getPageSize()) {
			// 我把pageSize设置成null,希望它全查出来??
			query.setPageSize(null);
		}

		if (isFuzzyMachineIp != null && isFuzzyMachineIp == false) {
			// “是否模糊查询”参数不为空 &&
			// 后边精确checkbox框没被选中,意思是模糊查询,就把模糊的fuzzyIP赋值给machineIp
			query.setMachineIp(query.getFuzzyMachineIp());
		}

		Page<MachineBO> page = machineService.queryMachinePageByQuery(query);

		List<MachineBO> machineBOList = page.getItems();
		SimpleJsonResult result = new SimpleJsonResult();
		// 机器列表不为空则输出到excel
		if (machineBOList.isEmpty() == false) {
			try {
				String excelname = machineService
						.exportMachineListToExcel(machineBOList);
				result.setResult(true);
				result.setMsg(excelname);
			} catch (Exception e) {
				logger.error("ExportToExcel is failed!", e);
				result.setResult(false);
				result.setMsg(e.getMessage());
			}
		}
		JsonResponseUtils.responseJson(rundata, result);
	}
}

3、那么文件放在哪里呢? 写死 D 盘?本地windows调试通过,但是linux上没有D盘啊。 尴尬那么放在代码所在路径?

一开始放的是:D:/workspaceutf8/2013.10.14 for xiafeng/gaea.env.war/src/main/webapp/machine/excel  编译打包时会生成对应的execl路径,那个路径用户是可以访问到的,但是生成的链接总是少了最后的文件名,为什么呢?噢~~因为我是编译打包完之后,用户去点“导出”按钮时才会去写这个文件,那么target下面显然没有这文件。所以,其实应该放到用户可以访问的目录里去D:/workspaceutf8/2013.10.14 for xiafeng/gaea.env.war/target/gaea.env.war-0.0.1-SNAPSHOT/machine/excel。本地调试通过,linux不行,为啥?linux配置的路径是:/home/admin/env_src/gaea.env.war/target/gaea.env.war-0.0.1-SNAPSHOT/machine/excel 。这不是他真正的部署目录尴尬

找到他的部署目录/home/admin/web-deploy/jetty_server/webapps,发现里面是个war包,没有解压。。。而且还是从别的地方拷贝过来的,那我在脚本里写解压:

gaea.env.deploy\bin\jettyctl.sh     这文件部署后会变成/home/admin/web-deploy/bin/jettyctl.sh

if  $production_mode ; then
  		## 线上环境,通过link方式部署output/web.war
		rm -rf  "$JETTY_WEBAPPS/autotest.war" 
		##cp  "$DEPLOY_HOME/web.war"  "$JETTY_WEBAPPS/newenv.war"  ## 目前只能拷贝
		mkdir -p "$JETTY_WEBAPPS/newenv"
		unzip "$DEPLOY_HOME/web.war" -d "$JETTY_WEBAPPS/newenv/"
  	else
  		## 开发环境/测试环境
		##cp $DEPLOY_HOME/../*.war  $JETTY_WEBAPPS/newenv.war
		mkdir -p "$JETTY_WEBAPPS/newenv"
		unzip "$DEPLOY_HOME/../*.war" -d "$JETTY_WEBAPPS/newenv/"
  	fi  	
这样,我在linux上就可以直接放到那目录里去了,下面是machineimpl.java部分内容:

	public String exportMachineListToExcel(List<MachineBO> machineBOList) {

		// 定义各列序号
		int MACHINE_NAME = 0, MACHINE_IP = 1, GROUP_NAME = 2, MACHINE_STATUS = 3, APPLY_NUM = 4,

		TYPE_NAME = 5, AGENT_STATUS = 6, AGENT_START_DATE = 7;
		SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

		if (machineBOList.isEmpty()) {
			return "";
		}
		HSSFWorkbook machineWorkBook = new HSSFWorkbook();
		HSSFSheet machineSheet = machineWorkBook
				.createSheet("machineManageList");

		// 创建每个SHEET页的固定列名
		HSSFRow titleRow = machineSheet.createRow(0);
		HSSFCell cell;
		cell = titleRow.createCell(MACHINE_NAME);
		cell.setCellValue("机器名");
		cell = titleRow.createCell(MACHINE_IP);
		cell.setCellValue("机器IP");
		cell = titleRow.createCell(GROUP_NAME);
		cell.setCellValue("机器组");
		cell = titleRow.createCell(MACHINE_STATUS);
		cell.setCellValue("状态");
		cell = titleRow.createCell(APPLY_NUM);
		cell.setCellValue("申请上限");
		cell = titleRow.createCell(TYPE_NAME);
		cell.setCellValue("环境类型");
		cell = titleRow.createCell(AGENT_STATUS);
		cell.setCellValue("agent状态");
		cell = titleRow.createCell(AGENT_START_DATE);
		cell.setCellValue("agent启动时间");

		// 填充每个机器信息到SHEET页
		HSSFRow dataRow;
		for (int i = 1; i <= machineBOList.size(); i++) {
			dataRow = machineSheet.createRow(i);
			MachineBO machineBO = machineBOList.get(i - 1);
			dataRow.createCell(MACHINE_NAME).setCellValue(
					machineBO.getMachineName());
			dataRow.createCell(MACHINE_IP).setCellValue(
					machineBO.getMachineIp());
			dataRow.createCell(GROUP_NAME).setCellValue(
					machineBO.getGroupName());
			dataRow.createCell(MACHINE_STATUS).setCellValue(
					machineBO.getMachineStatus().getDescription());
			dataRow.createCell(APPLY_NUM).setCellValue(
					machineBO.getApplyNum() + "/" + machineBO.getMaxApplyNum());
			dataRow.createCell(TYPE_NAME).setCellValue(machineBO.getTypeName());
			dataRow.createCell(AGENT_STATUS).setCellValue(
					machineBO.getAgentStatus().getDescription());
			if (machineBO.getAgentStartDate() == null) {
				dataRow.createCell(AGENT_START_DATE).setCellValue("null");
			} else {
				dataRow.createCell(AGENT_START_DATE).setCellValue(
						format.format(machineBO.getAgentStartDate()));
			}

		}

		// 将文件存储
		Date now = new Date();
		String excelFileName = "machineList" + format.format(now)
				+ ".xls";
		// excel输出路径暂时先写死D盘
//		String excelExportPath = "D:";

		// JFileChooser jf = new JFileChooser();
		// jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
		// int result = jf.showSaveDialog(null);
		// if(result != JFileChooser.APPROVE_OPTION){
		// return "";
		// }
		// File fi = jf.getSelectedFile();
		// String excelExportPath = fi.getAbsolutePath();

		// FileDialog fileDialog = new FileDialog(new Frame(), "保存",
		// FileDialog.SAVE);
		// fileDialog.setVisible(true);
		// String excelExportPath = fileDialog.getDirectory();
		// if (excelExportPath == "") {
		// System.out
		// .println("****************ERROR:the excelExportPath should have a value!!**************");
		// }

//		String downloadPath = "D:/workspaceutf8/2013.10.14 for xiafeng/gaea.env.war/target/gaea.env.war-0.0.1-SNAPSHOT/machine/excel"+ File.separator + excelFileName;
		String downloadPath = "/home/admin/web-deploy/jetty_server/webapps/newenv/machine/excel"+ File.separator + excelFileName;
		System.out.println("save: " + downloadPath);
		try {
			FileOutputStream fout = new FileOutputStream(downloadPath);
			machineWorkBook.write(fout);
			fout.close();
			return excelFileName;
		} catch (Exception e) {
			logger.error("export xls file error:" + e.getMessage());
			return "";
		}

	}

4、其实配置apache也可以,把文件丢到apache目录,但是。。。这个我也不会啊 ,下次再学闭嘴

5、同事说我的做法太非主流,可以写二进制流,参考    

http://www.blogjava.net/sxyx2008/archive/2010/12/10/340279.html




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值