action中的方法

  /**
* Excel文件下载处理
* @return
*/
@RequestMapping("/downloanExcel")
public ModelAndView downloanExcel(){
List<AuContract> list = new ArrayList<AuContract>(); list= service.findAuContractList();//获得数据库所有的合同集合
Map<String,List<AuContract>> map = new HashMap<String, List<AuContract>>();
map.put("infoList", list);
ExcelView ve = new ExcelView();
return new ModelAndView(ve,map);
} excel操作工具类 ExcelView package com.ekyb.common.auContract.action; import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractExcelView; import com.ekyb.common.auContract.entity.AuContract;
import com.ekyb.common.util.CharEncodingEdit; /**
* 下载Excel视图
*
* @author gx
*/
public class ExcelView extends AbstractExcelView { @SuppressWarnings("static-access")
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception { @SuppressWarnings("unchecked")
List<AuContract> list = (List<AuContract>) model.get("infoList"); HSSFFont font= workbook.createFont();
font.setFontHeightInPoints((short)12); //设置字体的大小
font.setFontName("微软雅黑"); //设置字体的样式,如:宋体、微软雅黑等
font.setItalic(false); //斜体true为斜体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //对文中进行加粗
font.setColor(HSSFColor.PINK.index); //设置字体的颜色
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font); if (list != null && list.size() != 0) {
int length = list.size();
Sheet sheet = workbook.createSheet(); // 第一行文字说明
Row row = sheet.createRow(0);
Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("合同名称"); cell = row.createCell(1, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("合同单位"); cell = row.createCell(2, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("合同登记时间"); cell = row.createCell(3, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("合同金额"); cell = row.createCell(4, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("履行方式"); cell = row.createCell(5, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("合同类型"); cell = row.createCell(6, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("开始时间"); cell = row.createCell(7, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("结束时间"); cell = row.createCell(8, Cell.CELL_TYPE_STRING);
cell.setCellStyle(style);
cell.setCellValue("备注"); // 下面是具体内容
for (int i = 0; i < length; i++) {
sheet.setColumnWidth((short) i, (short) (35.7 * 100));
row = sheet.createRow(i + 1);
// 合同名称
cell = row.createCell(0, cell.CELL_TYPE_STRING);
cell.setCellValue(list.get(i).getName());
// 合同单位
cell = row.createCell(1, cell.CELL_TYPE_STRING);
cell.setCellValue(list.get(i).getUnit()); // 合同登记时间
cell = row.createCell(2, cell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getReg_time())); // 合同金额
cell = row.createCell(3, cell.CELL_TYPE_STRING);
//把float对象转换为String对象
float con_money=list.get(i).getCon_money();
String str =String.valueOf(con_money);
cell.setCellValue(str); // 合同履行方式
cell = row.createCell(4, cell.CELL_TYPE_STRING);
cell.setCellValue(list.get(i).getPerform_style()); // 合同类型
cell = row.createCell(5, cell.CELL_TYPE_STRING);
cell.setCellValue(list.get(i).getCon_type()); // 开始时间
cell = row.createCell(6, cell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getCon_start_time())); // 结束时间
cell = row.createCell(7, cell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getCon_end_time())); // 备注
cell = row.createCell(8, cell.CELL_TYPE_STRING);
cell.setCellValue(list.get(i).getRemark());
} //web浏览通过MIME类型判断文件是excel类型
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8"); // 对文件名进行处理。防止文件名乱码
String fileName = CharEncodingEdit.processFileName(request, "合同.xls");
// Content-disposition属性设置成以附件方式进行下载
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
}
}
} jsp页面代码用的是jQueryUI <a href="<%=basePath%>/auContract/downloanExcel" class="easyui-linkbutton"data-options="plain:'true',iconCls:'icon-down'">下载Excel</a> //可以直接通过a标签的href直接连接到action中的url映射地址

Java浏览器下载文件为excel(springMVC方式)的更多相关文章

  1. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  2. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  3. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

  4. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  5. Microsoft Edge浏览器下载文件乱码修复方法(二)

    之前有写过"Microsoft Edge浏览器下载文件乱码修复方法",发现很多情况下下载文件乱码问题还是存在,这里对之前内容做简单补充,希望可以帮到大家. 方法二: 默认如果提示下 ...

  6. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  7. 火狐浏览器下载文件中文乱码,文件名中的空格变加号("+")的问题

    解决一下问题: 1.火狐浏览器下载文件,中文变乱码 2.IE浏览器下载文件,丢失文件扩展名或强制扩展名为".txt" 3.浏览器下载文件,文件名中的空格变成加号("+&q ...

  8. 在Linux终端使用W3M浏览器下载文件

    在Linux终端使用W3M浏览器下载文件 W3M 是3个基于Linux系统命令行的WEB网站浏览工具(w3m/Links/Lynx) 对于需要验证cookie 和来源的页面,比如163的超大附件,直接 ...

  9. IE浏览器下载文件保存时提示:“你没有权限在此位置中保存文件”解决办法

    E浏览器下载文件保存时提示 解决办法: 1.Win + R,打开运行命令,输入gpedit.msc,如图所示 2.打开计算机本地组策略编辑器:选择计算机配置-windows设置-安全设置-本地策略-安 ...

随机推荐

  1. Codeforces 762D Maximum path 动态规划

    Codeforces 762D 题目大意: 给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍.经过一个格子会获得格子中的权值.每个格子的权值\(a ...

  2. javacpp-FFmpeg系列补充:FFmpeg解决avformat_find_stream_info检索时间过长问题

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  3. poj1734Sightseeing trip——无向图求最小环

    题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...

  4. C#如何立即回收内存

    1.把对象赋值为null 2.立即调用GC.Collect(); 注意:这个也只是强制垃圾回收器去回收,但具体什么时候执行不确定.  代码: class Test { ~Test() { Consol ...

  5. C# 架构模式

    单例模式 (Singleton) 单例讲的是当一个类被初次调用时,会产生一个类的实例, 而这个类的实例会贯穿程序的整个生命周期.单例提供了一个全局.唯一的实例. 步骤:1.让类自己创建一个实例:2.提 ...

  6. CF-828B

    B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. GridSplitter用法

    1.GridSplitter的ShowsPreview设置为True时拖动报null错误 解决方法在Grid外面包装一个装饰器:AdornerDecorator,至于为什么这么做,暂时还不知道 2.当 ...

  8. 转-tcp建立和释放详解

    建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. [更新于2017.01.04 ]该部分内容配图有误,请大家见谅,正确的配图如下,错误配图也不删 ...

  9. There&nbsp;is&nbsp;no&nbsp;resul…

    There is no result type defined for type 'json' mapped with name 'success'. 这个错误是json初学者很容易遇到的错误:现在把 ...

  10. windows64位如何安装pyspider并运行

    1.下载whl文件: http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 2.安装该文件 3.可能碰到问题,pip的版本低了,需要更新一下pip的版本.更新 ...