【SSH学习】导出Excel表格JavaWeb代码实现

本文介绍了一个具体的Java程序示例,该程序通过Struts2框架实现从数据库读取子区域数据,并使用Apache POI库将其导出为Excel文件。文章详细展示了如何创建Excel工作簿、设置单元格内容及格式,以及如何处理文件名编码以适应不同浏览器的下载需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、后台代码

package com.aitiman.bos.web.action;

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

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aitiman.bos.domain.Region;
import com.aitiman.bos.domain.Subarea;
import com.aitiman.bos.service.ISubareaService;
import com.aitiman.bos.utils.FileUtils;
import com.aitiman.bos.web.action.base.BaseAction;

@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea> {
	
	@Autowired
	private ISubareaService subareaService;
	
	public String exportXLS() throws IOException {
		//第一步查询数据
		List<Subarea> list = subareaService.findAll();
		
		//第二步使用poi将数据写到excel中
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet sheet = hssfWorkbook.createSheet("分区数据");
		HSSFRow row1 = sheet.createRow(0);
		row1.createCell(0).setCellValue("分区编号");
		row1.createCell(1).setCellValue("开始编号");
		row1.createCell(2).setCellValue("结束编号");
		row1.createCell(3).setCellValue("位置信息");
		row1.createCell(4).setCellValue("省市区");
		for(Subarea subarea : list) {
			HSSFRow row = sheet.createRow(sheet.getLastRowNum()+1);
			row.createCell(0).setCellValue(subarea.getId());
			row.createCell(1).setCellValue(subarea.getStartnum());
			row.createCell(2).setCellValue(subarea.getEndnum());
			row.createCell(3).setCellValue(subarea.getPosition());
			row.createCell(4).setCellValue(subarea.getRegion().getName());
		}
		
		//第三步 使用输出流进行文件下载
		String filename = "分区数据.xls";
		//获取文件类型
		String mimeType = ServletActionContext.getServletContext().getMimeType(filename);
		ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
		//设置文件类型
		ServletActionContext.getResponse().setContentType(mimeType);
		//设置客户端浏览器类型
		String agent = ServletActionContext.getRequest().getHeader("User-Agent");
		//使用工具类编码文件名(解决无法传递中文文件名的问题)
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);
		hssfWorkbook.write(os);		
		return null;
	}


}

二、中文编码工具类

package com.aitiman.bos.utils;

import java.io.IOException;
import java.net.URLEncoder;

import sun.misc.BASE64Encoder;

public class FileUtils {
		/**
		 * 下载文件时,针对不同浏览器,进行附件名的编码
		 * 
		 * @param filename
		 *            下载文件名
		 * @param agent
		 *            客户端浏览器
		 * @return 编码后的下载附件名
		 * @throws IOException
		 */
		public static String encodeDownloadFilename(String filename, String agent)
				throws IOException {
			if (agent.contains("Firefox")) { // 火狐浏览器
				filename = "=?UTF-8?B?"
						+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
						+ "?=";
				filename = filename.replaceAll("\r\n", "");
			} else { // IE及其他浏览器
				filename = URLEncoder.encode(filename, "utf-8");
				filename = filename.replace("+"," ");
			}
			return filename;
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值