根据模板生成html文件并下载
该模块用于相关内容导出功能,先读取模板文件,再替换模板中的内容,最后直接以流的形式提供下载。主要代码如下:
String templateContent = ""; // 读取模板文件 FileInputStream fileinputstream = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fileinputstream, "utf-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { templateContent += line; } br.close(); // 替换 templateContent = templateContent.replaceAll("###title###", 【替换的内容】); |
模板代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>###title###</title> <style type="text/css"> * { margin: 0; padding: 0; } body { overflow: auto; font-family: "微软雅黑"; font-size:14px; color:#444;} </style> </head> <body> ###htmlContent### </body> </html> |