使用Poi-tl对word模板生成动态报告

一、pom依赖问题:

<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.2</version>
</dependency>

使用 poi-tl 的 1.12.2版本,如果使用了poi依赖,则必须使用 poi的5.2.2版本(没有用到可以不引用)

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>

注意:如果还需要使用easyPoi 4.4.0 那这样是不行的,因为poi的5.2.2版本不兼容easyPoi ,只能将poi的版本降低到4.1.1,这样 poi-tl 就不能使用了,这里只能三者取其二。

二、具体实现:

1. word模板文件结构,根据实际情况修改:

这里我们使用了基本字符、表格,还有集合里面嵌套集合。 

2. 相关代码:

基本entity:

package com.fan.poi.entity;

import lombok.Data;

import java.util.List;

@Data
public class PersonDuty {
    private String startMonth;
    private String startDay;
    private String endMonth;
    private String endDay;
    /**
     * 到岗总人数
     */
    private String dgSum;
    /**
     * 未到岗总人数
     */
    private String ndgNum;
    /**
     * 到岗率
     */
    private String dgRate;

    private String dutyName;


    private List<ReportTable> tableList1;

    private List<ReportDataTable> monList;

}
package com.fan.poi.entity;

import lombok.Data;

@Data
public class ReportTable {
    /**
     * 所属部门
     */
    private String supUnitName;
    /**
     * 到岗天数
     */
    private String supCount;
    /**
     * 未到岗数量
     */
    private String noSupCount;
    /**
     * 部门到岗率
     */
    private String syoSum;
    /**
     *  人员到岗总时长
     */
    private String allSupDuration;
}
package com.fan.poi.entity;

import lombok.Data;

import java.util.List;

@Data
public class ReportDataTable {
    private String name;
    /**
     * 所属部门
     */
    private String supUnitName;
    /**
     * 共到岗天数
     */
    private String supCount;
    /**
     * 未到岗天数
     */
    private String noSupCount;
    /**
     * 到岗率
     */
    private String syoSum;
    /**
     * 累计到岗时长
     */
    private String allSupDuration;
    /**
     * 详细情况
     */
    private List<TaskDailyDataList> taskDailyDataList;

}
package com.fan.poi.entity;

import lombok.Data;

@Data
public class TaskDailyDataList {
    private String month;

    private String day;
    /**
     * 考勤人员
     */
    private String Name;
    /**
     * 累计在岗时长
     */
    private String totalOnSiteMinutes;
}

实现代码: 

package com.fan.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.fan.poi.entity.PersonDuty;
import com.fan.poi.entity.ReportDataTable;
import com.fan.poi.entity.ReportTable;
import com.fan.poi.entity.TaskDailyDataList;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态生成word模板
 * @author fan
 */
public class FileUtil {
    public static void main(String[] args) {
        exportWord("D:\\生成前的模板文件.docx", "D:\\");
    }

