springboot統合poi-tlテンプレートからwordをエクスポートします.
24747 ワード
springboot統合poi-tlテンプレートからwordをエクスポートします.
poi-tl中国語文書:http://deepoove.com/poi-tl/必要なパケットを導入する 工具類 処理controller ワードテンプレートピクチャのパラメータ{param}は、上記controlerにおけるmapのkey値 に対応する.によって導出された利点は、データの置き換えによってスタイルが変更されることがないことである.
poi-tl中国語文書:http://deepoove.com/poi-tl/
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.0</version>
</dependency>
import com.deepoove.poi.XWPFTemplate;
import org.springframework.util.ClassUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
/**
* @ Author :AZY.
* @ Date :Created in 10:55 2019/8/19
* @ Description:
*/
public class DocUtil {
public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, Map dataMap) {
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
XWPFTemplate template = XWPFTemplate.compile(path+"static/templates/word.docx").render(dataMap);
OutputStream out = null;
try {
out = new FileOutputStream("out_template.docx");
template.write(out);
out.flush();
out.close();
template.close();
} catch (IOException e) {
e.printStackTrace();
}
InputStream fis = null;
OutputStream toClient = null;
File file = new File("out_template.docx");
try {
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// response
response.reset();
// response Header
newWordName = URLEncoder.encode(newWordName, "utf-8"); // URLEncoder
response.addHeader("Content-Disposition", "attachment;filename=" + newWordName+"");
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(toClient!=null){
toClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @ Author :AZY.
* @ Date :Created in 11:24 2019/8/19
* @ Description:
*/
@RestController
public class wordcontroller {
@RequestMapping("getDoc")
public void getDoc(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String,String> dataMap = new HashMap<String,String>();
dataMap.put("workname", " ");
dataMap.put("name1", "wuhui");
dataMap.put("name2", "azy");
dataMap.put("name3", "zyq");
dataMap.put("year", "2019");
dataMap.put("month", "08");
dataMap.put("day", "19");
String newWordName = " .doc";
// word
DocUtil.download(request,response,newWordName, dataMap);
}
}
print_r(' , ');
var_dump(' , ');
NSLog(@" , !")
System.out.println(" , !");
console.log(" , !");
print(" , !");
printf(" , !
");
cout << " , !" << endl;
Console.WriteLine(" , !");
fmt.Println(" , !")
Response.Write(" , ");
alert(’ , ’)