All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wicp.tams.commons.report.excel.ReportAbstract Maven / Gradle / Ivy

There is a newer version: 2.3.4
Show newest version
package net.wicp.tams.commons.report.excel;

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.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.wicp.tams.commons.Conf;
import net.wicp.tams.commons.apiext.IOUtil;
import net.wicp.tams.commons.apiext.StringUtil;
import net.wicp.tams.commons.constant.PathType;
import net.wicp.tams.commons.web.J2EEAssist;

public abstract class ReportAbstract {
	private Logger logger = LoggerFactory.getLogger(ReportAbstract.class);
	protected final static String tempdir = PathType.getPath(Conf.get("jxls.dir.temp"));
	protected final static String exportDefault = PathType.getPath(Conf.get("jxls.dir.export"));

	protected final String tempName;
	protected List headers;// excel的标题

	public void exportExcel(OutputStream os) {
		InputStream is = null;
		try {
			is = StringUtil.isNull(tempName) ? null
					: new FileInputStream(IOUtil.mergeFolderAndFilePath(tempdir, tempName));
		} catch (FileNotFoundException e) {
			throw new RuntimeException("模板文件不存在");
		}
		export(is, os);
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				logger.info("关闭输入流出错");
			}
		}
	}

	/***
	 * 导出指定文件名,且以response方式返回给客户端
	 * 
	 * @param response
	 *            要返回信息
	 * @param fileName
	 *            要导出的文件名
	 */
	public void exportExcel(HttpServletResponse response, String fileName) {
		exportExcel(J2EEAssist.getExcelOS(response, fileName, true));
	}

	/***
	 * 导出指定文件名的excel
	 * 
	 * @param fileName
	 *            指定的文件名
	 * @return
	 */
	public String exportExcel(String fileName) {
		OutputStream os;
		try {
			File outdir = new File(exportDefault);
			if (!outdir.exists()) {
				outdir.mkdir();
			}
			String outpath = IOUtil.mergeFolderAndFilePath(exportDefault, fileName);
			os = new FileOutputStream(outpath);
			exportExcel(os);
			return outpath;
		} catch (FileNotFoundException e) {
			logger.error("打开输出流出错", e);
			throw new RuntimeException("导出文件出错");
		}
	}

	public abstract void export(InputStream is, OutputStream os);

	////////////////////////////////////////////////////// getset方法///////////////////////////////////////////////////////////////
	/****
	 * 没有模板文件直接导出文件
	 */
	public ReportAbstract() {
		this.tempName = null;
	}

	public ReportAbstract(String tempName) {
		this.tempName = tempName;
	}

	public List getHeaders() {
		return headers;
	}

	public void setHeaders(List headers) {
		this.headers = headers;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy