excel导出

package com.tpaic.budget.presentation.controller.preexpense;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.tpaic.budget.dto.AgingScheduleDTO;
import com.tpaic.budget.util.ServiceRequestID;
import com.tpaic.tpfa.app.biz.dispatch.IDispatchService;
import com.tpaic.tpfa.app.dto.AggregatedSearchDTO;

public class AgingScheduleExcelController extends AbstractController{
private IDispatchService dispatch;

public void setDispatch(IDispatchService dispatch) {
this.dispatch = dispatch;
}
public IDispatchService getDispatch() {
return dispatch;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String financeDepartmentCode=request.getParameter("financeDepartmentCode");
String agingScheduleApplyNo=request.getParameter("agingScheduleApplyNo");
String paymentOfTime=request.getParameter("paymentOfTime");
String transactorName=request.getParameter("transactorName");
String surplusAmount=request.getParameter("surplusAmount");
String start="";
String end="";
//将拿到的账龄区间进行分割
if(paymentOfTime !=null && !"".equals(paymentOfTime)){
if(paymentOfTime=="731" || "731".equals(paymentOfTime)){
start="731";
end="";
}else{
String[] st=paymentOfTime.split("-");
start=st[0];
end=st[1];
}
}
try{
AggregatedSearchDTO Searchdto = new AggregatedSearchDTO();
Searchdto = new AggregatedSearchDTO();
Map parameterMap = new HashMap();
parameterMap.put("financeDepartmentCode", financeDepartmentCode);
parameterMap.put("agingScheduleApplyNo", agingScheduleApplyNo);
parameterMap.put("transactorName", transactorName);
parameterMap.put("surplusAmount", surplusAmount);
parameterMap.put("start", start);
parameterMap.put("end", end);
Searchdto.setCurrentObject(parameterMap);
Searchdto.setStatementId("preexpense.agingSchedule");
Searchdto.setMulti(true);
List<AgingScheduleDTO> agingScheduleDTOList = (List<AgingScheduleDTO>) this.dispatch.dispatchRequest(Searchdto, ServiceRequestID.AGGREGATED_SEARCH_ACTION);

//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet=wb.createSheet("帐龄数据表");
sheet.setVerticallyCenter(true);

//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1=sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell=row1.createCell(0);
//设置单元格内容
cell.setCellValue("动支/付款帐龄数据表");
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,11));
//在sheet里创建第二行
HSSFRow row2=sheet.createRow(1);
//创建单元格并设置单元格内容
row2.createCell(0).setCellValue("机构财务代码");
row2.createCell(1).setCellValue("机构名称");
row2.createCell(2).setCellValue("动支/付款申请号");
row2.createCell(3).setCellValue("动支/付款");
row2.createCell(4).setCellValue("类型(根据动支中的借款类型、付款申请中的内容类型)");
row2.createCell(5).setCellValue("供应商名称");
row2.createCell(6).setCellValue("摘要");
row2.createCell(7).setCellValue("经办人");
row2.createCell(8).setCellValue("部门");
row2.createCell(9).setCellValue("挂账(借款)日期");
row2.createCell(10).setCellValue("挂账余额");
row2.createCell(11).setCellValue("帐龄区间");
sheet.setColumnWidth((short) 0, (short) 3000);
sheet.setColumnWidth((short) 1, (short) 6000);
sheet.setColumnWidth((short) 2, (short) 5000);
sheet.setColumnWidth((short) 3, (short) 3000);
sheet.setColumnWidth((short) 4, (short) 6000);
sheet.setColumnWidth((short) 5, (short) 6000);
sheet.setColumnWidth((short) 6, (short) 6000);
sheet.setColumnWidth((short) 7, (short) 3000);
sheet.setColumnWidth((short) 8, (short) 8000);
sheet.setColumnWidth((short) 9, (short) 3000);
sheet.setColumnWidth((short) 10, (short) 3000);
sheet.setColumnWidth((short) 11, (short) 3000);
/* HSSFFont fontStyle=wb.createFont();
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle tt=wb.createCellStyle();
tt.setFont(fontStyle);
row2.setRowStyle(tt);*/


HSSFCellStyle cellstyle = (HSSFCellStyle) wb.createCellStyle();// 设置表头样式
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中

HSSFCellStyle headerStyle = (HSSFCellStyle) wb .createCellStyle();// 创建标题样式
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //设置垂直居中
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置水平居中
HSSFFont headerFont = (HSSFFont) wb.createFont(); //创建字体样式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
headerFont.setFontName("Times New Roman"); //设置字体类型
headerFont.setFontHeightInPoints((short) 8); //设置字体大小
headerStyle.setFont(headerFont); //为标题样式设置字体样式


HSSFCellStyle headerStyle1 = (HSSFCellStyle) wb .createCellStyle();// 创建标题样式1
headerStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont1 = (HSSFFont) wb.createFont();
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
headerFont1.setFontName("Times New Roman");
headerFont1.setFontHeightInPoints((short) 8);
headerStyle1.setFont(headerFont1);


cell.setCellStyle(headerStyle);
row2.setRowStyle(headerStyle1);

for(int i=0;i<agingScheduleDTOList.size();i++){
//在sheet里创建第三行
HSSFRow row=sheet.createRow(i+2);
row.createCell(0).setCellValue(agingScheduleDTOList.get(i).getFinanceDepartmentCode());
row.createCell(1).setCellValue(agingScheduleDTOList.get(i).getAgingScheduleOrganName());
row.createCell(2).setCellValue(agingScheduleDTOList.get(i).getAgingScheduleApplyNo());
//账龄类型
if(agingScheduleDTOList.get(i).getType()=="0" || agingScheduleDTOList.get(i).getType().equals("0")){
row.createCell(3).setCellValue("动支");
}
if(agingScheduleDTOList.get(i).getType()=="1" || agingScheduleDTOList.get(i).getType().equals("1")){
row.createCell(3).setCellValue("付款");
}
row.createCell(4).setCellValue(agingScheduleDTOList.get(i).getFeetypeName());
row.createCell(5).setCellValue(agingScheduleDTOList.get(i).getVendorName());
row.createCell(6).setCellValue(agingScheduleDTOList.get(i).getDescription());
row.createCell(7).setCellValue(agingScheduleDTOList.get(i).getTransactorName());
row.createCell(8).setCellValue(agingScheduleDTOList.get(i).getTransactorDeparment());
//挂账日期
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
agingScheduleDTOList.get(i).setStartTime(sdf.format(sdf.parse(agingScheduleDTOList.get(i).getStartTime())));
Date startDate=sdf.parse(agingScheduleDTOList.get(i).getStartTime());
row.createCell(9).setCellValue(agingScheduleDTOList.get(i).getStartTime());
//挂账余额
BigDecimal bb=new BigDecimal(agingScheduleDTOList.get(i).getAggregateAmount());
BigDecimal bb1=new BigDecimal(agingScheduleDTOList.get(i).getAlreadyAmount());
BigDecimal bb2=bb.subtract(bb1);
DecimalFormat df=new DecimalFormat("#0.00");
String yy=df.format(bb2);
row.createCell(10).setCellValue(yy);
//账龄区间
Date nowDate=new Date(System.currentTimeMillis());
long ll=-((startDate.getTime()-nowDate.getTime())/(1000*60*60*24));
if(ll>=0 && ll<=30){
row.createCell(11).setCellValue("0-30天");
}
if(ll>30 && ll<=91){
row.createCell(11).setCellValue("31-91天");
}
if(ll>91 && ll<=182){
row.createCell(11).setCellValue("92-182天");
}
if(ll>182 && ll<=365){
row.createCell(11).setCellValue("183-365天");
}
if(ll>365 && ll<=730){
row.createCell(11).setCellValue("366-730天");
}
if(ll>730){
row.createCell(11).setCellValue("731天以上");
}
}
//输出Excel文件 账龄报表.xls
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename="+new String("账龄报表.xls".getBytes("GBK"),"ISO8859-1"));
response.setContentType("application/msexcel");
wb.write(output);
output.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}

}

原文地址:https://www.cnblogs.com/geng-geng1997/p/13745989.html