导出execl

导出Execl表格

/**
     * 导出集合方法封装
     * @param list 需要导出的数据
     * @return srModel 前台需要的uploadFile
     */
    private BatchEduceDetailPMSrModel EduceDetail(List<OasPlMainDedail> list){
        String srcPath = "D:"+FileUtil.separator;
        //ftp地址
        String DescFile = AppConfig.getProperty("Location_Default_Dir")+FileUtil.separator +"oas_dtplmain";

        BatchEduceDetailPMSrModel srModel = new BatchEduceDetailPMSrModel();
        //excel操作类
        HSSFWorkbook workbook = new HSSFWorkbook();
        //excel创建一个sheet
        HSSFSheet sheet = workbook.createSheet();
        //sheet.setDefaultColumnWidth(100);
        //sheet.addMergedRegion(new Region(0,(short)0,0,(short)3));
        HSSFCellStyle cellStyle = workbook.createCellStyle();//创建一个样式
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont cellStyleFont = workbook.createFont();//创建字体样式
        cellStyleFont.setBoldweight((short)4);//加粗
        cellStyleFont.setFontName("新宋体");
        cellStyleFont.setFontHeightInPoints((short) 10);//设置字体大小
        cellStyle.setFont(cellStyleFont);//字体样式添加到样式

        /*
        HSSFRow rHssfRow0 = sheet.createRow(0);//创建第一行
        rHssfRow0.setHeight((short)(35.7*14));//设置高度

         * 第一行跨行列格式

        sheet.addMergedRegion(new Region(0,(short)(0),0,(short)(1)));
        sheet.addMergedRegion(new Region(0,(short)(2),0,(short)(4)));
        sheet.addMergedRegion(new Region(0,(short)(5),0,(short)(6)));
        sheet.addMergedRegion(new Region(0,(short)(7),0,(short)(9)));
        sheet.addMergedRegion(new Region(0,(short)(10),0,(short)(11)));
        sheet.addMergedRegion(new Region(0,(short)(12),0,(short)(14)));
        rHssfRow0.createCell(0).setCellValue("会议计划名称");
        rHssfRow0.getCell(0).setCellStyle(cellStyle);
        rHssfRow0.createCell(5).setCellValue("会议时间");
        rHssfRow0.getCell(5).setCellStyle(cellStyle);
        rHssfRow0.createCell(10).setCellValue("创建者");
        rHssfRow0.getCell(10).setCellStyle(cellStyle);

        rHssfRow0.createCell(2).setCellValue(oasPlMain.getPlanName());
        rHssfRow0.createCell(7).setCellValue(oasPlMain.getPlanTime());
        HSSFCellStyle cellDateStyleRow1 = CellStyleUtil.createDateFormat(workbook);
        rHssfRow0.getCell(7).setCellStyle(cellDateStyleRow1); //设置日期格式
        rHssfRow0.createCell(12).setCellValue(oasPlMain.getTrueName());
        */

        HSSFRow rHssfRow = sheet.createRow(0);//创建第一行
        rHssfRow.setHeight((short)(35.7*14));//设置高度
        //rHssfRow.setRowStyle(cellStyle);//设置行样式
        rHssfRow.createCell(0).setCellValue("序号");
        //rHssfRow.getCell(0).setCellStyle(cellStyle);//引用列样式
        sheet.setColumnWidth(0,(short)(2000));//宽度

        rHssfRow.createCell(1).setCellValue("分类");
        //rHssfRow.getCell(1).setCellStyle(cellStyle);//引用列样式
        sheet.setColumnWidth(1,(short)(3000));//宽度

        rHssfRow.createCell(2).setCellValue("项目名称");
        //rHssfRow.getCell(2).setCellStyle(cellStyle);//引用列样式
        sheet.setColumnWidth(2,(short)(6500));//宽度

        rHssfRow.createCell(3).setCellValue("入住日期");
        //rHssfRow.getCell(3).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(3,(short)(6400));//宽度

        rHssfRow.createCell(4).setCellValue("预备会时间");
        //rHssfRow.getCell(4).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(4,(short)(6400));//宽度

        rHssfRow.createCell(5).setCellValue("住宿人数");
        //rHssfRow.getCell(5).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(5,(short)(4000));//宽度

        rHssfRow.createCell(6).setCellValue("第一步开标时间");
        //rHssfRow.getCell(6).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(6,(short)(6400));//宽度

        rHssfRow.createCell(7).setCellValue("第一步开标人数");
        //rHssfRow.getCell(7).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(7,(short)(5300));//宽度

        rHssfRow.createCell(8).setCellValue("第二步开标时间");
        //rHssfRow.getCell(8).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(8,(short)(6400));//宽度

        rHssfRow.createCell(9).setCellValue("第二步开标人数");
        //rHssfRow.getCell(9).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(9,(short)(5300));//宽度

        rHssfRow.createCell(10).setCellValue("评标会议室需求");
        //rHssfRow.getCell(10).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(10,(short)(5500));//宽度

        rHssfRow.createCell(11).setCellValue("会议室落实情况");
        //rHssfRow.getCell(11).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(11,(short)(5400));//宽度

        rHssfRow.createCell(12).setCellValue("离店日期");
        //rHssfRow.getCell(12).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(12,(short)(6400));//宽度

        rHssfRow.createCell(13).setCellValue("项目经理");
        //rHssfRow.getCell(13).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(13,(short)(4500));//宽度

        rHssfRow.createCell(14).setCellValue("开标地点");
        //rHssfRow.getCell(14).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(14,(short)(10000));//宽度

        rHssfRow.createCell(15).setCellValue("评标地点");
        //rHssfRow.getCell(15).setCellStyle(cellStyle);//列样式
        sheet.setColumnWidth(15,(short)(10000));//宽度

        rHssfRow.createCell(16).setCellValue("会务负责人");
        sheet.setColumnWidth(16,(short)(4500));//宽度
        CellStyle cell=workbook.createCellStyle();
        HSSFFont font = workbook.createFont();//创建字体样式
        font.setBoldweight((short)4);//加粗
        font.setFontName("新宋体");
        font.setFontHeightInPoints((short) 10);//设置字体大小
        cell.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cell.setFont(font);
        cell.setWrapText(true);
        rHssfRow.getCell(16).setCellStyle(cell);//列样式

        for (int i = 0; i <= 16; i++) {
            rHssfRow.getCell(i).setCellStyle(CellStyleUtil.createTopBorder(workbook)); //第一行 每一列设置边框
        }

        //创建行id 从1开始
        int i = 1;
        //内容写入excel
        HSSFRow row = null;
        HSSFCellStyle cellDateStyle = CellStyleUtil.createDateFormat(workbook);
        for(OasPlMainDedail oas : list){
            //创建行
            row = sheet.createRow(i);
            //写入内容
            row.createCell(0).setCellValue(i);
            row.getCell(0).setCellStyle(cellStyle);

            row.createCell(1).setCellValue(dao.getPlanType(oas.getPlanType()));

            row.createCell(2).setCellValue(oas.getBidName());

            if(null==oas.getCheckIn()){
                row.createCell(3).setCellValue("");
                row.getCell(3).setCellStyle(cellDateStyle); //设置日期格式
            }else{
                row.createCell(3).setCellValue(oas.getCheckIn());
                row.getCell(3).setCellStyle(cellDateStyle); //设置日期格式
            }

            if(null==oas.getReadinessTime()){
                row.createCell(4).setCellValue("");
                row.getCell(4).setCellStyle(cellDateStyle);
            }else{
                row.createCell(4).setCellValue(oas.getReadinessTime());
                row.getCell(4).setCellStyle(cellDateStyle);
            }

            row.createCell(5).setCellValue(oas.getStayPopulation());

            if(null==oas.getFirstBidOpeningDate()){
                row.createCell(6).setCellValue("");
                row.getCell(6).setCellStyle(cellDateStyle);
            }else{
                row.createCell(6).setCellValue(oas.getFirstBidOpeningDate());
                row.getCell(6).setCellStyle(cellDateStyle);
            }

            row.createCell(7).setCellValue(oas.getFirstBidOpeningPopulation());

            if(null==oas.getFirstBidOpeningDate()){
                row.createCell(8).setCellValue("");
                row.getCell(8).setCellStyle(cellDateStyle);
            }else{
                row.createCell(8).setCellValue(oas.getFirstBidOpeningDate());
                row.getCell(8).setCellStyle(cellDateStyle);
            }

            row.createCell(9).setCellValue(oas.getSecondBidOpeningPopulation());

            row.createCell(10).setCellValue(oas.getEvaluationOfBidNeed());

            row.createCell(11).setCellValue(oas.getEvaluationOfBidEndInfo());

            if(null==oas.getDepartureDate()){
                row.createCell(12).setCellValue("");
                row.getCell(12).setCellStyle(cellDateStyle);
            }else{
                row.createCell(12).setCellValue(oas.getDepartureDate());
                row.getCell(12).setCellStyle(cellDateStyle);
            }

            row.createCell(13).setCellValue(oas.getProjectManager());

            row.createCell(14).setCellValue(oas.getOpenAddress());

            row.createCell(15).setCellValue(oas.getEvaAddress());

            row.createCell(16).setCellValue(oas.getOptUserId());

            for (int j = 0; j <= 16; j++) {
                if(j!=12&&j!=8&&j!=6&&j!=3&&j!=4)
                    row.getCell(j).setCellStyle(CellStyleUtil.createBodyBorder(workbook)); //给非日期列设置边框
            }
            i++;
        }

        java.io.FileOutputStream _fos = null;
        try{
            //初始路径D:/flowDedail.xls
            srcPath = srcPath+"flowDedail.xls";
            //输出流  设置输出位置
            _fos  = new java.io.FileOutputStream(srcPath);
            //创建的HSSFWorkbook对象写入输出流
            workbook.write(_fos);
            //创建文件
            File file=new File(srcPath);
            //设置id  在前台下载
            srModel.setUpLoadFileId(FileUtil.uploadFile(file.getAbsolutePath(),DescFile));
            logger.info("文件id:\t"+srModel.getUpLoadFileId());
        } catch (Exception e) {
            logger.info("导出失败..");
            e.printStackTrace();
        } finally {
            try {
                _fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return srModel;
    }
时间: 2024-08-01 00:56:43

导出execl的相关文章

在MySQL query browser中要导出execl表单

导出:  在MySQL query browser中要导出execl表单,只需要点击file,选择export execl就可以了(要先进入表单哦!). 导入: 数据库的数据太多一下导入有点麻烦,如何将excel的数据直接导入数据库呢? 第一步 另存为txt文档,名字最好为数据库的table名.  在这我们要保存问文本文件用制表符分割的那个,这样我们的数据就不会乱. 第二步 打开txt文档,点击另存为,修改编码,解决中文utf8.  这个时候的文件名必须和你要导入的table名称一样,否则在lo

js导出execl兼容ie Chrome Firefox各种主流浏览器(js export execl)

第一种导出table布局的表格 <html> <head> <meta charset="utf-8"> <script type="text/javascript" language="javascript"> var idTmr; function getExplorer() { var explorer = window.navigator.userAgent; //ie if(explore

asp.net中导出Execl的方法

一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给浏览器.在Response输出时,\t分隔的数据,导出 execl时,等价于分列,\n等价于换行. 1.将整个html全部输出execl 此法将html中所有的内容,如按钮,表格,图片等全部输出到Execl中.   Response.Clear();       Response.Buffer=  

mvc 导出execl

mvc导出execl 必须是get方法才可以  也不能使用ajax function ComplaintsExport() { var serial = $("form").serialize(); window.location.href = "/BidComplaints/Complaints/ComplaintsExport?" + serial; }; [HttpGet] [Description("导出")] public FileRe

Asp.net中利用NPOI组件快速导入导出Execl数据

相信很多童鞋都开发过Execl的导入导出功能,最近产品中无论是后台数据分析的需要,还是前端满足用户管理的方便,都有Execl导入导出的维护需求产生. 以前做这个功能,如果是web,利用HttpContext.Current.Response.ContentType ="application/ms-excel";就可以导出html数据表格到execl中,这种方法的问题就是编码格式的兼容性太差,用Mac OS之类的 office打开直接乱码给你看.或者是调用office的COM组件,或宏

C#将DataTable导出Execl、Word、Xml

    /// <summary>     /// 将DT转换为Execl的方法     /// </summary>     /// <param name="dt">需要导出的DT     /// <param name="page">页面     /// <param name="fileName">文件名     public void ToExecl(DataTable dt,

不使用第三方组件,只调用COM,导出EXECL,但只装OFFICE2007,不装2003,直接强制引用动态库的方法

步骤1:强制引用动态库”Microsoft.Office.Interop.Excel“,版本为11.0: 步骤2:添加引用OFFICE2007的COM组件”MicroSoft Office 12.0 Object Library“(OFFICE2003的是MicroSoft Office 11.0 Object Library),添加完成后动态库为”microsoft.office.core“. 不使用第三方组件,只调用COM,导出EXECL,但只装OFFICE2007,不装2003,直接强制引

NPIO 导出Execl

步骤1:导入NOIO.dll    (我导入压缩包中的4.0) 下载地址:http://npoi.codeplex.com/downloads/get/1572743 步骤二:粘贴代码(^  ....   ^) public class ExeclController : Controller    {        //        // GET: /Execl/        public ActionResult Index()        {            DataTable

MVC 导出Execl 的总结几种方式 (二)

接着上面的来,继续导出Execl 的功能 使用FileResult 方式直接可以生产Execl ,这样我们将会写大量处理后台的代码,个人感觉不好,只是展示出来,提供参考 第一步:编辑控制器 public FileResult ExportFile() { var list = GetList(); var sbHtml = new StringBuilder(); string title = "order"; //标题 sbHtml.Append("<table bo