`
shelllgd
  • 浏览: 30054 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java导出Excel表

阅读更多
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExportDict {

  /**
     * 根据类型获取导入其他属性
     * 
     * @param impflag 导入类型
     * @return
     * @throws DataAccessException
     */
	private static BaseParam getExportPros(String exptype)
            throws DataAccessException {

        try {
            Iterator params = null;
            ParamClass paramClass = BaseParamClass
                    .getParamClassByName("DICT_EXPORTDICT");
            
            params = paramClass.getRootParams();
            
            String exptype1 = null;
            while (params.hasNext()) {
                BaseParam baseParam = (BaseParam) params.next();
                exptype1 = (String) baseParam.getCode();
                if (exptype.equals(exptype1)) {
                	return baseParam;
                }
            }
            return null;
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
	/**
	 * <p>
	 * 设定导出属性
	 * 
	 * @param exptype
	 *            导出类别,确定要导出哪个表
	 * @return ExportPO
	 */
	private static ExportPO getExportPor(String exptype)
			throws DataAccessException {
		ExportPO exportPO = new ExportPO();
		 BaseParam baseParam =  getExportPros(exptype); 
		 try {
				exportPO.setTitles(((String) baseParam.getAttribute("TITLES")).split(","));
				exportPO.setTitleCols(((String) baseParam.getAttribute("TITLECOLS")).split(","));
				exportPO.setFileName((String) baseParam.getAttribute("FILENAME"));
				exportPO.setSheetName((String) baseParam.getAttribute("SHEETNAME"));

			} catch (ParamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return exportPO;
	}

	/**
	 * <p>
	 * 导出execl
	 * 
	 * @param exptype
	 *            导出类别,确定要导出哪个表
	 * @param
	 * @return ExportPO
	 */
	public static void ExportExecl(com.cvicse.infra.base.BaseVO[] expPOs,
			String exptype, HttpServletResponse response)
			throws DataAccessException {
		// 获取导出属性
		System.out.println(exptype);
		ExportPO exportPO = getExportPor(exptype);
		// 文件名
		String fileName = exportPO.getFileName();
		// 表单名
		String sheetName = exportPO.getSheetName();
		try {
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=\""
					+ com.cvicse.util.StringUtil
							.toUTF8String(fileName + ".xls") + "\"");

			 OutputStream outputStream = response.getOutputStream();
			 WritableWorkbook wwb = Workbook.createWorkbook(outputStream);

			 WritableSheet wrb = wwb.createSheet(sheetName, 0);
			Label labelC = null;
			// 设置字体
			WritableFont wf_data = new WritableFont(WritableFont.ARIAL, 10);
			// 设置单元格格式
			WritableCellFormat wcf_data = new WritableCellFormat(wf_data);
			wcf_data.setBorder(Border.ALL, BorderLineStyle.THIN);
			// 自动换行
			wcf_data.setWrap(true);
			// 垂直居中
			wcf_data.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			int titlecol = 0;
			for (int titleCount = 0; titleCount < (exportPO.getTitles()).length; titleCount++) {

				int row = 0;

				labelC = new Label(titlecol++, row,
						exportPO.getTitles()[titleCount], wcf_data);
				wrb.addCell(labelC);
			}
			for (int i = 0; i < expPOs.length; i++) {
				int row = i + 1;
				int col = 0;
				com.cvicse.infra.base.BaseVO baseVO = expPOs[i];
				Method[] methods = baseVO.getClass().getMethods();
				for (int titleColsCount = 0; titleColsCount < (exportPO
						.getTitleCols()).length; titleColsCount++) {
					String titleCol = exportPO.getTitleCols()[titleColsCount];
					for (int methodsCount = 0; methodsCount < methods.length; methodsCount++) {
						Method method = methods[methodsCount];
						String methodName = method.getName();
						if (methodName.startsWith("get")							
								&& !methodName.startsWith("getClass")) {
							String fieldName = methodName.substring(3);
							if (fieldName.equals(titleCol)) {
								labelC = new Label(col++, row, method.invoke(
										baseVO, null)
										+ "", wcf_data);
								wrb.addCell(labelC);
							}
						}
					}

				}
			}
			wwb.write();
			wwb.close();
			outputStream.close();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics