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包
<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包
相关文章推荐
- java 练习题2
- Myeclipse8.5导入项目后报错:background indexer crash recovery Java.lang.stackoverflowerror
- JAVA基础知识(转)
- java循环语句
- 【第十一章】 SSH集成开发积分商城 之 11.1 概述 ——跟我学spring3
- java抽象类和接口
- Spring mvc数据转换与格式化
- springMVC学习笔记之DispatcherServelet
- Java知识点归纳 ―给Java新手的一些建议(新手必看)
- Java/Java Web中乱码解决汇总
- struts之Cookie会话保持
- Java源码分析之LinkedList
- java 泛型详解
- [转]史上最全最强SpringMVC详细示例实战教程
- Java源码分析之ArrayList
- xutils问题java.io.IOException: stack size 1038KB
- [转]史上最全最强SpringMVC详细示例实战教程
- springMVC学习笔记OpenSessionInView
- java RSA 加密解密
- java ftp