Java 在ModelAndView中传递多个模型并迭代这些模型

Java 在ModelAndView中传递多个模型并迭代这些模型,java,model-view-controller,apache-poi,poi-hssf,modelandview,Java,Model View Controller,Apache Poi,Poi Hssf,Modelandview,我有一个要求,即我必须编写包含多张HSSF表的HSSF工作手册。 对于第一张纸,我有不同的列,第二张纸有不同的列。对于每一张表,我都编写了不同的存储过程。 目前,我的java代码的编写方式只能处理一个存储过程数据。我想修改它并调用第二个存储过程来填充第二个工作表中的数据。我正在ModelAndView对象中设置ExcelBean数据并构建文档。现在我的问题是如何获取每个模型的数据并将其写入HSSfSheet?就像在控制器中一样,我在map downloadExcelBean和downloadEx

我有一个要求,即我必须编写包含多张HSSF表的HSSF工作手册。 对于第一张纸,我有不同的列,第二张纸有不同的列。对于每一张表,我都编写了不同的存储过程。 目前,我的java代码的编写方式只能处理一个存储过程数据。我想修改它并调用第二个存储过程来填充第二个工作表中的数据。我正在ModelAndView对象中设置ExcelBean数据并构建文档。现在我的问题是如何获取每个模型的数据并将其写入HSSfSheet?就像在控制器中一样,我在map downloadExcelBean和downloadExcelBean2中设置了两个bean。我想在第一页写下downloadExcelBean,在第二页写下downloadExcelBean2。请建议。 我的控制器功能如下

     public ModelAndView exportToExcel(String fileName1, String filename2,String sheetname1,String sheetName2,
        String[] headerLabels1, String[] headerLabels2,int[] datatypeArray1,int[] datatypeArray2, String procString1,String procString2,
        Object[] objarray, HttpServletRequest arg0,
        HttpServletResponse arg1, String[] dbColumnNameArray1,String[] dbColumnNameArray2)
        throws Exception {
    Log.info(DownloadExcelController.class,
            "Execution Starts..exportExcel()");

    Map<String, DownloadExcelBean> downloadExcelBean = new HashMap<String, DownloadExcelBean>();
    //System.out.println(" in export excel part....."+procString+":::"+datatypeArray);
    List<List> workLists1 = downloadExcelService.storedProcedureToExcel(
            datatypeArray1, procString1, objarray, dbColumnNameArray1);

    List<List> workLists2 = downloadExcelService.storedProcedureToExcel(
            datatypeArray2, procString2, objarray, dbColumnNameArray2);

    DownloadExcelBean bean = new DownloadExcelBean();

    bean.setFilename(fileName1);
    bean.setSheetname(sheetname1);
    bean.setHeaderArray(headerLabels1);
    bean.setDatatypeArray(datatypeArray1);
    // bean.setVisibleArray(visibleArray);

    bean.setData(workLists1);

    DownloadExcelBean bean2 = new DownloadExcelBean();
    bean2.setFilename(filename2);
    bean2.setSheetname(sheetName2);
    bean2.setHeaderArray(headerLabels2);
    bean2.setDatatypeArray(datatypeArray2);
    bean2.setData(workLists2);

    downloadExcelBean.put("downloadExcelBean", bean);
    downloadExcelBean.put("downloadExcelBean2", bean2);
    return new ModelAndView("DownloadExcelView", "model",
                downloadExcelBean);



}
public model和view exportToExcel(字符串文件名1、字符串文件名2、字符串sheetname1、字符串sheetName2、,
字符串[]headerLabels1,字符串[]headerLabels2,int[]datatypeArray1,int[]datatypeArray2,字符串procString1,字符串procString2,
对象[]objarray,HttpServletRequest arg0,
HttpServletResponse arg1,字符串[]dbColumnNameArray1,字符串[]dbColumnNameArray2)
抛出异常{
Log.info(下载ExcelController.class,
“执行开始..exportExcel()”;
Map downloadExcelBean=new HashMap();
//System.out.println(“在导出excel部件中…”+procString+:“+datatypeArray);
列表工作列表1=下载ExcelService.storedProcedureToExcel(
datatypeArray1、procString1、objarray、dbColumnNameArray1);
List workLists2=下载ExcelService.storedProcedureToExcel(
datatypeArray2、procString2、objarray、dbColumnNameArray2);
DownloadExcelBean=新的DownloadExcelBean();
setFilename(fileName1);
bean.setSheetname(sheetname1);
bean.setHeaderArray(headerLabels1);
setDatatypeArray(datatypeArray1);
//设置visibleArray(visibleArray);
setData(workLists1);
DownloadExcelBean bean2=新的DownloadExcelBean();
bean2.setFilename(filename2);
bean2.设置表名(表名2);
bean2.setHeaderArray(headerLabels2);
bean2.setDatatypeArray(datatypeArray2);
bean2.设置数据(工作列表2);
downloadExcelBean.put(“downloadExcelBean”,bean);
downloadExcelBean.put(“downloadExcelBean2”,bean2);
返回新模型和视图(“下载Excel视图”、“模型”,
下载ExcelBean);
}
在DownloadExcelView.java中,我构建了excel文档函数

    protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    Log.info(DownloadExcelView.class,
            "Execution starts....buildExcelDocument()");

    response.setHeader("Content-Type", "application/vnd.ms-excel");

    Map<String, DownloadExcelBean> exceldownload = (Map<String, DownloadExcelBean>) model.get("model");

    for (Map.Entry<String, DownloadExcelBean> mapobject : exceldownload.entrySet()) 
    {

        DownloadExcelBean obj = (DownloadExcelBean) mapobject.getValue();
        String filename = obj.getFilename(); // File name
        String sheetname = obj.getSheetname(); // sheet Name
        String headerlist[] = obj.getHeaderArray(); // header names
        int dataTypeArray[] = obj.getDatatypeArray();

        List resultsetValues = obj.getData();

        response.setHeader("Content-Disposition", "attachment; filename="
                + filename + ".xls");

        HSSFSheet sheet = workbook.createSheet(sheetname); // create a sheet
        HSSFSheet sheet1 = workbook.createSheet("Second tab");

        HSSFCellStyle cellStyle = setHeaderStyle(workbook); // Apply bold
                                                            // for header

        HSSFRow header = sheet1.createRow(0); // create a header row

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        short decimalDataformat = workbook.createDataFormat().getFormat(
                "#,###,##0.00");

        HSSFCellStyle decimalCellStyle = workbook.createCellStyle();
        decimalCellStyle.setDataFormat(decimalDataformat);
        HSSFCellStyle wrapStyle = workbook.createCellStyle();
        wrapStyle.setWrapText(true);

        int visibleCol = -1;
        for (int i = 0; i < headerlist.length; i++) {

            visibleCol++;
            HSSFCell cell = header.createCell(visibleCol);
            cell.setCellStyle(cellStyle);

            cell.setCellValue(headerlist[i].replace("<br/>", ARConstants.EXPORT_CRLF));

        }

        // Excel cell values
        HSSFRow hssfrow;
        HSSFCell cell;

        for (int row = 0; row < resultsetValues.size(); row++) {

            List rowList = (ArrayList) resultsetValues.get(row);

            hssfrow = sheet1.createRow(row + 1);
            visibleCol = -1;
            for (int col = 0; col < rowList.size(); col++) {

                // if(visibleArray[col]){
                visibleCol++;
                cell = hssfrow.createCell(visibleCol);

                if (dataTypeArray[col] == java.sql.Types.DECIMAL) {

                    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(decimalCellStyle);

                    if (rowList.get(col) != null)
                        cell.setCellValue((Double) rowList.get(col));
                    else
                        cell.setCellValue("");

                } else if (dataTypeArray[col] == java.sql.Types.DATE) {

                    if (rowList.get(col) != null)
                        cell.setCellValue(dateFormat.format(rowList
                                .get(col)));
                    else
                        cell.setCellValue("");

                }
                else if (dataTypeArray[col] == java.sql.Types.TIMESTAMP) {
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellStyle(wrapStyle);


                    if (rowList.get(col) != null)
                        cell.setCellValue(StringUtils.dateToUSFormatDateTimeTillMins(rowList.get(col).toString())
                                .replace(" ", ARConstants.EXPORT_CRLF));
                    else
                        cell.setCellValue("");

                }

                else if (dataTypeArray[col] == java.sql.Types.OTHER)
                {
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellStyle(wrapStyle);
                    String crlf = Character.toString((char) 13)
                            + Character.toString((char) 10);

                    if (rowList.get(col) != null)
                    {
                        String str=(rowList.get(col).toString()).replaceAll("\t","    ");
                        if((str.replace("|", crlf)).length()> 32767)
                            cell.setCellValue((str.replace("|", crlf)).substring(0,32766));
                        else
                            cell.setCellValue(str.replace("|", crlf));
                    }
                    else

                        cell.setCellValue("");

                } 
                else 
                {
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

                    if (rowList.get(col) != null)
                    {
                        String str=(rowList.get(col).toString()).replaceAll("\t","    ");
                        if(str.length() > 32767)
                        {
                            cell.setCellValue(str.substring(0,32766));

                        }
                        else
                        {
                            cell.setCellValue(str);

                        }
                    }

                    else
                        cell.setCellValue("");

                }
                // sheet.autoSizeColumn(col);
            }

        }

    }


    Log.info(DownloadExcelView.class,
            "Execution ends....buildExcelDocument()");

}
受保护的文档(地图模型、HSSF工作簿、,
HttpServletRequest请求,HttpServletResponse响应)
抛出异常{
Log.info(下载ExcelView.class,
“执行开始…buildExcelDocument()”;
response.setHeader(“内容类型”、“应用程序/vnd.ms excel”);
MapExcelDownload=(Map)model.get(“model”);
对于(Map.Entry mapobject:exceldownload.entrySet())
{
DownloadExcelBean obj=(DownloadExcelBean)mapobject.getValue();
字符串filename=obj.getFilename();//文件名
String sheetname=obj.getSheetname();//工作表名称
字符串头列表[]=obj.getHeaderArray();//头名称
int dataTypeArray[]=obj.getDatatypeArray();
List resultsetValues=obj.getData();
setHeader(“内容处置”、“附件;文件名=”
+文件名+“.xls”);
HSSFSheet sheet=workbook.createSheet(sheetname);//创建工作表
HSSFSheet sheet1=工作簿.createSheet(“第二个选项卡”);
HSSFCellStyle cellStyle=setHeaderStyle(工作簿);//应用粗体
//用于标题
HSSFRow header=sheet1.createRow(0);//创建标题行
DateFormat DateFormat=新的简化格式(“MM/dd/yyyy”);
short decimalDataformat=工作簿.createDataFormat().getFormat(
"#,###,##0.00");
HSSFCellStyle decimalCellStyle=工作簿.createCellStyle();
setDataFormat(decimalDataformat);
HSSFCellStyle wrapStyle=工作簿.createCellStyle();
wrapStyle.setWrapText(true);
int visibleCol=-1;
for(int i=0;i”,ARConstants.EXPORT_CRLF));
}
//Excel单元格值
HSSFRow HSSFRow;
HSSF细胞;
对于(int row=0;row