public void exportExcel(List<P2pInfo> repayXist,HttpServletRequest request,HttpServletResponse response,List<DimNode> listArea,String drxh) throws Exception{

log.info("导出银还款信息Excel文件");

FileOutputStream fos=null;

InputStream is=null;

OutputStream os=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try {

//当前日期

String dateStr=DateUtil.parseDateFormat(new Date(), "yyyyMMdd");

//银导出文件服务器上存放路径

String ysbPath=getSystemConfigValue(ConstantsApplication.YSB_EXPORT_PATH);

String folderFileName="YSB" + dateStr + "_" + drxh;

String filePath=ysbPath+"/"+folderFileName;

StringOperator.deletePath(filePath);

File file=new File(filePath);

file.delete();

CommonUtils.mkDirs(filePath);

String fileNameHtml=ConstantsApplication.YSB_OPERATION_CODE+"_"+dateStr+"_"+drxh+".xls";

String fileName=filePath+"/"+fileNameHtml;

/*导出到服务器上指定文件:D:\ExportYSB下*/

fos=new FileOutputStream(fileName);

//创建Excel文件对象    H

SSFWorkbook workbook = new HSSFWorkbook();

//创建Sheet对象    HSSFSheet sheet1 = workbook.createSheet();

//设置Excel样式

// 设置列宽

sheet1.setColumnWidth(0, 3000);

sheet1.setColumnWidth(1, 6000);

sheet1.setColumnWidth(2, 3000);

sheet1.setColumnWidth(3, 4000);

sheet1.setColumnWidth(4, 3000);

// 设置标题字体

HSSFFont headfont = workbook.createFont();

headfont.setFontName("黑体");

headfont.setFontHeightInPoints((short) 10);

// 字体大小

headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗       // 标题样式

HSSFCellStyle headstyle = workbook.createCellStyle();

headstyle.setFont(headfont);

headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中

headstyle.setLeftBorderColor(HSSFColor.BLACK.index);

headstyle.setBorderLeft((short) 1);

headstyle.setRightBorderColor(HSSFColor.BLACK.index);

headstyle.setBorderRight((short) 1);

headstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体

headstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.        /*普通单元格样式*/

HSSFFont font = workbook.createFont();

font.setFontName("宋体");

font.setFontHeightInPoints((short) 10);

// 普通单元格样式

HSSFCellStyle contentStyle = workbook.createCellStyle();

contentStyle.setFont(font);

contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中

contentStyle.setWrapText(true);

contentStyle.setLeftBorderColor(HSSFColor.BLACK.index);

contentStyle.setBorderLeft((short) 1);

contentStyle.setRightBorderColor(HSSFColor.BLACK.index);

contentStyle.setBorderRight((short) 1);

contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体

contentStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.

//创建行对象

HSSFRow row1;

//第一行标题

row1 = sheet1.createRow(0);

row1.createCell(0).setCellValue("帐户名");

row1.createCell(1).setCellValue("银行账号");

row1.createCell(2).setCellValue("收款金额");

row1.createCell(3).setCellValue("资金用途");

row1.createCell(4).setCellValue("商家订单号");

//为标题引入上面设置的样式

row1.getCell(0).setCellStyle(headstyle);

row1.getCell(1).setCellStyle(headstyle);

row1.getCell(2).setCellStyle(headstyle);

row1.getCell(3).setCellStyle(headstyle);

row1.getCell(4).setCellStyle(headstyle);

row1.setHeightInPoints((short)20);

if (null!=repayXist&&repayXist.size()>0) {

int size=repayXist.size()+1;

int j=0;

P2pRepayInfo p2pRepayInfo=null;

DimNode dimNode=null;

for (int i = 1; i < size; i++) {

j=i-1;

p2pRepayInfo=repayXist.get(j);

row1 = sheet1.createRow(i);

row1.createCell(0).setCellValue(p2pRepayInfo.getLoanName());

row1.createCell(1).setCellValue(p2pRepayInfo.getFactRepayAccountNo());      //应还本金+利息

Double dueCI=(p2pRepayInfo.getDuetoCapital()==null?0:p2pRepayInfo.getDuetoCapital())+(p2pRepayInfo.getDuetoInterest()==null?0:p2pRepayInfo.getDuetoInterest());      //已还本金+已还利息

Double paidCI=(p2pRepayInfo.getPaidCapital()==null?0:p2pRepayInfo.getPaidCapital())+(p2pRepayInfo.getPaidInterest()==null?0:p2pRepayInfo.getPaidInterest());

row1.createCell(2).setCellValue(dueCI-paidCI);

row1.createCell(3).setCellValue(" ");      //商家订单号——地区

for (int k = 0; k < listArea.size(); k++) {

dimNode=listArea.get(k);

if (dimNode.getNodeNo().equals(p2pRepayInfo.getAreaNo())) {

row1.createCell(4).setCellValue(dimNode.getNodeName());

break;

}

}

row1.getCell(0).setCellStyle(contentStyle);

row1.getCell(1).setCellStyle(contentStyle);

row1.getCell(2).setCellStyle(contentStyle);

row1.getCell(3).setCellStyle(contentStyle);

row1.getCell(4).setCellStyle(contentStyle);

row1.setHeightInPoints((short)15);

}

}

workbook.write(fos);

/*将保存到服务器上的Excel文件读取到客户端*/

//清空输出流

response.reset();

response.setCharacterEncoding("utf-8");

response.setHeader("Content-disposition", "attachment;filename="+fileNameHtml);

response.setContentType("application/msexcel");

is=new FileInputStream(fileName);

bis=new BufferedInputStream(is);

os=response.getOutputStream();

bos=new BufferedOutputStream(os);

int read=0;

byte[] bytes=new byte[8072];

while ((read=bis.read(bytes,0, bytes.length))!=-1) {

bos.write(bytes, 0, read);

}

bos.flush();

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

fos.close();

os.close();

bos.close();

is.close();

bis.close();

} catch (IOException e) {

log.info("银生宝还款信息文件导出异常……");

e.printStackTrace();

throw new Exception("银生宝还款信息文件导出异常……", e);

}

}

}

