1 将导出的模板上传到数据库
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:oracle:thin:@172.16.4.41:1521:orcl";
String userName = "sdwst";// 登陆名
String password = "sdwst";// 密码
try {
Class.forName("oracle.jdbc.OracleDriver"); //创建数据库连接
conn = DriverManager.getConnection(url, userName, password);
String sql = "insert into RPT_RPTDEFINE(id,reportinfo) values(?,?)";
PreparedStatement pres = conn.prepareStatement(sql);
File file = new File("C:\\Users\\李逍遥\\Desktop\\评审查看.xls");
FileInputStream fis = new FileInputStream(file); //创建两个流
//InputStream in2 = new FileInputStream("C:\\Users\\李逍遥\\Desktop\\评审查看.xls");
//直接setBlob完工
pres.setString(1, "ReviewResultDetailExtdd");
pres.setBinaryStream(2, fis, (int)file.length());
pres.executeUpdate(); pres.close();
}
catch (Exception e)
{ e.printStackTrace(); }
}
2 模板
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/eb7bb0b355b9b42dd0dd42c924436d38.png)
3 調用
Map infoMap = new HashMap();
infoMap.put("data", preventMeasureList);
infoMap.put("reportTime", reportTime);
FileUtil.exportExcel(response, infoMap, "山东省疫情防控工作措施日报表", "", "preventMeasure");
4 FileUtil类
public class FileUtil {
public static void exportExcel(HttpServletResponse response, Map rptDataMap, String excelName, String reportPath,
String reportId) {
try {
HSSFWorkbook workBook = null;
InputStream in1 = null;
if ((reportPath == null) || (reportPath == "")) {
in1 = accessFullPath(reportId).getBinaryStream();
} else {
in1 = new FileInputStream(new File(reportPath));
}
XLSTransformer transformer = new XLSTransformer();
if (in1 != null) {
workBook = (HSSFWorkbook) transformer.transformXLS(in1, rptDataMap);
}
String fileName = excelName + ".xls";
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.reset();
response.setDateHeader("Expires", 1L);
response.setContentType("application/vnd.ms-excel");
excelName = new String(excelName.getBytes(), "ISO8859-1");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream os = response.getOutputStream();
workBook.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Blob accessFullPath(String repInfo) throws Exception {
Blob blob = null;
DruidDataSource dataSource = (DruidDataSource) SpringContextUtil.getBean("rmdb_dataSource");
String url = dataSource.getUrl();
String userName = dataSource.getUsername();
String password = dataSource.getPassword();
String sql = "select reportinfo from RPT_RPTDEFINE where id='" + repInfo + "'";
// String url = (String) prop.get("dataSource.url");
// String userName = (String) prop.get("dataSource.username");// 登陆名
// String password = (String) prop.get("dataSource.password");// 密码
// String url = "jdbc:oracle:thin:@172.16.13.107:1521:orcl";
// String userName = "yqjc";// 登陆名
// String password = "yqjc";// 密码
Class.forName("oracle.jdbc.OracleDriver");
// 创建数据库连接
Connection conn = DriverManager.getConnection(url, userName, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
try {
if (rs.next()) {
blob = rs.getBlob("reportinfo");
}
} catch (Exception e) {
e.printStackTrace();
rs.close();
throw new Exception("获得模板有误!");
}
return blob;
}
}