如何利用Jaspersoft iReport Designer创建表格
1个回答
2016-02-22
展开全部
1)用ireport创建完成jrxml文件后,到ireport的安装目录找到相应的jasper文件,然后放到项目的一个目录中。(由于让程序每次都编译jrxml很浪费时间,再说模板也不容易改变,建议只编译一次就OK)
2)使用代码如下
Session session = HibernateUtil.currentSession();
Connection con = session.connection(); //得到链接
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQ ry("select * from applications_list where app_id='" +appId+"'"); //用数据集传入带参数的SQL语句
System.out.println("select * from applications_list where appl_list_id='" +appId+"'");
String appPath = req st.getSession().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 = ToUTF8.toUtf8String("设备申请单"); //处理下载后文件名中文问题
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, response.getOutputStream());
exporter.exportReport();
?
?
?
* 返回字符编码UTF-8 */
?p lic static String toUtf8String(String s) {
StringB?r sb = new StringB?r();
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 = Character.toString(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("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
?}
不懂楼主为什么要用这个也太麻烦了吧,上面是我去论坛里面搜的,真心无力啊啊,我用的finereport,功能很强大齐全啊,也不用还搞代码。网上有免费版,楼主可以百度看看,希望可以帮到你。
2)使用代码如下
Session session = HibernateUtil.currentSession();
Connection con = session.connection(); //得到链接
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQ ry("select * from applications_list where app_id='" +appId+"'"); //用数据集传入带参数的SQL语句
System.out.println("select * from applications_list where appl_list_id='" +appId+"'");
String appPath = req st.getSession().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 = ToUTF8.toUtf8String("设备申请单"); //处理下载后文件名中文问题
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, response.getOutputStream());
exporter.exportReport();
?
?
?
* 返回字符编码UTF-8 */
?p lic static String toUtf8String(String s) {
StringB?r sb = new StringB?r();
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 = Character.toString(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("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
?}
不懂楼主为什么要用这个也太麻烦了吧,上面是我去论坛里面搜的,真心无力啊啊,我用的finereport,功能很强大齐全啊,也不用还搞代码。网上有免费版,楼主可以百度看看,希望可以帮到你。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询