POI实现Excel省市区三级联动java实现

2003版Excel

import java.io.FileOutputStream;
import java.util.*;

import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
/**
 * @ClassName: test3
 * @Author: yongtao.ding on 2019/5/29 9:48
 * @Version: 1.0
 */
public class test3 {

    public static void main(String[] args) {
        Cascade();
    }

    public static void Cascade() {
        // 创建一个excel
        @SuppressWarnings("resource")
        Workbook book = new HSSFWorkbook();

        // 创建需要用户填写的sheet
        HSSFSheet sheetPro = (HSSFSheet) book.createSheet("省市县");
        Row row0 = sheetPro.createRow(0);
        row0.createCell(0).setCellValue("省");
        row0.createCell(1).setCellValue("市");
        row0.createCell(2).setCellValue("区");


        // 查询所有的省名称
        List provinceArr = new ArrayList();
        provinceArr.add("安徽省");
        provinceArr.add("浙江省");

        // 整理数据,放入一个Map中,mapkey存放父地点,value 存放该地点下的子区域
        Map> areaMap = new HashMap>();
        areaMap.put("浙江省", Lists.newArrayList("杭州市", "宁波市"));
        areaMap.put("安徽省", Lists.newArrayList("芜湖市", "滁州市"));
        areaMap.put("芜湖市", Lists.newArrayList("戈江区", "三山区"));
        areaMap.put("滁州市", Lists.newArrayList("来安县", "凤阳县"));

        // 创建一个专门用来存放地区信息的隐藏sheet页
        // 因此也不能在现实页之前创建,否则无法隐藏。
        Sheet hideSheet = book.createSheet("area");
        // 这一行作用是将此sheet隐藏,功能未完成时注释此行,可以查看隐藏sheet中信息是否正确
        // book.setSheetHidden(book.getSheetIndex(hideSheet), true);

        int rowId = 0;
        // 设置第一行,存省的信息
        Row provinceRow = hideSheet.createRow(rowId++);
        provinceRow.createCell(0).setCellValue("省列表");
        for (int i = 0; i < provinceArr.size(); i++) {
            Cell provinceCell = provinceRow.createCell(i + 1);
            provinceCell.setCellValue(provinceArr.get(i));
        }
        // 将具体的数据写入到每一行中,行开头为父级区域,后面是子区域。
        Iterator keyIterator = areaMap.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            List son = areaMap.get(key);
            Row row = hideSheet.createRow(rowId++);
            row.createCell(0).setCellValue(key);
            for (int i = 0; i < son.size(); i++) {
                Cell cell = row.createCell(i + 1);
                cell.setCellValue(son.get(i));
            }
            // 添加名称管理器
            String range = getRange(1, rowId, son.size());
            Name name = book.createName();
            // key不可重复
            name.setNameName(key);
            String formula = "area!" + range;
            name.setRefersToFormula(formula);
        }

        // 省规则
        DVConstraint provConstraint = DVConstraint.createExplicitListConstraint(provinceArr.toArray(new String[] {}));
        CellRangeAddressList provRangeAddressList = new CellRangeAddressList(1, 20, 0, 0);
        DataValidation provinceDataValidation = new HSSFDataValidation(provRangeAddressList, provConstraint);
        provinceDataValidation.createErrorBox("error", "请选择正确的省份");
        sheetPro.addValidationData(provinceDataValidation);

你可能感兴趣的:(excel,java,开发语言)