Excel导出

来源:互联网 发布:社会法则 知乎 编辑:程序博客网 时间:2024/06/06 03:23

直接上代码

  • controller
import javax.servlet.ServletOutputStream;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.util.CellRangeAddress;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping(value = "${adminPath}/impexcel/basedata/UnitProductImpl")public class UnitProductImpl  extends BaseController {    @Autowired    private CdlyUnitProductBomService cdlyUnitProductBomService;@RequestMapping(value = {"unitdaochu"})    public String impleexcelChangeRecord(String str ,HttpServletRequest req, HttpServletResponse rep, Model model) throws IOException {        //List<String> list = null;        String aaa = req.getParameter("aaa");        System.out.println(aaa+"++++++++++++++++++");        //这里返回具体集合,类型为map或者具体的实体类        List<Map<String, String>> list = cdlyUnitProductBomService.unitlist(aaa);        String msg = null;        try{            Boolean bl = false;            bl = quotaExportChangeRecord(req,rep,list);//调用导出方法            msg = "导出成功!";        }catch(Exception e){            e.printStackTrace();            msg = "导出失败!";        }        req.setAttribute("MSG", msg);        return null;    }    @SuppressWarnings("deprecation")    public Boolean quotaExportChangeRecord(HttpServletRequest req, HttpServletResponse rep            ,List<Map<String, String>> list) throws IOException{        String attribute[] = new String[]{"单元列表","单元编码","名称","规格","客户规格","单位"};        //创建工作簿workbook        //利用工作簿对象创建工作表sheet        //利用工作表对象创建行row        //利用行对象创建单元格对象cell        HSSFWorkbook wb= new HSSFWorkbook(); //创建工作薄        HSSFSheet sheet=wb.createSheet(); //利用工作薄创建工作表        HSSFFont font=wb.createFont();        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗          HSSFCellStyle cellstyle=wb.createCellStyle();        cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框        cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框        cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框        cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框        cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式          HSSFCellStyle cellstyle2=wb.createCellStyle();        cellstyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框        cellstyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框        cellstyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框        cellstyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框        cellstyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式          cellstyle2.setFont(font);         //        HSSFRow row = null;        HSSFCell cell =null;        CellRangeAddress cellRangeAddress =null;// new CellRangeAddress(0, 0, 0, 11);        //CellRangeAddress:设置这个单元格的范围地址(一般在合并单元格用)        cellRangeAddress=new CellRangeAddress(0,0,0,(attribute.length-2));  //起始行号,终止行号, 起始列号,终止列号        sheet.addMergedRegion(cellRangeAddress);//工作表增加一个范围地址(合并的单元格)        for(int n=0;n<attribute.length;n++){            if(n==0){                row=sheet.createRow((short)(0));//利用工作表对象创建行(下标)                cell = row.createCell((short)0);//行对象创建第几个单元格(下标)                cell.setCellValue(attribute[0]);//单元格添加内容                cell.setCellStyle(cellstyle2);//单元格设置样式                sheet.setColumnWidth((short)0,(short)(15 * 300*(attribute.length-1)));//设置单元格列宽                row=sheet.createRow((short)(1));            }else{                cell = row.createCell((short)(n-1));                cell.setCellValue(attribute[n]);                cell.setCellStyle(cellstyle2);                sheet.setColumnWidth((short)n,(short)(15 * 300));//设置单元格列宽            }        }        for (int i = 2; i < list.size()+2; i++) {              Map<String, String> stu = list.get(i-2);            HSSFRow row1=sheet.createRow((short)i);//创建行对象            HSSFCell cell1 = row1.createCell((short)0);//利用行对象,创建列            cell1.setCellValue(stu.get("cinv_code"));//编码            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)0,(short)(15 * 300));//设置单元格列宽            cell1 = row1.createCell((short)1);            cell1.setCellValue(stu.get("cinv_name"));//名称            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)1,(short)(15 * 300));            cell1 = row1.createCell((short)2);            cell1.setCellValue(stu.get("cinv_std"));//规格            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)2,(short)(15 * 300));            cell1 = row1.createCell((short)3);            cell1.setCellValue(stu.get("cinvadd_code"));//客户规格            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)3,(short)(15 * 300));            cell1 = row1.createCell((short)4);            cell1.setCellValue(stu.get("c_com_unit_name"));//正常提前期            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)4,(short)(15 * 300));            /*            cell1 = row1.createCell((short)5);            cell1.setCellValue(stu.get());//紧急提前期            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)5,(short)(15 * 300));            cell1 = row1.createCell((short)6);            cell1.setCellValue(stu.getShelfLife());//保质期            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)6,(short)(15 * 300));            cell1 = row1.createCell((short)7);            cell1.setCellValue(stu.getMaxStockQty());//最大库存            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)7,(short)(15 * 300));            cell1 = row1.createCell((short)8);            cell1.setCellValue(stu.getMinStockQty());//最小库存            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)8,(short)(15 * 300));            cell1 = row1.createCell((short)9);            cell1.setCellValue(stu.getSafetyStockQty());//安全库存            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)9,(short)(15 * 300));            cell1 = row1.createCell((short)10);            cell1.setCellValue(stu.getRejectionRate());//废品率            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)10,(short)(15 * 300));            cell1 = row1.createCell((short)11);            cell1.setCellValue("1".equals(stu.getTheCome())?"外购":"自制");//来源            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)11,(short)(15 * 300));            cell1 = row1.createCell((short)12);            cell1.setCellValue(stu.getEconomicBatch());//经济批量            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)12,(short)(15 * 300));            cell1 = row1.createCell((short)13);            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");            cell1.setCellValue(sdf.format(stu.getCreateDate()));//创建时间            cell1.setCellStyle(cellstyle);            sheet.setColumnWidth((short)13,(short)(15 * 300));*/        }        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,          // 第六步,将文件存到指定位置          boolean t =false;        try {              ServletOutputStream servletoutputstream = rep.getOutputStream();            String fileName = "";            if(req.getHeader("user-agent").indexOf("MSIE") != -1 || req.getHeader("user-agent").indexOf("rv:11") !=-1) {                fileName = java.net.URLEncoder.encode("MRP修改记录信息列表","utf-8") + ".xls";             }else{                fileName = new String("MRP修改记录信息列表".getBytes("utf-8"),"iso-8859-1")+ ".xls";             }             rep.setHeader("Content-disposition", "attachment; filename="+ fileName);            rep.setContentType("application/vnd.ms-excel;charset=utf-8");            wb.write(servletoutputstream);             servletoutputstream.flush();            t =true;        }          catch (Exception e) {              e.printStackTrace();          }         return t;    }
  • 以上根据传值进行查找导出,可以通过js选择页面数据返回到controller来做导出操作
  • 本文章为自己工作学习用,代码直接拷贝的
原创粉丝点击