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

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);

}

}

}

时间: 2024-07-31 18:46:20

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

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

现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: 1 <?php 2 date_default_timezone_set('PRC');//设置时区 3 4 /*设置head头信息*/ 5 Header("Content-Type:application/vnd.ms-excel;charset=UTF-8"); 6 Header("Accept-Ranges:bytes"); 7 Header("Conte

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

将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下:  WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar (2)StringBuffer转换为二维数组 //定义一个StringBuffer,以 \r\n 分一维数组,以 \t 分二维数组 StringBuffer strff = new StringBuffer("姓名\t年龄\t性别\r\n小仙女\t18\t女\r\n"); //将StringBuffer数据转换为一维数组:

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

1.从数据查出数据扔进table中: private DataTable getTable() { var dbHelper = applyBLL.CreateDataBase("VISAdoDb"); StringBuilder SB = new StringBuilder(); SB.AppendFormat("Select ApplyForID,Applicants,CardNo from VIS_ApplyFor where iState = 0 and (Apply

springmvc+mybatis+html 下将查询数据以excell形式上传到ftp(下)

上节讲到的是从数据库中查询相应的结果以excell形式写到ftp服务器上,今天又试了试从ftp上将excell 文件下载到本地目录,一开始的时候遇到了中文乱码问题,文件名中含有中文下载下来文件名为乱码,以下贴出核心代码 1 package com.ninefbank.smallpay.admin.util; 2 3 import java.io.BufferedOutputStream; 4 import java.io.File; 5 import java.io.FileInputStrea

【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出

篇一:WinForm开发总体概述与技术实现 篇二:WinForm开发扇形图统计和Excel数据导出 篇三:Access远程连接数据库和窗体打包部署 [小记]:最近基于WinForm+Access数据库完成一个法律咨询管理系统.本系统要求类似网页后台管理效果,并且基于局域网内,完成多客户端操作同一数据库,根据权限不同分别执行不同功能模块.核心模块为级联统计类型管理.数据库咨询数据扇形统计.树的操作.咨询数据的管理.手写分页.Excel数据的导出.多用户操作服务器数据等.并支持多用户同时操作,远程连

Exchange2013配置-导出用户邮箱为PST文件

如何导出用户邮箱为PST文件 描述:客户端本地可以创建邮箱归档,有时候出于管理需要,可能要在服务器上为某用户创建邮箱归档. 分析:邮箱服务器上创建归档的命令是new-mailboxexportrequest,但默认情况,没有用户被委派执行该命令的权限,因此,实现之前需要委派权限. Step1:委派给administrator相应权限 New-ManagementRoleAssignment –Role "Mailbox Import Export" –User Administrato

.Net core 使用NPOI 直接导入Excel到数据库(即不先将Excel保存到服务器再读取文件到数据库)

原文:.Net core 使用NPOI 直接导入Excel到数据库(即不先将Excel保存到服务器再读取文件到数据库) 1 /// <summary> 2 /// 导入信息 3 /// </summary> 4 /// <param name="file"></param> 5 /// <returns></returns> 6 /// /Public/PublicPool/ImportCustomer 7 pub

java导出excel 浏览器直接下载或者或以文件形式导出

1 /** 2 * excel表格直接下载 3 */ 4 public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse httpServletResponse,String fileName) throws Exception { 5 //响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着不想直接显示内容 6 httpServletResponse.setC

excel导入 导出 兼容各个版本服务器不装EXCEL也可以

1 首先要引用 NPOI.dll (可在网上下载!) 2 //导入 3 public void OnSubmit() 4 { 5 string path = Server.MapPath("/upload/201410/27/201410271103461051.xls"); 6 FileStream fs = File.Open(path, FileMode.Open); 7 System.Data.DataTable dt = ConvertToDataTable(fs); 8 9