zhouxiaoyanios 2017-04-20 03:15 采纳率: 100%
浏览 4988
已采纳

java导出excel表格多sheet问题。已有模板。附代码。

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;
}

}

  • 写回答

1条回答 默认 最新

  • 过劳肥码农 2017-04-22 03:46
    关注

    WritableWorkbook workbook = Workbook.createWorkbook(......);

           /** **********创建工作表************ */  
    
           WritableSheet sheet = workbook.createSheet("Sheet1", 0); 
    

    0是第一个,1第二个

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台