Java导出Excel的Springmvc实例
程序员文章站
2022-09-27 18:57:50
@RequestMapping(value = "downloadExcel", method = RequestMethod.GET) public String download(HttpServletRequest request,HttpServletResponse response) t ......
@requestmapping(value = "downloadexcel", method = requestmethod.get)
public string download(httpservletrequest request,httpservletresponse response) throws ioexception{
string filename="excel文件";
//填充projects数据
excelutil excelutil = new excelutil();
list<project> projects = excelutil.createdata();
list<map<string,object>> list = excelutil.createexcelrecord(projects);
string columnnames[]={"id","项目名","销售人","负责人","所用技术","备注"};//列名
string keys[] = {"id","name","saler","principal","technology","remarks"};//map中的key
bytearrayoutputstream os = new bytearrayoutputstream();
try {
excelutil.createworkbook(list,keys,columnnames).write(os);
} catch (ioexception e) {
e.printstacktrace();
}
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((filename + ".xls").getbytes(), "utf-8"));
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;
while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesread);
}
bos.flush();
} catch (final ioexception e) {
logger.error("导出excel异常:",e);
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return null;
}
/**
* 导出excel
* @param request
* @param response
*/
@requestmapping(value = "processrequestexcel", method = requestmethod.get)
public void processrequest(httpservletrequest request,httpservletresponse response){
java.text.simpledateformat format = new java.text.simpledateformat("yyyymmddhhmmss");
string now = format.format(new date());
string exportfilename = "信息导出_"+now+".xls";//导出文件名
list<basicinfo> list = getinfolist();
hssfworkbook workbook = null;
string[] celltitle = {"序号", "姓名", "性别", "部门"};
try {
workbook = new hssfworkbook();//创建工作薄
hssfsheet sheet = workbook.createsheet();
workbook.setsheetname(0, "订单信息");//工作簿名称
hssffont font = workbook.createfont();
font.setcolor(hssffont.color_normal);
font.setboldweight(hssffont.boldweight_bold);
hssfcellstyle cellstyle = workbook.createcellstyle();//创建格式
cellstyle.setfont(font);
cellstyle.setalignment(hssfcellstyle.align_center);
cellstyle.setverticalalignment(hssfcellstyle.vertical_center);
//创建第一行标题
hssfrow titlerow = sheet.createrow((short) 0);//第一行标题
for(int i = 0,size = celltitle.length; i < size; i++){//创建第1行标题单元格
sheet.setcolumnwidth(i,4000);
hssfcell cell = titlerow.createcell(i,0);
cell.setcellstyle(cellstyle);
cell.setcellvalue(celltitle[i]);
}
//从第二行开始写入数据
for(int i=1,size = list.size();i<size;i++){
hssfrow row = sheet.createrow((short) i);
basicinfo entity = list.get(i);
for (int j = 0,length=celltitle.length; j < length; j++) {
hssfcell cell = row.createcell(j, 0);// 在上面行索引0的位置创建单元格
cell.setcelltype(hssfcell.cell_type_string);// 定义单元格为字符串类型
switch(j){// 在单元格中输入一些内容
case 0:
cell.setcellvalue(string.valueof(i));
break;
case 1:
cell.setcellvalue(entity.getname());
break;
case 2:
cell.setcellvalue(entity.getsex());
break;
case 3:
cell.setcellvalue(entity.getdepart());
break;
}
}
}
// 表示以附件的形式把文件发送到客户端
response.setheader("content-disposition", "attachment;filename=" + new string((exportfilename).getbytes(), "iso8859-1"));//设定输出文件头
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");// 定义输出类型
// 通过response的输出流把工作薄的流发送浏览器形成文件
outputstream outstream = response.getoutputstream();
workbook.write(outstream);
outstream.flush();
outstream.close();
}catch(ioexception e){
system.out.println("io 异常!"+e.getmessage());
e.printstacktrace();
}finally{
}
}
/**
* 模拟数据库获取信息
* @return
*/
@suppresswarnings("unchecked")
public list<basicinfo> getinfolist(){
list<basicinfo> list = new arraylist();
for(int i=1;i<101;i++){
basicinfo entity = new basicinfo();
entity.setname("员工"+i);
entity.setsex(i%2==1?"男":"女");
entity.setdepart(i>80?"销售部":"财务部");
list.add(entity);
}
return list;
}
上一篇: java实现Windows记事本
推荐阅读
-
PHP实现导出excel数据的类库用法
-
python 将数据保存为excel的xls格式(实例讲解)
-
Java web Filter, Strurs2 Interceptor 和 SpringMVC Interceptor 三者之间的联系与区别
-
Python读写Excel文件的实例
-
利用反射获取Java类中的静态变量名及变量值的简单实例
-
php编程实现获取excel文档内容的代码实例_php技巧
-
(转)java Springboot富文本编辑器ueditor的内容使用itext5导出为pdf文件
-
Java中运行环境搭建的图文教程实例
-
java实现IP黑名单的代码实例
-
ExtJs Excel导出并下载IIS服务器端遇到的问题