您的位置:首页 > 编程语言 > Java开发

java+jsp导出excel

2016-05-16 11:07 411 查看
jsp页面

<button onclick="excelexport()">导出excel</button>

function excelexport(){

$.ajax({

type:"get",

url:"·······.test1.action",

success:function(inputStream){

alert("文件已成功导出到您的桌面!");

},

error:function () {

alert("导出失败!");

}

});

}

action

private InputStream inputStream;

/**

* 导出excel

* @return

*/

public String test1(){

ExportExcel excel=new ExportExcel();

try {

excel.Export(此处参数为集合);

inputStream =new ByteArrayInputStream("导出成功".getBytes("utf-8"));

} catch (Exception e) {

try {

inputStream =new ByteArrayInputStream("导出失败".getBytes("utf-8"));

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

}

}

return "exportExcel";

}

ExportExcel(导出方法类)

import java.io.File;

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import javax.swing.filechooser.FileSystemView;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.bigdatacn.sfgk.wlzbs.domain.Lexicon;

import org.springframework.stereotype.Controller;

@Controller

public class ExportExcel {

/**

* 导出lexicon的信息到Excel

*/

public void Export(List list){

if (null!=list) {

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet("词汇");

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

HSSFRow row = sheet.createRow((int) 0);

// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

for (int i = 0; i < list.size(); i++)

{

Lexicon le=null;

if (i!=0) {

le=(Lexicon) list.get(i-1);//http://blog.163.com/longsu2010@yeah/blog/static/173612348201202114212933/

}

row = sheet.createRow((int) i);

HSSFCell cell = row.createCell((short) 0);

cell.setCellValue(i==0?"创建者":le.getUser().getUserName());

// wb.createFont().setFontHeightInPoints((short) 11);//11号字体

// wb.createFont().setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗

cell.setCellStyle(style);

cell = row.createCell((short)1);

cell.setCellValue(i==0?"创建时间":new SimpleDateFormat("yyyy-mm-dd hh:MM:ss").format(le.getCreateDate()));

cell.setCellStyle(style);

cell = row.createCell((short)2);

cell.setCellValue(i==0?"内容":le.getContains());

cell.setCellStyle(style);

cell = row.createCell((short)3);

cell.setCellValue(i==0?"是否停用":le.getIsPause()==0?"启用":"停用");

cell.setCellStyle(style);

if (i<4) {

sheet.setColumnWidth(i, 8000);

}

}

// 第六步,将文件存到指定位置

try

{

//desktopPath 桌面路径

File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();

String desktopPath = desktopDir.getAbsolutePath();

FileOutputStream fout = new FileOutputStream(desktopPath+"/"+new SimpleDateFormat("yyyymmddhhMMss").format(new Date())+".xls");

wb.write(fout);

fout.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

}

maven的方式,在pom.xml中的配置

<!-- 加入POI核心依赖 -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.14</version>

</dependency>

若是不用maven的方式,则需要导入jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: