导出excel

package com.dfjd.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
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.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.struts2.ServletActionContext;

import com.dfjd.bean.Appearance;
import com.dfjd.bean.Lectotype;

public class ExportLectotypeAppearanceExcel {
	
	public String ExportAppearance(Lectotype lectotype, Appearance appearance, String imgurl) throws Exception{
		/**
		 * 以下为生成Excel操作
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(lectotype.getLDate());
		
		// 1.创建一个workbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 2.在workbook中添加一个sheet,对应Excel中的一个sheet
		HSSFSheet sheet = wb.createSheet("东风机电-选型计算表");
		//设置打印属性
		HSSFPrintSetup print = (HSSFPrintSetup) sheet.getPrintSetup(); 
		print.setScale((short) 80);//设置打印缩放72% 
		sheet.setMargin(HSSFSheet.BottomMargin, (double)0.8); //页边距(下)
        sheet.setMargin(HSSFSheet.TopMargin, (double)0.9); //页边距(上)   
		sheet.setHorizontallyCenter(true); //设置打印页面为水平居中
        sheet.setVerticallyCenter(true); //设置打印页面为垂直居中
        //logo图片写入
       
	    String realPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/"); 
        
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(realPath+"/"+imgurl)));
	    ImageIO.write(bufferImg, "JPEG", byteArrayOut);
	    HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
	    HSSFClientAnchor anchor = new HSSFClientAnchor(300, 5, 0, 0,(short) 0, 13, (short)8, 14);
	    patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
	    
	    
	    ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream();
        BufferedImage bufferImg2 = ImageIO.read(new FileInputStream(new File(realPath+"/images/excellogo.png")));
	    ImageIO.write(bufferImg2, "png", byteArrayOut2);
	    HSSFPatriarch patriarch2 = (HSSFPatriarch) sheet.createDrawingPatriarch();
	    HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 0, 0,(short) 1, 0, (short)3, 1);
	    patriarch2.createPicture(anchor2, wb.addPicture(byteArrayOut2.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
	    
	    
		/*sheet.getPrintSetup().setPaperSize(
			      XSSFPrintSetup.A4_PAPERSIZE);*/
		//设置列宽  第x列   宽度xxxx
		sheet.setColumnWidth(0, 3100); 
		sheet.setColumnWidth(1, 2900); 
		sheet.setColumnWidth(2, 2900); 
		sheet.setColumnWidth(3, 2900); 
		sheet.setColumnWidth(4, 3600); 
		sheet.setColumnWidth(5, 2900); 
		sheet.setColumnWidth(6, 2900); 
		sheet.setColumnWidth(7, 2900);
		sheet.setColumnWidth(8, 2900);
		HSSFPrintSetup printSetup = sheet.getPrintSetup();
	    printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); // 纸张
		
		/**
		 * 1 首行样式设置
		 */
		HSSFCellStyle cellStyle1 = wb.createCellStyle();
		
		//首行   新建font实体
		HSSFFont hssfFont1 = wb.createFont();
        //首行   字体和大小  
		hssfFont1.setFontHeightInPoints((short)20);
		hssfFont1.setFontName("宋体");
        //首行   粗体
		hssfFont1.setBoldweight(Font.BOLDWEIGHT_BOLD);
        //首行   将字体样式加入设置
		cellStyle1.setFont(hssfFont1);
        //首行   文字水平  垂直 居中
		cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		
		//边框样式
		cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        
        /**
         * 2 字体为11号 宋体 加粗 全实线的样式设置
         */
        HSSFCellStyle cellStyle2 = wb.createCellStyle();
        HSSFFont hssfFont2 = wb.createFont();
        hssfFont2.setFontHeightInPoints((short)11);
        hssfFont2.setFontName("宋体");
        hssfFont2.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle2.setFont(hssfFont2);
        cellStyle2.setWrapText(true);
        cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        
        //边框样式
        cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
      
        /**
         * 3 字体为11号 宋体 细 全实线 的样式设置
         */
        HSSFCellStyle cellStyle3 = wb.createCellStyle();
        HSSFFont hssfFont3 = wb.createFont();
        hssfFont3.setFontHeightInPoints((short)11);
        hssfFont3.setFontName("宋体");
        cellStyle3.setFont(hssfFont3);
        cellStyle3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        
        //边框样式
        cellStyle3.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		
		 /**
         * 4 字体为 全实线 的边框样式设置
         */
        HSSFCellStyle cellStyle4 = wb.createCellStyle();
      
        cellStyle4.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle4.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle4.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        
		
