常立欣
如何使⽤iReport和jasper⽣成报表(SQL传参数及中⽂⽂件名)
2.这⾥只介绍⼏个难点。
1)⽤ireport创建完成jrxml⽂件后,到ireport的安装⽬录到相应的jasper⽂件,然后放到项⽬的⼀个⽬录中。(由于让程序每次都编译jrxml很浪费时间,再说模板也不容易改变,建议只编译⼀次就OK) 2)使⽤代码如下
玉米蛋白粉Session session = HibernateUtil.currentSession();
Connection con = tion(); //得到链接
Statement stmt = ateStatement();
ResultSet rs = uteQuery("select * from applications_list where app_id='" +appId+"'"); //⽤数据集传⼊带参数的SQL 语句
System.out.println("select * from applications_list where appl_list_id='" +appId+"'");
String appPath = Session().getServletContext().getRealPath("/"); //得到以便于好的jasper⽂件
if (appPath.lastIndexOf("//") != appPath.length() - 1)appPath += "//";
String reportFileName = appPath+ "jasper//untitled_report_1.jasper";
Map parameters = new HashMap();
parameters.put("Title", "设备申请列表");//注意可以有很多个参数 JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName, parameters, new JRResultSetDataSource(rs));
JRRtfExporter exporter = null;
exporter = new JRRtfExporter();
response.setContentType("text/html; charset=UTF-8");
response.setContentType("application/msword");灌溉排水学报
String applicatiosname = Utf8String("设备申请单"); //处理下载后⽂件名中⽂问题
response.setHeader("Content-disposition","attachment;filename="+applicatiosname+".doc");
宝能收购万科exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, OutputStream());
* 返回字符编码UTF-8 */
rosmmpublic static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = String(c).getBytes("UTF-8");
} catch (Exception ex) {
//System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
行动研究法
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + HexString(k).toUpperCase()); }
}
}
String();
}