82 lines
2.5 KiB
Java
82 lines
2.5 KiB
Java
package com.example.survey.util;
|
|
|
|
import com.deepoove.poi.XWPFTemplate;
|
|
import com.deepoove.poi.config.Configure;
|
|
import com.deepoove.poi.config.ConfigureBuilder;
|
|
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
|
|
import com.example.survey.entity.MetaData;
|
|
import com.example.survey.entity.Record;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @author Pope
|
|
*/
|
|
public class WordUtil {
|
|
public static void export2word(HttpServletResponse response, Map<String, Object> values, String templatePath) {
|
|
|
|
response.addHeader("Content-Disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".docx");
|
|
|
|
ConfigureBuilder configureBuilder = Configure.builder();
|
|
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
|
|
for (String key : values.keySet()) {
|
|
Object value = values.get(key);
|
|
if (value instanceof List) {
|
|
List<Object> newValue = (List<Object>) value;
|
|
if(newValue.size()==0){
|
|
continue;
|
|
}
|
|
Object o = newValue.get(0);
|
|
if (o instanceof String) {
|
|
StringBuilder sb = new StringBuilder();
|
|
newValue.forEach(str -> {
|
|
sb.append(str + " ");
|
|
});
|
|
values.put(key, sb.toString());
|
|
} else {
|
|
configureBuilder = configureBuilder.bind(key, policy);
|
|
}
|
|
}
|
|
}
|
|
XWPFTemplate template = XWPFTemplate.compile(templatePath, configureBuilder.build()).render(values);
|
|
try (BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream())) {
|
|
template.write(os);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
template.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void export2Word(HttpServletResponse response, MetaData metaData, Record record){
|
|
Map<String, Object> values = record.getValues();
|
|
String templatePath = metaData.getWordTemplate();
|
|
for (Map.Entry<String, Object> entry : metaData.getForm().entrySet()) {
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public static void handleText(){
|
|
|
|
}
|
|
|
|
public static void handleDate(){
|
|
|
|
}
|
|
|
|
public static void handleList(){
|
|
|
|
}
|
|
|
|
|
|
}
|