		 /**
         * 5 字体为 右边全实线 的边框样式设置
         */
        HSSFCellStyle cellStyle5 = wb.createCellStyle();
     
		cellStyle5.setBorderRight(HSSFCellStyle.BORDER_THIN);
		
		/**
         * 6 字体为 左边全实线 的边框样式设置
         */
        HSSFCellStyle cellStyle6 = wb.createCellStyle();
     
		cellStyle6.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	
        
		//新建行对象
		HSSFRow row = null;
		List<HSSFRow> rowlist = new ArrayList<HSSFRow>();
		
		// 循环生成40行
		for (int i = 0; i < 15; i++) {
			row = sheet.createRow(i);
			rowlist.add(row);
		}
		//设置2到最后行行高
		for (int i = 1; i < rowlist.size(); i++) {
			rowlist.get(i).setHeight((short) 500);
		}
		
		System.out.println(rowlist.size());

		// 第一行数据
		for (int i = 0; i < 9; i++) {
			rowlist.get(0).createCell(i).setCellStyle(cellStyle4);
		}
		HSSFCell cell1 = rowlist.get(0).createCell(0);// 获取第一行第一列的单元格---
		// 合并前两行以及前5列
		CellRangeAddress cra1 = new CellRangeAddress(0, 0, 0, 8);
		//合并单元格边框设置
		sheet.addMergedRegion(cra1);
		cell1.setCellValue("安装尺寸图");// 写入首行内容
		
		//加载首行样式
		cell1.setCellStyle(cellStyle1);
		//设置首行 行高
		rowlist.get(0).setHeight((short) 1000);
		
		
		// 第二行数据
		HSSFCell cell2 = rowlist.get(1).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell2.setCellValue("企业名称");
		// 合并前两行以及前5列
		CellRangeAddress cra2 = new CellRangeAddress(1, 1, 1, 8);
		//合并单元格边框设置
		sheet.addMergedRegion(cra2);
		
		//加载第二行样式
		cell2.setCellStyle(cellStyle2); 
		//加入样式 合并单元格的样式
		for (int i = 1; i < 9; i++) {
			rowlist.get(1).createCell(i).setCellStyle(cellStyle3);
		}
		//获得被合并的单元格
		HSSFCell cellh1 = rowlist.get(1).createCell(1);
		cellh1.setCellValue(lectotype.getLQymc());
		cellh1.setCellStyle(cellStyle3);
		
		// 第三行数据
		HSSFCell cell3 = rowlist.get(2).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell3.setCellValue("项目名称");
		// 合并前两行以及前5列
		CellRangeAddress cra3 = new CellRangeAddress(2, 2, 1, 8);
		//合并单元格边框设置
		sheet.addMergedRegion(cra3);
		
		//加载第三行样式
		cell3.setCellStyle(cellStyle2); 
		//加入样式 合并单元格的样式
		for (int i = 1; i < 9; i++) {
			rowlist.get(2).createCell(i).setCellStyle(cellStyle3);
		}
		//获得被合并的单元格
		HSSFCell cellh2 = rowlist.get(2).createCell(1);
		cellh2.setCellValue(lectotype.getLXmmc());
		cellh2.setCellStyle(cellStyle3);
		
		
		// 第四行数据
		HSSFCell cell4 = rowlist.get(3).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell4.setCellValue("用途名称");
		// 合并前两行以及前5列
		CellRangeAddress cra4 = new CellRangeAddress(3, 3, 1, 8);
		//合并单元格边框设置
		sheet.addMergedRegion(cra4);
		
