poi導出excel 下拉列表值超過255問題

public void exportExcel(List<List<String>> list,HttpServletResponse response,String fileName) throws Exception {app

XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheetCs = wb.createSheet("sheet1");
    //爲首行賦值
    for(int i=0;i<list.size();i++){
        XSSFRow row = sheetCs.createRow(i);
        for(int j=0;j<list.get(i).size();j++){
            sheetCs.setColumnWidth(j, 3500);//設置列寬
            XSSFCell cell = row.createCell(j);
            cell.setCellValue(list.get(i).get(j));
        }
    }

    int sheetTotal=wb.getNumberOfSheets();
    XSSFRow row;
    //第一個下拉選
    //新建一個sheet頁
    String hiddenSheetName = "hiddenSheet" + sheetTotal;
    XSSFSheet hiddenSheet = wb.createSheet(hiddenSheetName);
    //數據
    String[] selectList =new String[100]; //對應列下拉框數據
    for(int i=0;i<100;i++){
        selectList[i]="測試數據"+i;
    }
    //寫入下拉數據到新的sheet頁中
    for (int i = 0; i < selectList.length; i++) {
        row = hiddenSheet.createRow(i);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(selectList[i]);
    }
    //獲取新sheet頁內容
    String strFormula = hiddenSheetName + "!$A$1:$A$65535";
    XSSFDataValidationConstraint constraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST,strFormula);
    // 設置數據有效性加載在哪一個單元格上,四個參數分別是:起始行、終止行、起始列、終止列
    CellRangeAddressList regions = new CellRangeAddressList(1,65535, 0, 0);
    // 數據有效性對象
    DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) sheetCs);
    DataValidation validation = help.createValidation(constraint, regions);
    sheetCs.addValidationData(validation);
    //將新建的sheet頁隱藏掉
    wb.setSheetHidden(sheetTotal, true);
    sheetTotal++;


    //第二個下拉選
    hiddenSheetName = "hiddenSheet" + sheetTotal;
    XSSFSheet hiddenSheet2 = wb.createSheet(hiddenSheetName);
    String[] selectList2 =new String[100]; //對應列下拉框數據
    for(int i=0;i<100;i++){
        selectList2[i]="測試數據2"+i;
    }
    for (int i = 0; i < selectList2.length; i++) {
        row = hiddenSheet2.createRow(i);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(selectList2[i]);
    }
    String strFormula2 = hiddenSheetName + "!$A$1:$A$65535";
    XSSFDataValidationConstraint constraint2 = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST,strFormula2);
    CellRangeAddressList regions2 = new CellRangeAddressList(1,65535, 1, 1);
    DataValidationHelper help2 = new XSSFDataValidationHelper((XSSFSheet) sheetCs);
    DataValidation validation2 = help2.createValidation(constraint2, regions2);
    sheetCs.addValidationData(validation2);
    wb.setSheetHidden(sheetTotal, true);
    sheetTotal++;


    try {
        OutputStream output = response.getOutputStream();
        fileName = URLEncoder.encode(fileName,"UTF-8");
        response.addHeader("Content-Disposition", "inline;filename="+fileName+".xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.flush();
        output.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // 結束
    System.out.println("Over");

}測試

相關文章
相關標籤/搜索