以ireport作为模版导出excel代码
来源:互联网 发布:淘宝亲宝贝天猫入驻 编辑:程序博客网 时间:2024/05/22 12:47
public static String exportXls(List list,String reportName) {
String fileName = "";
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
FileOutputStream outputStream = null;
String reportModelFile = "//Resource//reports//" + reportName +".jasper";
System.out.println(reportModelFile);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);//list中装载的是一个个javabean
String szTemp = ExportReport.class.getResource("/").toString();
reportModelFile = szTemp.substring(6,szTemp.length()-17) + reportModelFile;
try {
fileName = DateTimeUtil.getYYYYMMDDHHMMSS() + ".xls";
JasperPrint jasperPrint = new JasperPrint();
jasperPrint = JasperFillManager.fillReport(reportModelFile, null, ds);
JRXlsExporter exporter = new JRXlsExporter();
exporter
.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outPut);
exporter.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
exporter.setParameter(
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);
exporter.exportReport();
String szPath = "C:\\szTemp\\fileDownload";
File pathFile = new File(szPath);
if (!pathFile.isDirectory())
pathFile.mkdirs();
File savedFile = new File(pathFile, fileName);
if (savedFile.exists())
savedFile.delete();
outputStream = new FileOutputStream(savedFile);
outputStream.write(outPut.toByteArray(), 0,
outPut.toByteArray().length);
outputStream.flush();
outputStream.close();
} catch (JRException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outPut.flush();
outPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
String fileName = "";
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
FileOutputStream outputStream = null;
String reportModelFile = "//Resource//reports//" + reportName +".jasper";
System.out.println(reportModelFile);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);//list中装载的是一个个javabean
String szTemp = ExportReport.class.getResource("/").toString();
reportModelFile = szTemp.substring(6,szTemp.length()-17) + reportModelFile;
try {
fileName = DateTimeUtil.getYYYYMMDDHHMMSS() + ".xls";
JasperPrint jasperPrint = new JasperPrint();
jasperPrint = JasperFillManager.fillReport(reportModelFile, null, ds);
JRXlsExporter exporter = new JRXlsExporter();
exporter
.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outPut);
exporter.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
exporter.setParameter(
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);
exporter.exportReport();
String szPath = "C:\\szTemp\\fileDownload";
File pathFile = new File(szPath);
if (!pathFile.isDirectory())
pathFile.mkdirs();
File savedFile = new File(pathFile, fileName);
if (savedFile.exists())
savedFile.delete();
outputStream = new FileOutputStream(savedFile);
outputStream.write(outPut.toByteArray(), 0,
outPut.toByteArray().length);
outputStream.flush();
outputStream.close();
} catch (JRException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outPut.flush();
outPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
}
容易忘记加入的jar包如下:
jasperreports相关jar包(ireport工具中一般会自带),commons-digester.jar,poi相关jar
- 以ireport作为模版导出excel代码
- ireport、jasperreport 导出excel的java代码
- 最新 excel 导出代码.....(不需要模版)
- iReport导出Excel不分页
- 导出excel调用模版
- 模版导出excel、word
- Easypoi模版导出excel
- jxl调用模版导出Excel
- POI利用模版导出excel
- java 导出excel copy 模版
- ireport Excel/pdf导出去掉分页显示
- iReport导出Excel线特别乱
- 从GridView控件以Excel表格导出代码的注释
- 根据excel模版导出数据到excel
- 根据excel模版导出数据到excel
- DataTable作为数据源导出Excel
- 导出EXCEL以HTML代码向excel输出不是标准的excel 方法直接可用
- 以Excel作为模板产生Excel
- C#中的委托和事件(续)
- 迭代式mapreduce
- oracle数据库中sql基础
- 三种东西永远不要放到数据库里
- TF/IDF/DF算法
- 以ireport作为模版导出excel代码
- new和operate new的区别与联系
- Android开发学习笔记:数据存取之SQLite浅析
- 推荐一个很棒的免费自助建站工具:Tap
- Qt教程
- Pseudoregisters在MFC里debugging中的应用
- 分享50佳高质量免费按钮图标资源(上篇)
- win7 dlna设置
- 比照顾爹妈还周全:智能狗圈让你进一步变狗奴