try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode("文件名", "UTF-8") + ".xls");
JRDataSource ds = new JRBeanCollectionDataSource(list);
List<ColumnBuilder> columnList = new ArrayList<ColumnBuilder>();
columnList.add(Columns
.column("任务单号", "TASKNUM", DataTypes.stringType())
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.setWidth(150));
columnList.add(Columns.column("任务标题", "TASKTITLE",
DataTypes.stringType()).setWidth(250));
columnList.add(Columns.column("任务内容", "TASKCONTENT",
DataTypes.stringType()).setWidth(250));
columnList.add(Columns.column("检查企业", "OBJECTNAME",
DataTypes.stringType()).setWidth(250));
columnList.add(Columns.column("任务类型", "TASKTYPE",
DataTypes.stringType()));
columnList.add(Columns.column("任务来源", "TASKSOURCENAME",
DataTypes.stringType()));
columnList.add(Columns.column("开始时间", "STARTTIME",
DataTypes.stringType()).setWidth(120));
columnList.add(Columns.column("截止时间", "ENDTIME",
DataTypes.stringType()).setWidth(120));
columnList.add(Columns.column("紧急程度", "URGENCYNAME",
DataTypes.stringType()));
columnList.add(Columns.column("发起人", "CREATERNAME",
DataTypes.stringType()));
columnList.add(Columns.column("任务状态", "TASKSTATUS",
DataTypes.stringType()));
DynamicreportsReport.toXlsReport(columnList, ds,
response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
import static net.sf.dynamicreports.report.builder.DynamicReports.export;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import java.io.OutputStream;
import java.util.List;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.jasper.builder.export.JasperXlsExporterBuilder;
import net.sf.dynamicreports.jasper.constant.JasperProperty;
import net.sf.dynamicreports.report.builder.column.ColumnBuilder;
import net.sf.jasperreports.engine.JRDataSource;
public class DynamicreportsReport {
public static void toXlsReport(List<ColumnBuilder> columnList,
JRDataSource dataSource, OutputStream out) throws Exception {
try {
JasperXlsExporterBuilder xlsExporter = export.xlsExporter(out)
.setDetectCellType(true).setIgnorePageMargins(true)
.setWhitePageBackground(false)
.setRemoveEmptySpaceBetweenColumns(true);
JasperReportBuilder exporterBuilder = report()
.addProperty(JasperProperty.EXPORT_XLS_FREEZE_ROW, "2")
.ignorePageWidth().ignorePagination();
for (ColumnBuilder textColumnBuilder : columnList) {
exporterBuilder = exporterBuilder.columns(textColumnBuilder);
}
exporterBuilder.setDataSource(dataSource).toXls(xlsExporter);
// out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}