struts下载文件配置
<struts>
<package name="yuyue" extends="cic" namespace="/yuyue">
<action name="download" class="yuYueAction" method="download">
<result name="export" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=UTF-8</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${xlsName}.zip</param>
<param name="bufferSize">20480</param>
</result>
<result name ="reSendUrl" type="redirect">${requestUrl}</result>
</action>
</package>
</struts>
/**
* 下载文件excel
* @return
*/
public String download(){
QueryRule queryRule = QueryRule.getInstance();
boolean flag = true;
if(queryModel!=null){
if(StringUtils.isNotEmpty(queryModel.getProductCode())){
queryRule.addEqual("productCode", queryModel.getProductCode());
}else if(StringUtils.isNotEmpty(queryModel.getProductType())){
if(("card").equals(queryModel.getProductType())){
queryRule.addIn("productCode", "trafficjd","trafficzg","trafficzg","children");
}else{
queryRule.addIn("productCode", "chexian","xueping");
}
}
if(StringUtils.isNotBlank(queryModel.getBusinessName()) || StringUtils.isNotBlank(queryModel.getBusinessPhone())){
List<String> businessCodeList = yuYueService.getBusinessCode(queryModel.getBusinessName(),queryModel.getBusinessPhone());
if(businessCodeList==null || businessCodeList.size()<1){
flag = false;
}
}
if(queryModel.getDate1()!=null){
queryRule.addGreaterEqual("createDate",queryModel.getDate1());
}
if(queryModel.getDate2()!=null){
queryRule.addLessEqual("createDate", queryModel.getDate2());
}
}
queryRule.addDescOrder("createDate");
List<YzYuyueInfo> reportList = null;
if(flag){
reportList = yuYueService.getAllByQueryRule(queryRule);
}else {
return null;
}
Date sysTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMddhhmmss");
xlsName = "预约投保-"+sdf.format(sysTime);
//第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook hwk = new HSSFWorkbook();
//第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = hwk.createSheet(xlsName);
sheet.setColumnWidth(0, 7*257);
sheet.setColumnWidth(1, 30*257);
sheet.setColumnWidth(2, 25*257);
sheet.setColumnWidth(3, 20*257);
sheet.setColumnWidth(4, 25*257);
sheet.setColumnWidth(5, 25*257);
sheet.setColumnWidth(6, 30*257);
sheet.setColumnWidth(7, 40*257);
sheet.setColumnWidth(8, 25*257);
sheet.setColumnWidth(9, 25*257);
sheet.setColumnWidth(10, 20*257);
sheet.setColumnWidth(11, 25*257);
//第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow rowx = sheet.createRow(0);
HSSFRow row = sheet.createRow(1);
row.setHeight((short)500);
rowx.setHeight((short)500);
//第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle titleStyle =hwk.createCellStyle();
HSSFCellStyle style =hwk.createCellStyle();
HSSFFont hf = hwk.createFont();
hf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hf.setFontHeightInPoints((short)12);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell hcl = rowx.createCell(1);
hcl.setCellValue("预约投保提交日期");
hcl.setCellStyle(titleStyle);
titleStyle.setFont(hf);
HSSFCell hc = row.createCell(0);
hc.setCellValue("序号");
hc.setCellStyle(titleStyle);
hc = row.createCell(1);
hc.setCellValue("预约编号");
hc.setCellStyle(titleStyle);
hc = row.createCell(2);
hc.setCellValue("产品");
hc.setCellStyle(titleStyle);
hc = row.createCell(3);
hc.setCellValue("提交日期");
hc.setCellStyle(titleStyle);
hc = row.createCell(4);
hc.setCellValue("客户姓名");
hc.setCellStyle(titleStyle);
hc = row.createCell(5);
hc.setCellValue("客户联系方式");
hc.setCellStyle(titleStyle);
hc = row.createCell(6);
hc.setCellValue("配送地址");
hc.setCellStyle(titleStyle);
hc = row.createCell(7);
hc.setCellValue("业务员姓名");
hc.setCellStyle(titleStyle);
hc = row.createCell(8);
hc.setCellValue("业务员联系方式");
hc.setCellStyle(titleStyle);
hc = row.createCell(9);
hc.setCellValue("备注");
hc.setCellStyle(titleStyle);
//第五步,写入实体数据 实际应用中这些数据从数据库得到
int rowNum = 1;
int rownum = 0;
for(YzYuyueInfo yzYuyueInfo : reportList){
rowNum++;
rownum++;
row = sheet.createRow(rowNum);
row.setHeight((short)500);
hc = row.createCell(0);
hc.setCellValue(rownum);
hc.setCellStyle(style);
hc = row.createCell(1);
hc.setCellValue(yzYuyueInfo.getSerialno() + "");
hc.setCellStyle(style);
hc = row.createCell(2);
hc.setCellValue(yzYuyueInfo.getProductName() + "");
hc.setCellStyle(style);
hc = row.createCell(3);
hc.setCellValue(yzYuyueInfo.getCreateDate() + "");
hc.setCellStyle(style);
String contactName = "";
String contactPhone = "";
String contactAddress = "";
if(yzYuyueInfo.getYuyueInsureList()!=null){
for(YzYuyueInsured insured : yzYuyueInfo.getYuyueInsureList()){
if(("01").equals(insured.getInsuredType())){//联系人
contactName = insured.getName();
contactPhone = insured.getPhone();
contactAddress = insured.getAddress();
}
}
}
hc = row.createCell(4);
hc.setCellValue(contactName + "");
hc.setCellStyle(style);
hc = row.createCell(5);
hc.setCellValue(contactPhone + "");
hc.setCellStyle(style);
hc = row.createCell(6);
hc.setCellValue(contactAddress + "");
hc.setCellStyle(style);
hc = row.createCell(7);
if (yzYuyueInfo.getYuyueBusiness() != null) {
hc.setCellValue(yzYuyueInfo.getYuyueBusiness().getBusinessName() + "");
}else {
hc.setCellValue("");
}
hc.setCellStyle(style);
hc = row.createCell(8);
if (yzYuyueInfo.getYuyueBusiness() != null) {
hc.setCellValue(yzYuyueInfo.getYuyueBusiness().getMobile() + "");
}else {
hc.setCellValue("");
}
hc.setCellStyle(style);
hc = row.createCell(9);
hc.setCellValue(yzYuyueInfo.getDes() + "");
hc.setCellStyle(style);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(baos); // 压缩xls文件的流
ZipEntry entry = new ZipEntry(xlsName + ".xls");
byte[] byteArray = null;
try {
zipStream.putNextEntry(entry);
ByteArrayOutputStream writeToBytes = new ByteArrayOutputStream();//将HSSFWorkbook读到内存中
hwk.write(writeToBytes);
byte[] bytes = writeToBytes.toByteArray();
writeToBytes.close();
zipStream.write(bytes);
zipStream.close(); // 关闭压缩流
byteArray = baos.toByteArray();
baos.close();
inputStream = new ByteArrayInputStream(byteArray);
} catch (IOException e) {
e.printStackTrace();
}
try {
xlsName= new String(xlsName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "export";
}
<struts>
<package name="yuyue" extends="cic" namespace="/yuyue">
<action name="download" class="yuYueAction" method="download">
<result name="export" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=UTF-8</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${xlsName}.zip</param>
<param name="bufferSize">20480</param>
</result>
<result name ="reSendUrl" type="redirect">${requestUrl}</result>
</action>
</package>
</struts>
/**
* 下载文件excel
* @return
*/
public String download(){
QueryRule queryRule = QueryRule.getInstance();
boolean flag = true;
if(queryModel!=null){
if(StringUtils.isNotEmpty(queryModel.getProductCode())){
queryRule.addEqual("productCode", queryModel.getProductCode());
}else if(StringUtils.isNotEmpty(queryModel.getProductType())){
if(("card").equals(queryModel.getProductType())){
queryRule.addIn("productCode", "trafficjd","trafficzg","trafficzg","children");
}else{
queryRule.addIn("productCode", "chexian","xueping");
}
}
if(StringUtils.isNotBlank(queryModel.getBusinessName()) || StringUtils.isNotBlank(queryModel.getBusinessPhone())){
List<String> businessCodeList = yuYueService.getBusinessCode(queryModel.getBusinessName(),queryModel.getBusinessPhone());
if(businessCodeList==null || businessCodeList.size()<1){
flag = false;
}
}
if(queryModel.getDate1()!=null){
queryRule.addGreaterEqual("createDate",queryModel.getDate1());
}
if(queryModel.getDate2()!=null){
queryRule.addLessEqual("createDate", queryModel.getDate2());
}
}
queryRule.addDescOrder("createDate");
List<YzYuyueInfo> reportList = null;
if(flag){
reportList = yuYueService.getAllByQueryRule(queryRule);
}else {
return null;
}
Date sysTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMddhhmmss");
xlsName = "预约投保-"+sdf.format(sysTime);
//第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook hwk = new HSSFWorkbook();
//第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = hwk.createSheet(xlsName);
sheet.setColumnWidth(0, 7*257);
sheet.setColumnWidth(1, 30*257);
sheet.setColumnWidth(2, 25*257);
sheet.setColumnWidth(3, 20*257);
sheet.setColumnWidth(4, 25*257);
sheet.setColumnWidth(5, 25*257);
sheet.setColumnWidth(6, 30*257);
sheet.setColumnWidth(7, 40*257);
sheet.setColumnWidth(8, 25*257);
sheet.setColumnWidth(9, 25*257);
sheet.setColumnWidth(10, 20*257);
sheet.setColumnWidth(11, 25*257);
//第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow rowx = sheet.createRow(0);
HSSFRow row = sheet.createRow(1);
row.setHeight((short)500);
rowx.setHeight((short)500);
//第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle titleStyle =hwk.createCellStyle();
HSSFCellStyle style =hwk.createCellStyle();
HSSFFont hf = hwk.createFont();
hf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hf.setFontHeightInPoints((short)12);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell hcl = rowx.createCell(1);
hcl.setCellValue("预约投保提交日期");
hcl.setCellStyle(titleStyle);
titleStyle.setFont(hf);
HSSFCell hc = row.createCell(0);
hc.setCellValue("序号");
hc.setCellStyle(titleStyle);
hc = row.createCell(1);
hc.setCellValue("预约编号");
hc.setCellStyle(titleStyle);
hc = row.createCell(2);
hc.setCellValue("产品");
hc.setCellStyle(titleStyle);
hc = row.createCell(3);
hc.setCellValue("提交日期");
hc.setCellStyle(titleStyle);
hc = row.createCell(4);
hc.setCellValue("客户姓名");
hc.setCellStyle(titleStyle);
hc = row.createCell(5);
hc.setCellValue("客户联系方式");
hc.setCellStyle(titleStyle);
hc = row.createCell(6);
hc.setCellValue("配送地址");
hc.setCellStyle(titleStyle);
hc = row.createCell(7);
hc.setCellValue("业务员姓名");
hc.setCellStyle(titleStyle);
hc = row.createCell(8);
hc.setCellValue("业务员联系方式");
hc.setCellStyle(titleStyle);
hc = row.createCell(9);
hc.setCellValue("备注");
hc.setCellStyle(titleStyle);
//第五步,写入实体数据 实际应用中这些数据从数据库得到
int rowNum = 1;
int rownum = 0;
for(YzYuyueInfo yzYuyueInfo : reportList){
rowNum++;
rownum++;
row = sheet.createRow(rowNum);
row.setHeight((short)500);
hc = row.createCell(0);
hc.setCellValue(rownum);
hc.setCellStyle(style);
hc = row.createCell(1);
hc.setCellValue(yzYuyueInfo.getSerialno() + "");
hc.setCellStyle(style);
hc = row.createCell(2);
hc.setCellValue(yzYuyueInfo.getProductName() + "");
hc.setCellStyle(style);
hc = row.createCell(3);
hc.setCellValue(yzYuyueInfo.getCreateDate() + "");
hc.setCellStyle(style);
String contactName = "";
String contactPhone = "";
String contactAddress = "";
if(yzYuyueInfo.getYuyueInsureList()!=null){
for(YzYuyueInsured insured : yzYuyueInfo.getYuyueInsureList()){
if(("01").equals(insured.getInsuredType())){//联系人
contactName = insured.getName();
contactPhone = insured.getPhone();
contactAddress = insured.getAddress();
}
}
}
hc = row.createCell(4);
hc.setCellValue(contactName + "");
hc.setCellStyle(style);
hc = row.createCell(5);
hc.setCellValue(contactPhone + "");
hc.setCellStyle(style);
hc = row.createCell(6);
hc.setCellValue(contactAddress + "");
hc.setCellStyle(style);
hc = row.createCell(7);
if (yzYuyueInfo.getYuyueBusiness() != null) {
hc.setCellValue(yzYuyueInfo.getYuyueBusiness().getBusinessName() + "");
}else {
hc.setCellValue("");
}
hc.setCellStyle(style);
hc = row.createCell(8);
if (yzYuyueInfo.getYuyueBusiness() != null) {
hc.setCellValue(yzYuyueInfo.getYuyueBusiness().getMobile() + "");
}else {
hc.setCellValue("");
}
hc.setCellStyle(style);
hc = row.createCell(9);
hc.setCellValue(yzYuyueInfo.getDes() + "");
hc.setCellStyle(style);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(baos); // 压缩xls文件的流
ZipEntry entry = new ZipEntry(xlsName + ".xls");
byte[] byteArray = null;
try {
zipStream.putNextEntry(entry);
ByteArrayOutputStream writeToBytes = new ByteArrayOutputStream();//将HSSFWorkbook读到内存中
hwk.write(writeToBytes);
byte[] bytes = writeToBytes.toByteArray();
writeToBytes.close();
zipStream.write(bytes);
zipStream.close(); // 关闭压缩流
byteArray = baos.toByteArray();
baos.close();
inputStream = new ByteArrayInputStream(byteArray);
} catch (IOException e) {
e.printStackTrace();
}
try {
xlsName= new String(xlsName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "export";
}