poi.jar包

public void downExcel(HttpServletResponse response,Page<ShopApply> page)
throws Exception{

// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("xxx");

sheet.setDefaultColumnWidth((short) 15);//表格宽度

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell=null;
//表头
String[] headers={"申请单类型","编号","所属公司","主题","跳转地址","提交人","提交时间","审核状态","审核人","审核时间","资讯状态"};
for(int i=0;i<headers.length;i++){
cell = row.createCell((short) i);
cell.setCellValue(headers[i]);
cell.setCellStyle(style);
}

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
StringBuffer str=new StringBuffer();
str.append("SELECT s.`c_checkid`,s.c_checkuser,s.c_applytype,g.c_companyname,s.`c_beforetopic`,s.`c_aftertopic`,s.`c_beforeimage`,s.`c_afterimage`,s.`c_checkstatus`,s.`dt_checkdate`,s.`c_adduser`,s.`dt_adddate`,m.`c_manageid`,m.`c_forwardurl`,m.c_isdel FROM shop_check s ,shop_website_manage m ,`global_company` g WHERE s.c_type=6 AND s.`c_businessid`=m.`c_manageid` AND m.c_companyid = g.c_companyid");
Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
page=this.makingApplyService.getList(str.toString(), page);

List<Object> rs=(List<Object>) page.getQueryList();

for (int i = 0; i < rs.size(); i++)
{
Object obj=rs.get(i);
Map<String, String> objs=(Map<String, String>)obj;

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

// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(String.valueOf(objs.get("c_applytype")).equals("0")?"新申请单":"修改类申请单");
row.createCell((short) 1).setCellValue(objs.get("c_manageid"));
row.createCell((short) 2).setCellValue(objs.get("c_companyname"));
row.createCell((short) 3).setCellValue(objs.get("c_aftertopic"));
row.createCell((short) 4).setCellValue(objs.get("c_forwardurl"));

row.createCell((short) 5).setCellValue(objs.get("c_adduser"));
row.createCell((short) 6).setCellValue(df.format(objs.get("dt_adddate"))==null?"":df.format(objs.get("dt_adddate")));

String checkstate="";
if(objs.get("c_checkstatus").equals("0")){
checkstate="待审核";
}else if(objs.get("c_checkstatus").equals("1")){
checkstate="审核通过";
}else if(objs.get("c_checkstatus").equals("2")){
checkstate="审核不通过";
}
row.createCell((short) 7).setCellValue(checkstate);
row.createCell((short) 8).setCellValue(objs.get("c_checkuser"));
row.createCell((short) 9).setCellValue(String.valueOf(objs.get("dt_checkdate")).length()>0?String.valueOf(objs.get("dt_checkdate")):null);

row.createCell((short) 10).setCellValue(String.valueOf(objs.get("c_isdel")).equals("1")?"N":"Y");
}
// 第六步,将文件存到指定位置
try
{
//导出的文件名
String fileName="photo_"+(new SimpleDateFormat("yyyyMMddHH").format(new Date()))+".xls";;

//导出
this.export(wb, this.getFilePath(), fileName);

// 下载Excel
this.download(response, this.getFilePath(), fileName);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/*
* 导出
* @Param 路径,文件名
*
* */
public void export(HSSFWorkbook wb,String filePath,String fileName){
// 第六步,将文件存到指定位置
try
{
long start = System.currentTimeMillis();

//全路径
String filefull=filePath+"/"+fileName;

File floder=new File(filePath);
if (!floder.exists() && !floder.isDirectory())
{
floder.mkdir();
}

FileOutputStream fout = new FileOutputStream(filefull);
wb.write(fout);
fout.flush();
fout.close();
System.out.println("*******导出EXCEL结束*********");
long end = System.currentTimeMillis();
System.out.println("----完成该操作共用的时间是:"+(end-start)/1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/*
* 下载
* @Param 路径,文件名
*
* */
public void download(HttpServletResponse response,String filePath , String fileName){

try
{
System.out.println("*******下载EXCEL开始*********");
FileInputStream fis = null;
OutputStream os = null;

//全路径
String filefull=filePath+"/"+fileName;

File file = new File(filefull);
if (!file.exists()) {
System.out.println("文件下载失败:文件或路径错误");

}

fis = new FileInputStream(file);
os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
response.setContentType("application/x-download");
byte[] mybyte = new byte[8192];
int len = 0;
while ((len = fis.read(mybyte)) != -1) {
os.write(mybyte, 0, len);
}
fis.close();
os.close();
System.out.println("*******下载EXCEL结束*********");
}
catch (Exception e)
{
e.printStackTrace();
}
}

*****************************不生成文件,按一定的格式放到流里*********************

public InputStream getTxtFile(){

setFileName("mobiles_"+(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+".txt");//设置导出的Excel名称
Shop shopNum=new Shop();
StringBuffer sf=new StringBuffer();

List datas=this.getService().find();

try{
String str="";
String enter = "\r\n";

for (int i = 0; i < datas.size(); i++) {
shopNum=(Shop) datas.get(i);
if(shopNum.getCMobile() !=null && shopNum.getCMobile() !="" && shopNum.getCMobile().length()>10)
{
str=shopNum.getCMobile() + enter;
sf.append(str);

}
}

}catch(Exception e){
return null;
}

byte[] txt=null;
try{

txt=sf.toString().getBytes("UTF-8");
}catch (Exception e){
txt=sf.toString().getBytes();
}
InputStream input=new ByteArrayInputStream(txt);//用字节数组构造一个输入流

return input;

}

poi excel导出,下载的更多相关文章

  1. thinkphp3.2.3 excel导出,下载文件,包含图片

    关于导出后出错的问题 https://segmentfault.com/q/1010000005330214 https://blog.csdn.net/ohmygirl/article/detail ...

  2. POI excel导出

    ******************************* excel表格导出,使用POI实现 ******************************* 实现导出步骤 --配置导出excel ...

  3. java POI excel 导出复合样式(一个单元格两个字体)

    前言:java poi 导出 excel 时,需要设置一个单元格有多个字体样式,有点类似于富文本. 想要达到的效果(一个单元格里): 我使用的 poi 版本是 <dependency> & ...

  4. poi excel导出 xssf 带下拉框

    需求:导出之后带有二级级联的下拉框.(类似于省市). 最初的思路是怀疑是不是数组内串太多了,导出之后的excel有36行,调试的误区在于刚开始认为对行数有限制,后自己写了一个测试类,才发现不是行数,而 ...

  5. POI Excel导出样式设置

    HSSFSheet sheet = workbook.createSheet("sheetName");    //创建sheet sheet.setVerticallyCente ...

  6. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  7. Java导出excel并下载功能

    我们使用的导出并下载功能是利用一个插件叫POI的插件提供的导出功能,很实用:首先先导入Jar包: Jar包下载地址:http://poi.apache.org/   官方文档地址:http://poi ...

  8. springMVC中使用POI方式导出excel至客户端、服务器实例

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 这里的方法支持导出excel至项目所在服务器,或导 ...

  9. SpringBoot加Poi仿照EasyPoi实现Excel导出

    POI提供API给Java程序对Microsoft Office格式档案读和写的功能,详细功能可以直接查阅API,因为使用EasyPoi过程中总是缺少依赖,没有搞明白到底是什么坑,索性自己写一个简单工 ...

随机推荐

  1. RTP RTCP在音视频传输与同步方面的使用

    转自:http://blog.csdn.net/kof98765/article/details/17733701 1 音视频实时传输 1.1 Jrtplib库介绍 本系统采用开源库Jrtplib进行 ...

  2. myeclipse报错: java compiler level does not match the version of the installed java project facet

    在升级到myeclipse 9.0正式版后,很无耐地出发现了一个error级别的错误,虽然没在代码中,但是看着让人很不舒服.第一反应就是到网上搜索解决之道,结果,网站说在工程的属性中去找个叫啥&quo ...

  3. hdu 5833 Zhu and 772002 高斯消元

    Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...

  4. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  5. opengl典型例程立方体投影与地图绘制

    立方体投影 http://www.cnblogs.com/opengl/p/3790450.html 地图绘制 http://www.cnblogs.com/opengl/p/3790354.html

  6. jsp网站环境搭建

    工具:tomcat7(exe安装版).jre7.javaxcms(安装版.非源码).mysql 1.先安装jre7,或者安装java7(自带了jre7) 2.安装tomcat7,期间要选择jre7安装 ...

  7. 防止ViewPager和Fragment结合使用时候的数据预加载

    不知道你们使用ViewPager和Fragment结合的时候发现一个问题没,如果你的每个Fragment都需要请求网络数据,并且你在请求网络数据的时候会加入进度对话框的加载显示效果,当你显示第一个Fr ...

  8. loadrunner处理HTTP重定向请求

    //place this in global.h     int HttpRetCode;  int i=0;  char depthVal[10];  char cTransactName[2000 ...

  9. wpf ,tooltip的style

    <Style x:Key="MyToolTipStyle" TargetType="ToolTip"> <Setter Property=&q ...

  10. NumPy 学习(1): ndarrays

    Numpy 是Numerical Python的简写,用来进行高性能的科学计算以及数据分析的基础包.它是一些高级工具(pandas)的基础.它主要提供以下几个功能: (1). ndarray:计算快, ...