Table2是我需要的。模板中字段、数目都是确定的。不知道怎么分sheet插入。
@Service
public class ReportServiceImpl implements ReportService {
@Resource
SummarySheetMapper summarySheetMapper;
@Resource
private DicService dicService;
@Override
public byte[] getSummarySheet(String contextPath, String year) throws IOException {
List<OrgOrder> orgOrders = summarySheetMapper.getOrgOrder();
List<Services> services = summarySheetMapper.getServiceData(year);
List<Dept1> dept1s = summarySheetMapper.getDept1Data(year);
List<Dept2> dept2s = summarySheetMapper.getDept2Data(year);
List<Entities> entitys = summarySheetMapper.getEntityData(year);
List<Techs> techs = summarySheetMapper.getTechData(year);
List<Icu> icu = summarySheetMapper.getIcuData(year);
List<Table1> list = mergeAndSort(orgOrders, services, dept1s, dept2s, entitys, techs);
List<Table2> list1 = mergeAndSort(orgOrders, icu);
ListXlsReport<Table1> report = new ListXlsReport<Table1>(4, 4) {
@Override
protected void write(Table1 data, HSSFSheet sheet, int rowIndex, int columnBeginIndex) throws IOException {
int index = columnBeginIndex;
List<String> row = data.getRow();
for (String s : row) {
if (s != null)
XlsUtils.setCellValue(sheet, rowIndex, index, s);
index++;
}
}
};
ListXlsReport<Table2> report1 = new ListXlsReport<Table2>(4, 4){
@Override
protected void write(Table2 data, HSSFSheet sheet, int rowIndex, int columnBeginIndex) throws IOException {
int index = columnBeginIndex;
List<String> row = data.getRow();
for (String s : row) {
if (s != null)
XlsUtils.setCellValue(sheet, rowIndex, index, s);
index++;
}
}
};
report.addData(list);
report1.addData(list1);
System.out.println(list.size());
System.out.println(list1.size());
ByteArrayOutputStream buf = new ByteArrayOutputStream(1024 * 1024 * 2);
File file = new File(contextPath, "files/template/summary_sheet.xls");
if (file.isFile()) {
FileInputStream template = new FileInputStream(file);
try {
report.build(template, buf);
report1.build(template, buf);
} finally {
template.close();
}
return buf.toByteArray();
} else {
throw new FileNotFoundException(file.getAbsolutePath());
}
}
private List<Table1> mergeAndSort(List<OrgOrder> orgOrders, List<Services> services, List<Dept1> dept1s, List<Dept2> dept2s, List<Entities> entitys, List<Techs> techs) {
List<Table1> list = new ArrayList<>();
for (OrgOrder o : orgOrders) {
Integer orgId = o.getOrgId();
Services service = find(orgId, services);
Dept1 dept1 = find(orgId, dept1s);
Dept2 dept2 = find(orgId, dept2s);
Entities entity = find(orgId, entitys);
Techs tech = find(orgId, techs);
if (tech != null) {
tech.setDicService(dicService);
}
Table1 t = new Table1(o, service, dept1, dept2, entity, tech);
list.add(t);
}
Collections.sort(list);
return list;
}
/*@Override
public byte[] getSummarySheet1(String contextPath, String year) throws IOException {
List<OrgOrder> orgOrders = summarySheetMapper.getOrgOrder();
List<Icu> icu = summarySheetMapper.getIcuData(year);
List<Table2> list1 = mergeAndSort(orgOrders, icu);
ListXlsReport<Table2> report1 = new ListXlsReport<Table2>(4, 4){
@Override
protected void write(Table2 data, HSSFSheet sheet, int rowIndex, int columnBeginIndex) throws IOException {
int index = columnBeginIndex;
List<String> row = data.getRow();
for (String s : row) {
if (s != null)
XlsUtils.setCellValue(sheet, rowIndex, index, s);
index++;
}
}
};
report1.addData(list1);
ByteArrayOutputStream buf = new ByteArrayOutputStream(1024 * 1024 * 2);
File file = new File(contextPath, "files/template/summary_sheet.xls");
if (file.isFile()) {
FileInputStream template = new FileInputStream(file);
try {
report1.build(template, buf);
} finally {
template.close();
}
return buf.toByteArray();
} else {
throw new FileNotFoundException(file.getAbsolutePath());
}
}*/
private List<Table2> mergeAndSort(List<OrgOrder> orgOrders, List<Icu> icu) {
List<Table2> list = new ArrayList<>();
for (OrgOrder o : orgOrders) {
Integer orgId = o.getOrgId();
Icu icus = find(orgId, icu);
Table2 t = new Table2(o, icus);
list.add(t);
}
Collections.sort(list);
return list;
}
private static <T extends OrgData> T find(Integer orgId, List<T> list) {
for (T d : list) {
if (orgId.equals(d.getOrgId())) {
return (T) d;
}
}
return null;
}
}