页面:
<form action="${path}/xxx/xxx.do"  method="get" >
        	表格下载:<input type="submit" value="下载">
    	</form>

  

@RequestMapping("/download")
	public void download(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws Exception {
		ArrayList<String> listTitle = new ArrayList<String>();
		listTitle.add("a");
		listTitle.add("b");
		listTitle.add("c");
		listTitle.add("d");
		listTitle.add("e");
		// 创建表格及其第一行标题栏
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("sheet1");
		HSSFRow row0 = sheet.createRow(0);
		for (int j = 0; j < 5; j++) {// 5列
			HSSFCell cell = row0.createCell((short) j);
			cell.setCellValue(listTitle.get(j).toString());
		}
		String maker = SystemUtil.getCureenUser(session);
		List<MainfestItem> list = transportService.getlist(maker);
		System.out.println(list.size());
		// 从第二行输入
		for (int i = 1; i <= list.size(); i++) { // 几行
			HSSFRow row = sheet.createRow(i);
			HSSFCell cell0 = row.createCell((short) 0);// 第n+1个
			cell0.setCellValue(list.get(i - 1).getId().toString());
			HSSFCell cell1 = row.createCell((short) 1);
			cell1.setCellValue(list.get(i - 1).getContainerid().toString());
			HSSFCell cell2 = row.createCell((short) 2);
			cell2.setCellValue(list.get(i - 1).getIsocode().toString());
		}
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		wb.write(os);
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数,可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String(("box" + ".xls").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}

  

java写简单Excel 首行是目录 然后前台下载的更多相关文章

  1. java生成简单Excel工作薄

    前言: 代码都是建立在实际需求上的,上周做完一个调外部电影券接口的项目,这周产品又要excel表格,大致内容为:券所属影院.图片URL.等信息制作为excel表格,把每次同步过来的数据给他分析. jx ...

  2. 用JAVA写简单的栈

    package com.gailekesi.example.expl_tuple; import javax.naming.NameNotFoundException; import java.awt ...

  3. Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

    1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...

  4. java 写 Excel(不生成实体文件,写为流的形式)

    java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...

  5. Java写Excel(不生成实体文件,写为流的形式)

    java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...

  6. Java代码工具箱之解析单行单列简单Excel

    1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...

  7. java导入导出excel常用操作小结及简单示例

    POI中常用设置EXCEL的操作小结: 操作excel如下 HSSFWorkbook wb = new HSSFWorkbook();  //创建一个webbook,对应一个Excel文件 HSSFS ...

  8. 在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间

    在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间

  9. Microsoft Excel 标题栏或首行锁定

    Microsoft Excel 标题栏或首行锁定 在进行Excel编辑的时候,希望在浏览的时候,第一行或者第一列能够始终显示. 需要做的是:在Excel中选择 "视图"->& ...

随机推荐

  1. android webview setcookie 设置cookie

    CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager ...

  2. Mybatis学习(2)原始dao开发和使用mapper接口代理开发

    基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...

  3. COMMON INTERVIEW QUESTIONS

    1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...

  4. 使用xmlHttprequest有感

    原文地址:http://my.oschina.net/LinBandit/blog/33160 之前一片日志说使用xmlhttprequest获取服务数据时,在IE下能通过而在chrome不能通过的问 ...

  5. Jmeter接口压测

    对于各个组件的使用,建议参考官方文档 1. Jmeter参数化,从txt文件读取. 1.txt

  6. Spring bean注解配置(1)

    Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...

  7. mvc和mtv

    Java中MVC详解以及优缺点总结 概念: MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  8. Samba 简介

    SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...

  9. linux 下各个4K区块文件大小测试速度对比 机械硬盘性能 64K性价比收益最高

    机械硬盘,每个区块取三次数最小值为准,带2G RAM缓存卡 4K3.4 MB/秒 8K7.3 MB/秒 16K9.5 MB/秒 32K16.7 MB/秒 64K44.2 MB/秒 128K67.1 M ...

  10. Spring AOP的总结