怎么样用java代码去实现文件导出到Excel

java文件导出Excel:

1. 声明一个类 编写导出样式


    public class ExcelExportUtils {
         public static synchronized void ExportExcel(String sheetName,String[] headers,List<Object[]> data,OutputStream out){  
                //声明一个工作簿  
                HSSFWorkbook workbook = new HSSFWorkbook();  
                //生成一个表格  
                HSSFSheet sheet = workbook.createSheet(sheetName);  
                //设置表格默认列宽度为15个字符  
                sheet.setDefaultColumnWidth(20);  
                //生成一个样式,用来设置标题样式  
                HSSFCellStyle style = workbook.createCellStyle();  
                //设置这些样式  
                style.setBorderBottom(BorderStyle.THIN);  
                style.setBorderLeft(BorderStyle.THIN);  
                style.setBorderRight(BorderStyle.THIN);  
                style.setBorderTop(BorderStyle.THIN);  
                style.setAlignment(HorizontalAlignment.CENTER_SELECTION);  
                style.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex());
                style.setFillPattern(FillPatternType.ALT_BARS);
                //生成一个字体  
                HSSFFont font = workbook.createFont();  
                font.setColor(HSSFColorPredefined.BLACK.getIndex());  
                font.setBold(true);
                font.setFontHeight((short)300);
                //把字体应用到当前的样式  
                style.setFont(font);  

                HSSFCellStyle style2 = workbook.createCellStyle();  
                style2.setVerticalAlignment(VerticalAlignment.CENTER);  
                style2.setAlignment(HorizontalAlignment.CENTER);

                //产生表格标题行  
                HSSFRow row = sheet.createRow(0);  
                row.setHeight((short)400);
                for(int i = 0; i<headers.length;i++){
                    HSSFCell cell = row.createCell(i);  
                    cell.setCellStyle(style);  
                    HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
                    cell.setCellValue(text);  
                }  
                for (int i=0;i<data.size();i++) {  
                   row = sheet.createRow(i+1);  
                   for(int j = 0 ; j< data.get(i).length; j ++) {
                       HSSFCell cell =row.createCell(j);
                       cell.setCellStyle(style2);
                       cell.setCellValue(String.valueOf(data.get(i)[j]));

                   }
                }  
                try {  
                    workbook.write(out);  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  

         public static void main(String[] args) throws FileNotFoundException {
            OutputStream  ops= new FileOutputStream("E:/aaa.xls");
            String[] heades= {"c","a","b","gg",};
            List<Object[]> list = new ArrayList<>();
            String [] arr = {"1","2","3","3"};
            list.add(arr);
            ExportExcel("测试",heades,list,ops);

        }

    }

2. 在controller层编写实现代码

    @RequestMapping("exportExcel")
        @ResponseBody
        public void ExportExcel(String plantCod, String time, HttpServletResponse res) {
            res.setContentType("octets/stream");
            String excelName = "测试导出功能";
            OutputStream out = null;
            try {
                res.addHeader("Content-Disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
                out = res.getOutputStream();
                String[] heades = { ""};
                List<Map<String, Object>> listMap = new ArrayList<>();
                List<Object[]> list = new ArrayList<>();
                listMap = energyReportService.queryEnergyReport(plantCod,time);
                for(int i = 0; i<listMap.size(); i++){

                    for(Map map:listMap){
                        Object [] obj = {map.get("")};
                        list.add(obj);
                    }
                    ExcelExportUtils.ExportExcel("", heades, list, out);
                    out.flush();
                }
                ExcelExportUtils.ExportExcel("aaa", heades, list, out);
                out.flush();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

3. 在jsp页面添加点击事件

        function exportExcel(){     
            var cod =  $('#input-plants').find("option:selected").attr('data-id');
            var time = $('#energyDate').datetimepicker().data().date;  
            window.location.href = getBasePath()+"/energyreport/exportExcel?      
        heatId=${heatId}&plantCod="+cod+"&time="+time;     
        }
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值