java+jsp导出excel

jsp页面

<button οnclick="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; //这是一个Bean
    if (i!=0) {
       le=(Lexicon) list.get(i-1);//只是得到一个bean集合 不要在乎这个过程
}
       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包


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鑫不列迭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值