标签:
struts.xml配置
<!-- 下载 -->
<action name="download" class="nam.menu.downloadAC">
<result name="input" type="stream">
<param name="contentType">application/vnd.ms-excel</param><!-- 指明文件的类型 -->
<param name="inputName">fis</param><!-- 指明文件下载的方法的名字 -->
<!--attachment属性是要选择打开时单独打开,而不是在浏览器中打开,默认是inline. 指明下载时文件的名字 -->
<param name="contentDisposition">attachment;filename="${exportFileName}.xls"</param>
<param name="bufferSize">4096</param>
</result>
</action>
java Action 层代码
import java.io.InputStream; import java.io.UnsupportedEncodingException; public class DownloadAC extends BaseMenuDataAC{ private String ids; private InputStream fis = null; private String exportFileName = null; public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public InputStream getFis() { return fis; } public void setFis(InputStream fis) { this.fis = fis; } public String getExportFileName() { return exportFileName; } public void setExportFileName(String exportFileName) { this.exportFileName = exportFileName; } public String execute() { String name = "表名称"; try { setExportFileName(new String(name.getBytes("GB2312"), "ISO8859-1")); } catch (UnsupportedEncodingException e) { // TODO: handle exception e.printStackTrace(); } fis = dowloadBS.exportcostinfolist(ids); return INPUT; } }
BS层
public InputStream exportcostinfolist(String ids) { // 创建excel导出对象 POIExcelObjectAdvance excel = new POIExcelObjectAdvance(); POIExcelSheet sheet1 = excel.createSheet("表"); // 初始化导出区域列表 List<POIExcelContent> areaList = new ArrayList<POIExcelContent>(); // 设置字体 HSSFFont font = excel.createDefaultFont(); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 10); HSSFFont font1=excel.createDefaultFont(); font1.setFontName("黑体"); font1.setFontHeightInPoints((short) 13); int[] cols = { 10, 10, 20, 30}; sheet1.setColumnWidth(cols); // 初始化导出区域1的内容列表 List<String[]> title1List = new ArrayList<String[]>(); // 在导出区域1内容列表增加导出内容 String[] titles = { "序号", "mingchengf", "nianling","jiankang}; title1List.add(titles); POIExcelContent title1 = new POIExcelContent(0, 0, title1List, font1); areaList.add(title1); List<Date> data = downloadDataDAO.queryExportMenuDataList(ids); List<String[]> title2List = new ArrayList<String[]>(); for (int i = 0; i < data.size(); i++) { String[] content = new String[titles.length]; content[0] = String.valueOf(i + 1); content[1] = data.get(i).getName(); content[2] = data.get(i).getAge(); content[3] = data.get(i).getJakang(); title2List.add(content); } POIExcelContent title2 = new POIExcelContent(0, 1, title2List, font); areaList.add(title2); sheet1.setContentList(areaList); return excel.toInputStream(); }
标签:
原文地址:http://www.cnblogs.com/songwy/p/4569795.html