		//加载第三行样式
		cell4.setCellStyle(cellStyle2); 
		//加入样式 合并单元格的样式
		for (int i = 1; i < 9; i++) {
			rowlist.get(3).createCell(i).setCellStyle(cellStyle3);
		}
		//获得被合并的单元格
		HSSFCell cellh3 = rowlist.get(3).createCell(1);
		cellh3.setCellValue(lectotype.getLYt());
		cellh3.setCellStyle(cellStyle3);
		
		
		// 第五行数据
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(4).createCell(i).setCellStyle(cellStyle4);
		}
		HSSFCell cell5 = rowlist.get(4).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell5.setCellValue("介质名称");
		
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 1, 3));// 合并第二行的千两列
		HSSFCell cell6 = rowlist.get(4).createCell(1);// 创建序号为1的单元格,第二行第1格
		cell6.setCellValue(lectotype.getLJzmc());
		
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 4, 5));// 合并第二行的千两列
		HSSFCell cell7 = rowlist.get(4).createCell(4);// 创建序号为1的单元格,第二行第1格
		cell7.setCellValue("状态");
		
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 6, 8));// 合并第二行的千两列
		HSSFCell cell8 = rowlist.get(4).createCell(6);// 创建序号为1的单元格,第二行第1格
		cell8.setCellValue(lectotype.getLJzzt());
		//写入样式
		cell5.setCellStyle(cellStyle2);
		cell6.setCellStyle(cellStyle3);
		cell7.setCellStyle(cellStyle2);
		cell8.setCellStyle(cellStyle3);
		
		
		// 第六行数据
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(5).createCell(i).setCellStyle(cellStyle4);
		}
		HSSFCell cell9 = rowlist.get(5).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell9.setCellValue("位号");
		
		sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 3));// 合并第二行的千两列
		HSSFCell cell10 = rowlist.get(5).createCell(1);// 创建序号为1的单元格,第二行第1格
		cell10.setCellValue(lectotype.getLWh());
		
		HSSFCell cell11 = rowlist.get(5).createCell(4);// 创建序号为1的单元格,第二行第1格
		cell11.setCellValue("台次/总台数");
		HSSFCell cell12 = rowlist.get(5).createCell(5);// 创建序号为1的单元格,第二行第1格
		cell12.setCellValue(lectotype.getLZts());
		HSSFCell cell13 = rowlist.get(5).createCell(6);// 创建序号为1的单元格,第二行第1格
		cell13.setCellValue("重量(kg)");
		sheet.addMergedRegion(new CellRangeAddress(5, 5, 7, 8));// 合并第二行的千两列
		HSSFCell cell14 = rowlist.get(5).createCell(7);// 创建序号为1的单元格,第二行第1格
		
		//判断温度的对特殊型号重量的影响
		String LCgqxh = lectotype.getLCgqxh();
		double wd = lectotype.getLCzwd();
		if(LCgqxh.equals("N50")|| LCgqxh.equals("C75")|| LCgqxh.equals("P50")){
			if(wd>=200){
				cell14.setCellValue(appearance.getAGwzl());
			}else{
				cell14.setCellValue(appearance.getADwzl());
			}
		}else{
			cell14.setCellValue(appearance.getADwzl());
		}
		
		//写入样式
		cell9.setCellStyle(cellStyle2);
		cell10.setCellStyle(cellStyle3);
		cell11.setCellStyle(cellStyle2);
		cell12.setCellStyle(cellStyle3);
		cell13.setCellStyle(cellStyle2);
		cell14.setCellStyle(cellStyle3);
		
		// 第七行数据
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(6).createCell(i).setCellStyle(cellStyle4);
		}
		
		HSSFCell cell15 = rowlist.get(6).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell15.setCellValue("传感器型号");
		HSSFCell cell16 = rowlist.get(6).createCell(1);// 创建序号为1的单元格,第二行第1格
		cell16.setCellValue("安装方式");
		
		sheet.addMergedRegion(new CellRangeAddress(6, 6, 2, 5));// 合并第二行的千两列
		HSSFCell cell17 = rowlist.get(6).createCell(2);// 创建序号为1的单元格,第二行第1格
		cell17.setCellValue("法兰标准");
		
		sheet.addMergedRegion(new CellRangeAddress(6, 6, 6, 8));// 合并第二行的千两列
		HSSFCell cell18 = rowlist.get(6).createCell(6);// 创建序号为1的单元格,第二行第1格
		cell18.setCellValue("法兰尺寸DN(标配)");
		
		//写入样式
		cell15.setCellStyle(cellStyle2);
		cell16.setCellStyle(cellStyle2);
		cell17.setCellStyle(cellStyle2);
		cell18.setCellStyle(cellStyle2);
		
		
		
		// 第八行数据
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(7).createCell(i).setCellStyle(cellStyle4);
		}
		
		HSSFCell cell21 = rowlist.get(7).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell21.setCellValue(appearance.getACgqxh());
		HSSFCell cell22 = rowlist.get(7).createCell(1);// 创建序号为1的单元格,第二行第1格
		cell22.setCellValue(appearance.getAAzfs());
		
		sheet.addMergedRegion(new CellRangeAddress(7, 7, 2, 5));// 合并第二行的千两列
		HSSFCell cell23 = rowlist.get(7).createCell(2);// 创建序号为1的单元格,第二行第1格
		cell23.setCellValue(appearance.getAFlbz());
		
		sheet.addMergedRegion(new CellRangeAddress(7, 7, 6, 8));// 合并第二行的千两列
		HSSFCell cell24 = rowlist.get(7).createCell(6);// 创建序号为1的单元格,第二行第1格
		cell24.setCellValue(appearance.getADn());
		
		
		//加入样式
		cell21.setCellStyle(cellStyle3);
		cell22.setCellStyle(cellStyle3);
		cell23.setCellStyle(cellStyle3);
		cell24.setCellStyle(cellStyle3);
		
		
		
		// 第九行数据
		HSSFCell cell19 = rowlist.get(8).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell19.setCellValue("A");
		HSSFCell cell20 = rowlist.get(8).createCell(1);// 创建序号为1的单元格,第二行第1格
		cell20.setCellValue("B");
		HSSFCell cell27 = rowlist.get(8).createCell(2);
		cell27.setCellValue("C");
		HSSFCell cell28 = rowlist.get(8).createCell(3);
		cell28.setCellValue("D");
		HSSFCell cell29 = rowlist.get(8).createCell(4);
		cell29.setCellValue("E");
		HSSFCell cell30 = rowlist.get(8).createCell(5);
		cell30.setCellValue("F");
		HSSFCell cell31 = rowlist.get(8).createCell(6);
		cell31.setCellValue("H");
		HSSFCell cell32 = rowlist.get(8).createCell(7);
		cell32.setCellValue("I");
		HSSFCell cell33 = rowlist.get(8).createCell(8);
		cell33.setCellValue("L");
		
		//加入样式
		cell19.setCellStyle(cellStyle2);
		cell20.setCellStyle(cellStyle2);
		cell27.setCellStyle(cellStyle2);
		cell28.setCellStyle(cellStyle2);
		cell29.setCellStyle(cellStyle2);
		cell30.setCellStyle(cellStyle2);
		cell31.setCellStyle(cellStyle2);
		cell32.setCellStyle(cellStyle2);
		cell33.setCellStyle(cellStyle2);
		
		
		// 第十行数据
		HSSFCell cell25 = rowlist.get(9).createCell(0);// 创建序号为1的单元格,第二行第1格
		cell25.setCellValue(appearance.getAAcs());
		HSSFCell cell26 = rowlist.get(9).createCell(1);
		cell26.setCellValue(appearance.getABcs());
		HSSFCell cell36 = rowlist.get(9).createCell(2);
		cell36.setCellValue(appearance.getACcs());
		HSSFCell cell37 = rowlist.get(9).createCell(3);
		cell37.setCellValue(appearance.getADcs());
		HSSFCell cell38 = rowlist.get(9).createCell(4);
		cell38.setCellValue(appearance.getAEcs());
		HSSFCell cell39 = rowlist.get(9).createCell(5);
		cell39.setCellValue(appearance.getAFcs());
		HSSFCell cell40 = rowlist.get(9).createCell(6);
		cell40.setCellValue(appearance.getAHcs());
		HSSFCell cell41 = rowlist.get(9).createCell(7);
		cell41.setCellValue(appearance.getAIcs());
		HSSFCell cell42 = rowlist.get(9).createCell(8);
		cell42.setCellValue(appearance.getALcs());
		
		
		//加入样式
		cell25.setCellStyle(cellStyle3);
		cell26.setCellStyle(cellStyle3);
		cell36.setCellStyle(cellStyle3);
		cell37.setCellStyle(cellStyle3);
		cell38.setCellStyle(cellStyle3);
		cell39.setCellStyle(cellStyle3);
		cell40.setCellStyle(cellStyle3);
		cell41.setCellStyle(cellStyle3);
		cell42.setCellStyle(cellStyle3);
		
		
		
		//第十一行数据
		
		HSSFCell cell34 = rowlist.get(10).createCell(0);
		cell34.setCellValue("M");
		HSSFCell cell35 = rowlist.get(10).createCell(1);
		cell35.setCellValue("N");
		HSSFCell cell45 = rowlist.get(10).createCell(2);
		cell45.setCellValue("K");
		HSSFCell cell46 = rowlist.get(10).createCell(3);
		cell46.setCellValue("Q");
		HSSFCell cell47 = rowlist.get(10).createCell(4);
		cell47.setCellValue("S");
		HSSFCell cell48 = rowlist.get(10).createCell(5);
		cell48.setCellValue("T");
		HSSFCell cell49 = rowlist.get(10).createCell(8);
		cell49.setCellValue("");
		//加入样式
		cell34.setCellStyle(cellStyle2);
		cell35.setCellStyle(cellStyle2);
		cell45.setCellStyle(cellStyle2);
		cell46.setCellStyle(cellStyle2);
		cell47.setCellStyle(cellStyle2);
		cell48.setCellStyle(cellStyle2);
		cell49.setCellStyle(cellStyle5);
		//第十二行数据
		
		HSSFCell cell50 = rowlist.get(11).createCell(0);
		cell50.setCellValue(appearance.getAMcs());
		HSSFCell cell51 = rowlist.get(11).createCell(1);
		cell51.setCellValue(appearance.getANcs());
		HSSFCell cell52 = rowlist.get(11).createCell(2);
		cell52.setCellValue(appearance.getAKcs());
		HSSFCell cell53 = rowlist.get(11).createCell(3);
		cell53.setCellValue(appearance.getAQcs());
		HSSFCell cell531 = rowlist.get(11).createCell(4);
		cell531.setCellValue(appearance.getAScs());
		HSSFCell cell532 = rowlist.get(11).createCell(5);
		cell532.setCellValue(appearance.getATcs());
		HSSFCell cell54 = rowlist.get(11).createCell(8);
		cell54.setCellValue("");
		
		//加入样式
		cell50.setCellStyle(cellStyle3);
		cell51.setCellStyle(cellStyle3);
		cell52.setCellStyle(cellStyle3);
		cell53.setCellStyle(cellStyle3);
		cell531.setCellStyle(cellStyle3);
		cell532.setCellStyle(cellStyle3);
		cell54.setCellStyle(cellStyle5);
		
		//第十三行为空数据
		HSSFCell cell100 = rowlist.get(12).createCell(0);
		cell100.setCellStyle(cellStyle6);
		HSSFCell cell55 = rowlist.get(12).createCell(8);
		cell55.setCellStyle(cellStyle5);
		
		//第十四行数据   待写入图片
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(13).createCell(i).setCellStyle(cellStyle4);
		}
		//设置行高
		rowlist.get(13).setHeight((short) 30000);
		
		sheet.addMergedRegion(new CellRangeAddress(13, 13, 0, 8));
		HSSFCell cell56 = rowlist.get(13).createCell(0);
		cell56.setCellValue("");
		cell56.setCellStyle(cellStyle4);
		
		//第十五行数据  
		//加入边框样式
		for (int i = 0; i < 9; i++) {
			rowlist.get(14).createCell(i).setCellStyle(cellStyle4);
		}
		
		sheet.addMergedRegion(new CellRangeAddress(14, 14, 0, 8));
		HSSFCell cell57 = rowlist.get(14).createCell(0);
		cell57.setCellStyle(cellStyle4);
		
		
		// excel名加上随机数,防止名称重复
		//int code = (int) (Math.random() * 9000 + 1000);
		String excel = "F:/" + date +"-"+ lectotype.getLQymc() +"-"+ lectotype.getLJzmc() + "外型尺寸表.xls";
		FileOutputStream out = new FileOutputStream(excel);
		wb.write(out);
		out.flush();
		out.close();
		//返回导出计算书的名称
		return date +"-"+ lectotype.getLQymc() +"-"+ lectotype.getLJzmc() + "外型尺寸表.xls" ;

	}
}

