Struts1 POI导出Excel

1.Apache POI简介

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 POI 的功能。

2.POI结构

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

3.POI实现

/**
 * 导出辅助类
 * @author Administrator
 *
 */
public class exportExcle {
	/**
	 * 导出Excel
	 * @param dataList 要导出的list
	 * @param response 
	 * @param mapTitle excel标题头
	 * @param strEntityName  对应的实体
	 * @param strSheetName sheet名称
	 * @param strFileName excel名字
	 * @param assistMap 辅助传值
	 * @throws Exception
	 */
	@SuppressWarnings({ "unused", "rawtypes" })
	public static void exportExcleMethod(List dataList, HttpServletResponse response,Map mapTitle,String strEntityName,String strSheetName,String strFileName ,Map assistMap) throws Exception {
		OutputStream ou = response.getOutputStream();
		Workbook wb = new HSSFWorkbook();
		OutputStream out = new FileOutputStream(strFileName);
		Sheet sheet = wb.createSheet(strSheetName);
		Row row = sheet.createRow(0);
		int mapColumn=mapTitle.size();
		for (int i = 0; i < mapColumn; i++) {
			Cell cell = row.createCell(i);
			cell.setCellValue((String)mapTitle.get(i));
		}		
		if (dataList != null && dataList.size() > 0) {

			for (int i = 0; i < dataList.size(); i++) {
				Row ro = ((org.apache.poi.ss.usermodel.Sheet) sheet).createRow(i + 1);				
				if("train_bm_info".equals(strEntityName)){
					train_bm_info re = (train_bm_info) dataList.get(i);
					trainBmInfo(re,ro,i,assistMap);
				}				
			}
		}
		response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(strFileName, "UTF-8"));
		response.setContentType("application/msexcel;charset=UTF-8");

		wb.write(ou);
		ou.close();

	}
	/**
	 * 培训报名设置每一列
	 * @param p 实体
	 * @param ro 行
	 * @param i i
	 * @param assistMap 辅助map值
	 */
	@SuppressWarnings("rawtypes")
	public static void trainBmInfo(train_bm_info p ,Row ro,int i,Map assistMap){
		 if (p != null) {		
		  ro.createCell(0).setCellValue(1 + i);
		  ro.createCell(1).setCellValue(p.getStu_name());
		  ro.createCell(2).setCellValue(p.getSex());
		  ro.createCell(3).setCellValue(p.getCertnum());
		  ro.createCell(4).setCellValue(p.getPhone());
		  ro.createCell(5).setCellValue(p.getTel());
		 }
	}
	/**
	 * 设置导出的map第一行name
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  Map trainBmInfoTitle(){
		Map mapTitle= new HashMap();
		mapTitle.put(0, "序号");
		mapTitle.put(1, "姓名");
		mapTitle.put(2, "性别");
		mapTitle.put(3, "身份证件号");
		mapTitle.put(4, "固定电话");
		mapTitle.put(5, "移动电话");
		return mapTitle;
	}
	
	
}
Struts1要是导出的话直接将方法声明为void就可以,这样既能导出,有没有返回值,这样非常方便,这是我写的一个公用的方法,我感觉写的还可以,假如大家有更好的方法,希望我们多多交流。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.*; import org.apache.struts.action.*; import org.apache.struts.upload.FormFile; import org.apache.commons.beanutils.BeanUtils; public class Action { /**//* * 把数据库中的字段导入到Excel ,并生成Excel文档 **/ public ActionForward getDownload(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Form fm = (Form) actionForm; // Excel 文件存放在服务器的相对路径下 String outputFile = request.getRealPath("/tmp/Excel.xls"); try { // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在Excel 工作簿中建一工作表 HSSFSheet sheet = workbook.createSheet("Sheet1"); // 设置单元格格式(文本) HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@")); // 在索引0的位置创建行(第一行) HSSFRow row = sheet.createRow((short) 0); HSSFCell cell1 = row.createCell((short) 0);// 第一列 HSSFCell cell2 = row.createCell((short) 1); HSSFCell cell3 = row.createCell((short) 2); // 定义单元格为字符串类型 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); cell2.setCellType(HSSFCell.CELL_TYPE_STRING); cell3.setCellType(HSSFCell.CELL_TYPE_STRING); cell1.setEncoding(HSSFCell.ENCODING_UTF_16); cell2.setEncoding(HSSFCell.ENCODING_UTF_16); cell3.setEncoding(HSSFCell.ENCODING_UTF_16); // 在单元格中输入数据 cell1.setCellValue("姓名"); cell2.setCellValue("性别"); cell3.setCellValue("年龄"); Connection connection = session.connection(); String sql = "Select t.name, t.sex, t.age from table t where t.sex = ?"; try { PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, fm.getSex());// 传入查询条件 ResultSet rs = ps.executeQuery();// 查询结果存入rs connection.commit();// 执行SQL while (rs.next()) { //设置j行从第二行开始 int j = 1; row = sheet.createRow((short) j); //设置i列从第二列开始 for (int i = 1; i <= 3; i++) { HSSFCell cell = row.createCell((short) (i-1)); // 设置单元格格式 cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(rs.getString(i)); } j++; } request.setAttribute("message", "文件生成成功!"); } catch (SQLException e) { request.setAttribute("message", "创建文件失败!"); e.printStackTrace(); } // 删除路径下同名的Excel 文件 File path = new File(outputFile); path.delete(); // 新建一输出文件流 FileOutputStream fOut = new FileOutputStream(outputFile); // 把相应的Excel 工作簿存盘 workbook.write(fOut); // 操作结束,关闭文件 fOut.flush(); fOut.close(); //该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空) request.getSession().setAttribute("Download", outputFile); } catch (Exception ioexception) { request.setAttribute("message", "创建文件失败!"); return actionMapping.findForward("outJSP"); } return actionMapping.findForward("outJSP"); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值