aspose导出表格到word

获取数据并且画表格方法,若从前台调用,用以下方式
window.location.href=”${_baseUrl}/dd/exportDoc;

public void exportDoc(HttpServletResponse response){
        documentService.setUnlimitedLicense();
        try {
            //创建document对象
            Document document = new Document();
            DocumentBuilder builder = new DocumentBuilder(document);
            builder.moveToDocumentEnd();
            // 设置文档方向--横向
            builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
            // 这里写内容……
            builder.getParagraphFormat().setAlignment(1);
            builder.writeln("表格名称");
            builder.startTable();
            HttpSession session = request.getSession();
            String tableJson = (String) session.getAttribute("tableJson");
            session.removeAttribute("tableJson");
            String[] cellArray = {};
            String[] rowArray = {};
            String[] colArray = tableJson.split(";");    //先将字符串转换为列数组,每个元素为一行
            double fontSize = 5;
            Map<Integer,Integer> rowSpanMap = new HashMap<Integer, Integer>();
            for(int i = 0;i<colArray.length;i++){
                rowArray = colArray[i].split("\\*");       //在把列数组的元素转换为行数组,行数组每一个元素为单元格
                for(int j = 0;j<rowArray.length;j++){
                    cellArray = rowArray[j].split("&");    //每个单元格有四个属性,分别为:内容,合并列数,合并行数,颜色
                    if(i<1){       //第一行需要进行水平单元格合并
                        int colSpan = Integer.parseInt(cellArray[1]);
                        if(colSpan>1){
                            builder.insertCell();
                            builder.getCellFormat().clearFormatting();
                            //设置单元格的背景颜色或者字体颜色
                            builder.getFont().clearFormatting();
                            if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                builder.getFont().setColor(Color.RED);
                            }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                            }else{
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                            }
                            builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                            builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                            builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                            builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
                            builder.getFont().setSize(fontSize);
                            builder.write(cellArray[0].equals("no")?"":cellArray[0]);
                            for(int k = 0;k<colSpan-1;k++){
                                builder.insertCell();
                                builder.getCellFormat().clearFormatting();
                                builder.getFont().clearFormatting();
                                if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                    builder.getFont().setColor(Color.RED);
                                }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                                }else{
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                                }
                                builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                                builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                                builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                                builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                                builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
                                builder.getFont().setSize(fontSize);
                            }
                        }else{         
                            builder.insertCell();
                            builder.getCellFormat().clearFormatting();
                            builder.getFont().clearFormatting();
                            if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                builder.getFont().setColor(Color.RED);
                            }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                            }else{
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                            }
                            builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                            builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                            builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
                            builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
                            builder.getFont().setSize(fontSize);
                            builder.write(cellArray[0].equals("no")?"":cellArray[0]);
                        }
                    }else{     //后面的单元格需要进行垂直方向的合并
                        int rowSpan = Integer.parseInt(cellArray[2]);
                        if(rowSpan>1){
                            rowSpanMap.put(j, rowSpan+i);
                            builder.insertCell();
                            builder.getCellFormat().clearFormatting();
                            builder.getFont().clearFormatting();
                            if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                builder.getFont().setColor(Color.RED);
                            }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                            }else{
                                builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                            }
                            builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                            builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                            builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                            builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
                            builder.getFont().setSize(fontSize);
                            builder.write(cellArray[0].equals("no")?"":cellArray[0]);
                        }else{
                            int rowNumber = rowSpanMap.get(j)==null?0:rowSpanMap.get(j);
                            if(i<rowNumber){
                                builder.insertCell();
                                builder.getCellFormat().clearFormatting();
                                builder.getFont().clearFormatting();
                                if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                    builder.getFont().setColor(Color.RED);
                                }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                                }else{
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                                }
                                builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                                builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                                builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                                builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                                builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
                                builder.getFont().setSize(fontSize);
                            }else{
                                builder.insertCell();
                                builder.getCellFormat().clearFormatting();
                                builder.getFont().clearFormatting();
                                if("red".equals(cellArray[3])){            //样式如果是red,则修改单元格的字体为red
                                    builder.getFont().setColor(Color.RED);
                                }else if(cellArray[3].startsWith("#")){    //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
                                }else{
                                    builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                                }
                                builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);    //垂直居中对齐
                                builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);         //水平居中对齐
                                builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                                builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
                                builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
                                builder.getFont().setSize(fontSize);
                                builder.write(cellArray[0].equals("no")?"":cellArray[0]);
                            }
                        }
                    }
                }
                builder.endRow();
            }
            builder.endTable();
            builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                document.save(outputStream, SaveFormat.DOCX);

                documentOperateService.downLoadBrowser(request, response, "表格名称", outputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

导出浏览器提示,可选择目录下载

public void downLoadBrowser(HttpServletRequest request, HttpServletResponse response, String fileName, OutputStream outputStream){
        ByteArrayOutputStream byteArrayOutputStream  = null;
        OutputStream outputStreamDownload  = null;
        try {
            response.reset();
            response.resetBuffer();
            if (request.getHeader("user-agent").toLowerCase().contains("msie")||request.getHeader("user-agent").toLowerCase().contains("like gecko") ) {
                response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("GB2312"), "ISO8859-1" ) +".docx"+ "\"");
            }else{
                response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("utf-8"), "ISO8859-1" ) +".docx"+ "\"");
            }
            response.setContentType("application/octet-stream");
            response.setContentType("text/html;charset=UTF-8");
            byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
            byte[] date = byteArrayOutputStream.toByteArray();
            outputStreamDownload = response.getOutputStream();
            outputStreamDownload.write(date);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(byteArrayOutputStream != null){
                    byteArrayOutputStream.close();
                }
                if(outputStreamDownload != null){
                    outputStreamDownload.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行云的逆袭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值