package com.dfjd.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

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

public class Download {
	  //下载模版工具类  
    public void downloadFile(HttpServletRequest request,    
        HttpServletResponse response,String fileName) throws Exception {    
        response.setContentType("text/html;charset=UTF-8");     
        BufferedInputStream in = null;    
        BufferedOutputStream out = null;    
        request.setCharacterEncoding("UTF-8");    
        try {    
            File f = new File("F:/" + fileName);    
            response.setContentType("application/x-excel");    
            response.setCharacterEncoding("UTF-8");    
            response.setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("gbk"),"iso-8859-1"));    
            response.setHeader("Content-Length",String.valueOf(f.length()));    
            in = new BufferedInputStream(new FileInputStream(f));    
            out = new BufferedOutputStream(response.getOutputStream());    
            byte[] data = new byte[1024];    
            int len = 0;    
            while (-1 != (len=in.read(data, 0, data.length))) {    
                out.write(data, 0, len);    
            }    
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {    
            if (in != null) {    
                in.close();    
            }    
            if (out != null) {    
                out.close();    
            }    
        }  
    }
}
//导出某条选型记录的选型计算书
	public void exportLectotype() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		
		String LId = request.getParameter("LId");
		//调用查询指定选型记录id的方法
		List<Object> msglist = lectotypeBIZ.LectotypeDetilsService(LId,null);
		//将查到的结果传至导出计算书的方法中
		ExportLectotypeRecordExcel exportlectotype = new ExportLectotypeRecordExcel();
		String addr = exportlectotype.ExportLectotype((Lectotype)msglist.get(0), (Calculate)msglist.get(1));
		
		Download download = new Download();
		download.downloadFile(request, response, addr);
	}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值