poi导出实例

action配置
<action name="exportTfUserList"	class="com.test.action.TfUserQueryAction" method="exportTfUserList">
	<!-- 指定下载文件的文件名 -->
	<param name="fileName">T_F_USER_LIST.xls</param>
	<result name="success" type="stream">
		<!-- 指定下载文件的内容类型,text/plain是默认类型 -->
		<param name="contentType">application/vnd.ms-excel</param>
		<!--动态获取文件名,从Action中的取得execl名,避免中文乱码-->
		<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
		<param name="bufferSize">1024</param>
		<param name="inputName">excelFileIns</param>
	</result>
</action>

action代码:

private TfUserService tfUserService;

private File excelFile;
private InputStream excelFileIns;
private String excelFileFileName;

public File getExcelFile() {
	return excelFile;
}

public void setExcelFile(File excelFile) {
	this.excelFile = excelFile;
}

public InputStream getExcelFileIns() {
	return excelFileIns;
}

public void setExcelFileIns(InputStream excelFileIns) {
	this.excelFileIns = excelFileIns;
}

public String getExcelFileFileName() {
	return excelFileFileName;
}

public void setExcelFileFileName(String excelFileFileName) {
	this.excelFileFileName = excelFileFileName;
}
/**
* 将用户列表以excel的形式导出
 * @return
*/
public String exportTfUserList(){
	List<SysUser> expList;
	try{
		expList=tfUserService.exportUserList(this.getCondition());
		HSSFWorkbook workbook = this.tfUserService.exportExcel(expList);
		//getAppSessionAttributes().remove("exportList");
		//getAppSessionAttributes().clear();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		workbook.write(output);
		byte[] ba = output.toByteArray();
		excelFileIns = new ByteArrayInputStream(ba);
		output.flush();
		output.close();
	}catch(Exception e){
		ActionContext.getContext().put("errorMsg","导出失败,请联系管理员!");
		return ERROR;
	}
	return SUCCESS;
}

/** 提供转换编码后的供下载用的文件名 模板下载*******************************/
private String fileName;// 初始的通过param指定的文件名属性    
private String inputPath;// 指定要被下载的文件路径
public String getDownloadFileName() {    
	String downFileName = fileName;    
	try {    
            downFileName = new String(downFileName.getBytes(), "ISO8859-1");    
       }catch(UnsupportedEncodingException e) {    
           e.printStackTrace();    
       }    
       return downFileName;    
  
    }

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}


service代码:

public HSSFWorkbook exportExcel(List list) throws Exception {
	List<SysOrg> orgList = orgDao.getAllOrg();
	Map<String, String> orgMap = new HashMap<String, String>();
	for(SysOrg org : orgList){
		orgMap.put(org.getOrgNo(), org.getOrgName());
	}
		
	HSSFWorkbook workbook = null;
	int i=0;
	try {
		// 创建工作簿实例
		workbook = new HSSFWorkbook();
		// 创建工作表实例
		HSSFSheet sheet = workbook.createSheet("TscExcel");
		// 设置列宽
		this.setSheetColumnWidth(sheet);
		// 获取样式
		HSSFCellStyle style = this.createTitleStyle(workbook);
		if (list != null && list.size() > 0) {
			HSSFRow row = sheet.createRow((short) 0);// 建立新行
			this.createCell(row, 0,style, HSSFCell.CELL_TYPE_STRING, "系统帐号");
			this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING,"用户姓名");
			this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING,"用户状态");
			this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING,"所属机构");
			Iterator it = list.iterator();  
			while(it.hasNext()) {  
				SysUser userInfo = (SysUser) it.next();
				HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
				if (userInfo.getUser_no() != null)
					this.createCell(row1, 0, style,HSSFCell.CELL_TYPE_STRING, userInfo.getUser_no());
				if (userInfo.getUser_name() != null)
					this.createCell(row1, 1, style,HSSFCell.CELL_TYPE_STRING, userInfo.getUser_name());
				if (userInfo.getUser_status() != null){
					if("1".equals(userInfo.getUser_status())){
						this.createCell(row1, 2, style,HSSFCell.CELL_TYPE_STRING, "激活");
					}else if("0".equals(userInfo.getUser_status())){
						this.createCell(row1, 2, style,HSSFCell.CELL_TYPE_STRING, "锁定");
					}
				}
				if (userInfo.getOrg_no() != null && !"".equals(userInfo.getOrg_no()) && !"undefined".equals(userInfo.getOrg_no()))
					this.createCell(row1, 3, style,HSSFCell.CELL_TYPE_STRING,orgMap.get(userInfo.getOrg_no()).toString());
					i++;
				}
			} else {
				this.createCell(sheet.createRow(0), 0, style,HSSFCell.CELL_TYPE_STRING, "查无资料");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return workbook;
	}
	private void setSheetColumnWidth(HSSFSheet sheet) {
		// 根据你数据里面的记录有多少列,就设置多少列
		sheet.setColumnWidth(0, 5000);
		sheet.setColumnWidth(1, 7000);
		sheet.setColumnWidth(2, 5000);
		sheet.setColumnWidth(3, 7000);
	}
	// 设置excel的title样式
	private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
		HSSFFont boldFont = wb.createFont();
		boldFont.setFontHeight((short) 200);
		HSSFCellStyle style = wb.createCellStyle();
		style.setFont(boldFont);
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
		return style;
	}
	// 创建Excel单元格
	private void createCell(HSSFRow row, int column, HSSFCellStyle style,int cellType, Object value) {
		HSSFCell cell = row.createCell(column);
		if (style != null) {
			cell.setCellStyle(style);
		}
		switch (cellType) {
		case HSSFCell.CELL_TYPE_BLANK: {
		}
		break;
		case HSSFCell.CELL_TYPE_STRING: {
			cell.setCellValue(value.toString());
		}
		break;
		case HSSFCell.CELL_TYPE_NUMERIC: {
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			cell.setCellValue(Double.parseDouble(value.toString()));
		}
		break;
		default:
			break;
		}
	}


dao层:

/* 
* 获取所有机构
*/
public List getAllOrg() {
	return getHibernateTemplate().find("from SysOrg");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值