    public static void exportWord( String sorFilePath, String TarFilePath) {
        try {

            PersonDuty personDuty = new PersonDuty();
            // 模拟数据
            personDuty.setDutyName("张三");
            personDuty.setStartDay("2023-05-01");
            personDuty.setEndDay("2023-05-07");
            personDuty.setStartMonth("2023-05");
            personDuty.setEndMonth("2023-05");
            personDuty.setDgSum("5");
            personDuty.setDgRate("90");
            personDuty.setNdgNum("1");
            List<ReportTable> tableList1 = new ArrayList<>();
            ReportTable reportTable = new ReportTable();
            reportTable.setSyoSum("100");
            reportTable.setSupUnitName("测试部门");
            reportTable.setSupCount("2");
            reportTable.setNoSupCount("0");
            reportTable.setAllSupDuration("10");
            tableList1.add(reportTable);
            ReportTable reportTable1 = new ReportTable();
            reportTable1.setSyoSum("33%");
            reportTable1.setSupUnitName("开发部门");
            reportTable1.setSupCount("2");
            reportTable1.setNoSupCount("1");
            reportTable1.setAllSupDuration("10");
            tableList1.add(reportTable1);
            personDuty.setTableList1(tableList1);
            List<ReportDataTable> monList = new ArrayList<>();
            ReportDataTable reportDataTable = new ReportDataTable();
            reportDataTable.setName("张三");
            reportDataTable.setSupUnitName("测试部");
            reportDataTable.setSupCount("5");
            reportDataTable.setNoSupCount("0");
            reportDataTable.setSyoSum("100%");
            reportDataTable.setAllSupDuration("40");
            List<TaskDailyDataList> taskDailyDataList = new ArrayList<>();
            TaskDailyDataList taskDailyDataList1 = new TaskDailyDataList();
            taskDailyDataList1.setName("张三");
            taskDailyDataList1.setDay("2023-05-01");
            taskDailyDataList1.setMonth("2023-05");
            taskDailyDataList1.setTotalOnSiteMinutes("40");
            TaskDailyDataList taskDailyDataList2 = new TaskDailyDataList();
            taskDailyDataList2.setName("李四");
            taskDailyDataList2.setDay("2021-05-01");
            taskDailyDataList2.setMonth("2021-05");
            taskDailyDataList2.setTotalOnSiteMinutes("40");
            taskDailyDataList.add(taskDailyDataList1);
            taskDailyDataList.add(taskDailyDataList2);
            reportDataTable.setTaskDailyDataList(taskDailyDataList);
            monList.add(reportDataTable);
            personDuty.setMonList(monList);

            String targetFileName = "生成后的文件.docx";

            //modelFileName是模板路径/模板文件名
            String modelFileName = "D:";
            File file = new File(modelFileName);
            if (!file.exists()) {
                System.out.println("模板文件不存在:" + modelFileName);
                throw new RuntimeException("模板文件不存在");
            }
            //生成报告,对于集合,只要personDuty包含对应的集合即可,有嵌套也会自动替换
            Configure config = Configure.builder()
                    .bind("tableList1", new LoopRowTableRenderPolicy())
                    .bind("monList", new LoopRowTableRenderPolicy())
                    .build();

            XWPFTemplate template = XWPFTemplate.compile(sorFilePath, config).render(personDuty);

            //判断目标路径是否存在
            File directory = new File(TarFilePath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            //定义输出路径
            String targetPath = TarFilePath + targetFileName;
            template.writeToFile(targetPath);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

生成后的文件:

  这里主要是简单的把几种场景实现一下,项目中遇到的问题,还需我们自定义拼接逻辑。

您可以使用Google Cloud Speech-to-Text API将音频PCM识别成文字。以下是Java代码示例: 1. 首先,您需要设置Google Cloud凭据和API客户端: ```java import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.speech.v1.*; import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class SpeechToText { public static void main(String[] args) throws Exception { // Set the credentials GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("path/to/credentials.json")); SpeechClient speechClient = SpeechClient.create(SpeechSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build()); // The path to the audio file to transcribe String audioFilePath = "path/to/audio.pcm"; // The language of the audio file String languageCode = "en-US"; // The sample rate of the audio file int sampleRate = 16000; // Transcribe the audio file String text = transcribeAudioFile(speechClient, audioFilePath, languageCode, sampleRate); // Print the transcribed text System.out.println(text); } // Transcribes the given audio file using the Google Cloud Speech-to-Text API public static String transcribeAudioFile(SpeechClient speechClient, String audioFilePath, String languageCode, int sampleRate) throws IOException { // Read the audio file Path path = Paths.get(audioFilePath); byte[] data = Files.readAllBytes(path); ByteString audioBytes = ByteString.copyFrom(data); // Configure the recognition request RecognitionConfig config = RecognitionConfig.newBuilder() .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) .setSampleRateHertz(sampleRate) .setLanguageCode(languageCode) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build(); // Recognize the audio file RecognizeResponse response = speechClient.recognize(config, audio); List<SpeechRecognitionResult> results = response.getResultsList(); // Concatenate the transcribed text from each result StringBuilder sb = new StringBuilder(); for (SpeechRecognitionResult result : results) { List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); for (SpeechRecognitionAlternative alternative : alternatives) { sb.append(alternative.getTranscript()); } } return sb.toString(); } } ``` 2. 在您的项目中添加以下依赖项: ```xml <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-speech</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.google.auth</groupId> <artifactId>google-auth-library-oauth2-http</artifactId> <version>0.22.2</version> </dependency> ``` 请注意,您需要替换代码中的“path/to/credentials.json”和“path/to/audio.pcm”为实际的凭据和音频文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值