//删除目录下文件
    public static void deletePath(String filepath) throws Exception {
     File f = new File(filepath);//定义文件路径         
        if(f.exists() && f.isDirectory()){//判断是文件还是目录  
        //若有则把文件放进数组,并判断是否有下级目录  
        File delFile[]=f.listFiles();  
        int i =f.listFiles().length;  
        for(int j=0;j<i;j++){  
            if(delFile[j].isDirectory()){  
             deletePath(delFile[j].getAbsolutePath());//递归调用del方法并取得子目录路径  
             }  
             delFile[j].delete();//删除文件  
        }
        }
    }

/**   *   * @功能描述:判断文件目录是否存在如果不存在则创建目录

* @param filePath   *

目录路径   *

@return   *

@throws Exception   *

@创建时间   * @author zst   *

@throws APSException   */

public static void mkDirs(String filePath) {

// 判断路径是否存在

File directory = new File(filePath.toString());

if (directory.exists() && directory.isDirectory()) {

}   else {    // 如果不存在则创建目录    directory.mkdirs();   }  }

数据以Excel形式导出导服务器,再将文件读取到客户端另存 以HSSFWorkbook方式实现的更多相关文章

  1. PHP中导出Excel,将数据以Excel形式导出

    现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: <?php date_default_timezone_set('PRC');//设置时区 /*设置h ...

  2. Java使用POI插件将数据以excel形式备份

    将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下:  WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar 下载链接:https://gith ...

  3. Java将数据以Excel文件形式导出后台代码实现

    下面代码实现所需jar包: tomcat-embed-core-8.5.11.jar: commons-lang3-3.0.1.jar: commons-io-2.5.jar: poi-3.9.jar ...

  4. Web浏览器导出FTP服务器上的文件

    开发思路:1.代码登录ftp服务器下载文件到服务器2.通过web浏览器下载服务器上的文件 using System; using System.Collections; using System.Co ...

  5. 将mysql数据库数据以Excel文件的形式导出

    最近在工作中,领导让从数据库中导出一些数据并存放到Excel表格中,网上有许多教程,下面是我总结的其中俩种方法. 从数据库管理工具中导出(navicat) 在navicat导出数据导Excel中还是比 ...

  6. 数据库数据以Excel的方式导出

    import java.io.Serializable; import java.util.List; import com.cfets.cwap.s.util.db.TableColumn; /** ...

  7. SAP ABAP: 把内表数据以excel或csv格式,通过前台或者后台的方式上传至FTP服务器

    今天接到一个FTP的需求,就是每天晚上把当天某个报表的数据自动保存excel上传到FTP服务器. SAP已经有现成的FTP函数使用,可以通过函数的方式来实现,实现前先准备一些数据: User:登录FT ...

  8. ASP.NET列表信息以Excel形式导出

    1.从数据查出数据扔进table中: private DataTable getTable() { var dbHelper = applyBLL.CreateDataBase("VISAd ...

  9. redisTemplate实现轻量级消息队列, 异步处理excel并实现腾讯云cos文件上传下载

    背景 公司项目有个需求, 前端上传excel文件, 后端读取数据.处理数据.返回错误数据, 最简单的方式同步处理, 客户端上传文件后一直阻塞等待响应, 但用户体验无疑很差, 处理数据可能十分耗时, 没 ...

随机推荐

  1. deb、rpm、tar.gz三种Linux软件包的区别

    初接解LINUX的,同样都是for linux,但rpm.tar.gz.deb包还是有很大区别的, 这种区别可使安装过程进行不下去.那我们应该下载什么格式的包呢? rpm包-在红帽LINUX.SUSE ...

  2. smarty变量

    前台: 注释的两种方式:<{**}>和<!--注释html代码-->比如: <{* <div style="width:100px; height:100 ...

  3. ajax用户名案例(重点)

      要求:失去焦点时如下效果 主页代码 <body> 用户名:<input type="text" id="a" /><div i ...

  4. 【jmeter】测试报告优化<二>

    如果按JMeter默认设置,生成报告如下: 从上图可以看出,结果信息比较简单,对于运行成功的case,还可以将就用着.但对于跑失败的case,就只有一行assert错误信息.(信息量太少了,比较难找到 ...

  5. AngularJs中的服务

    一.angularJs中的简单服务应用 下面的例子让我们明白在AngularJs中如何去调用文件中的数据,从而将文件中的数据显示在页面上;改变url的地址,也可以去调用后台接口. 实例: <!D ...

  6. CK方程

    上文中,“到时间n为止进入任意一个特定的状态集合”应理解为“在时间n及之前进入的都算”. 只要进入了该状态集合,之后是否离开已经不重要了.这个可类比于“先赢若干局”的赌徒问题:即使在赢得若干局后继续赌 ...

  7. node.js的作用、回调、同步异步代码、事件循环

    http://www.nodeclass.com/articles/39274 一.node.js的作用 I/O的意义,(I/O是输入/输出的简写,如:键盘敲入文本,输入,屏幕上看到文本显示输出.鼠标 ...

  8. CentOS7安装Oracle 11g R2 详细过程——零基础

    本人linux小白,因项目原因必须要在linux下使用oracle便开始了探索.安装过程中遇到了种种问题与原因,今天整理一下方便后面的可以少走弯路. *注明: 安装过程注意当前错作的用户,执行./ru ...

  9. 黄聪:Dsicuz x2.5、X3、X3.2如何去掉域名后面的/forum.php

    Dsicuz x2.5去掉域名后面的/forum.php 1, 后台--全局--域名设置--应用域名--设置默认域名为访问域名就可以,如:www.xxxxx.com 上面2种方法都可以去掉域名后面的/ ...

  10. 黄聪:Discuz!的SEO优化策略一:如何设置标题 & 如何去掉Powered by Discuz!尾巴

    1.如何设置标题 进入 管理中心 -- 全局 -- SEO设置 -- 论坛 -- 标题 ,设置你的网站标题和描述. PS:有人问keywords要不要设置,其实现在keywords对SEO的影响已经微 ...