weixin_40478037 2018-04-19 02:55 采纳率: 100%
浏览 921
已采纳

java如何识别excle合并的单元格

java如何识别excle合并的单元格,如何实现word的自动换行

  • 写回答

2条回答 默认 最新

  • happyKite777 2018-04-19 03:04
    关注

    利用POI处理,自动换行的话,如果是表格外面直接'\n'就行,如果是表格里面,必须用(char)11,下面是一段读取Excel生成word的代码,你可以参考下
    读取Excel
    public ArrayList

    readExcel() throws IOException{
    File excelFile = new File(excelPath);
    InputStream is = new FileInputStream(excelFile);
        ArrayList<Table> list = null;
    
        String suffix = excelPath.substring(excelPath.lastIndexOf(".")+1);
        if(suffix.equals("xls")){
            HSSFWorkbook workbook = null;
            workbook = new HSSFWorkbook(is);
    
            if(workbook != null){  
    
                HSSFSheet sheet = workbook.getSheetAt(1);//获取第二张表
                if(sheet == null){
                    System.out.println();
                    return null;
                }
                list = new ArrayList<Table>(); 
    
                int combineLines = 1;
                int number = 1;
                //读取行
                for (int rowNum = 3; rowNum < sheet.getLastRowNum(); rowNum+=combineLines) {
                    HSSFRow row = sheet.getRow(rowNum);
    
                    if (row == null) continue;
                    Table tb = new Table(); 
                    tb.setNumber("3.1." + number);
                    tb.setTestData(row.getCell(0).toString());
                    tb.setTitle(row.getCell(1).toString());
                    //Word换行主要有两大类,一类是表格单元格文本的换行,另一类是表格之外的文本的换行。对于表格外的文本我们可以使用“\r”或者“(char)11”来换行,然而对于表格内的文本我们只能使用“(char)11”来进行换行操作。
                    tb.setFunctionPoint(row.getCell(2).toString().replace('\n', (char)11));
                    combineLines = isMergedRegion(sheet, rowNum, 1);
                    String process = "";
                    String conclusion = "";
                    Set<String> s = new HashSet<String>();
                    for (int i = rowNum; i< rowNum + combineLines; i++) {
                        //步骤
                        HSSFRow r1 = sheet.getRow(i);
                        process += r1.getCell(5).toString() + (char)11;
    
                        //结果
                        String result = r1.getCell(7).toString(); 
                        s.add(result);
                    }
                    if(s.contains("未通过")){
                        conclusion = "未通过";
                    }else if(s.contains("无法测试")){
                        conclusion = "无法测试";
                    }else if(s.contains("不涉及")){
                        conclusion = "不涉及";
                    }else{
                        conclusion = "通过";
                    }
                    tb.setProcess(process);
                    tb.setConclusion(conclusion);
                    number++;
                    list.add(tb);
                }
            }
        }
        if(suffix.equals("xlsx")){
            XSSFWorkbook workbook = null;
            workbook = new XSSFWorkbook(is);
    
            if(workbook != null){  
    
                XSSFSheet sheet = workbook.getSheetAt(1);
                if(sheet == null){
                    System.out.println();
                    return null;
                }
                list = new ArrayList<Table>(); 
    
                int combineLines = 1;
                //读取行
                for (int rowNum = 3; rowNum < sheet.getLastRowNum(); rowNum+=combineLines) {
                    XSSFRow row = sheet.getRow(rowNum);
    
                    if (row == null) continue;
                    Table tb = new Table(); 
                    tb.setTestData(row.getCell(0).toString());
                    tb.setTitle(row.getCell(1).toString());
                    //Word换行主要有两大类,一类是表格单元格文本的换行,另一类是表格之外的文本的换行。对于表格外的文本我们可以使用“\r”或者“(char)11”来换行,然而对于表格内的文本我们只能使用“(char)11”来进行换行操作。
                    tb.setFunctionPoint(row.getCell(2).toString().replace('\n', (char)11));
                    combineLines = isMergedRegion(sheet, rowNum, 1);
                    String process = "";
                    String conclusion = "通过";
                    for (int i = rowNum; i< rowNum + combineLines; i++) {
                        //步骤
                        XSSFRow r1 = sheet.getRow(i);
                        process += r1.getCell(5).toString() + (char)11;
    
                        //结果
                        String result = r1.getCell(7).toString(); 
                        if(result.equals("不涉及")){
                            conclusion = "不涉及";
                        }
    
                        if(result.equals("无法测试")){
                            conclusion = "无法测试";
                        }
    
                        if(result.equals("未通过")){
                            conclusion = "未通过";
                        }
    
                    }
                    tb.setProcess(process);
                    tb.setConclusion(conclusion);
    
                    list.add(tb);
                }
            }
        }
    
        return list;
    }
    
    
    填充word模板,并合并多个word文档,合并文档POI好像没有,我用的Aspose,,也蛮强大的
    public String generateWord(List<Table> list, String wordTemplatePath, String outputPath) throws Exception{
    
        ArrayList<String> tempList = new ArrayList<String>();
    
        for(int i=0; i < list.size(); i++){
            FileInputStream in = new FileInputStream(wordTemplatePath);
            HWPFDocument document = new HWPFDocument(in);
    
            Table tb = list.get(i);
            Map<String,String> params = new HashMap();
            params.put("${title}", tb.getTitle());
            params.put("${functionPoint}", tb.getFunctionPoint());
            params.put("${conclusion}", tb.getConclusion());
            params.put("${testData}", tb.getTestData());
            params.put("${process }", tb.getProcess());
            params.put("${number}", tb.getNumber());
            Range range = document.getRange();
    
            // 替换读取到的 word 模板内容的指定字段
            for(Map.Entry<String,String> entry:params.entrySet()){
                range.replaceText(entry.getKey(),entry.getValue());
            }
    
            String savePath = outputPath +UUID.randomUUID() + ".doc";
            document.write(new File(savePath));
            document.close();
    
            tempList.add(savePath);
        }
    
        return mergeDoc(tempList, outputPath);
    }
    
    public String mergeDoc(ArrayList<String> list, String outputPath) throws Exception{
    

    // License lic = new License();
    // lic.setLicense((InputStream)null);//设置license,非试用
    String uuid = UUID.randomUUID() + ".doc";
    String savePath = outputPath + uuid;
    Document doc = new Document();
    doc.removeAllChildren();

        for (int i = 0; i < list.size(); i++) {
            Document srcDoc = new Document(list.get(i));
            doc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            File f = new File(list.get(i));
            f.delete();
        }
    
        doc.save(savePath);
        return uuid;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料