export导出报表

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 模板

在这里插入图片描述

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;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值