贴代码:
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import java.io.FileOutputStream;
import java.util.*;
public class test{
public static void testCascade2003() {
// 查询所有的省名称
List provNameList = new ArrayList();
provNameList.add("安徽省");
provNameList.add("浙江省");
// 整理数据,放入一个Map中,mapkey存放父地点,value 存放该地点下的子区域
Map> siteMap = new HashMap<>();
siteMap.put("浙江省", Lists.newArrayList("杭州市", "宁波市"));
siteMap.put("安徽省", Lists.newArrayList("芜湖市", "滁州市"));
siteMap.put("芜湖市", Lists.newArrayList("戈江区", "三山区"));
siteMap.put("滁州市", Lists.newArrayList("来安县", "凤阳县"));
// 创建一个excel
Workbook book = new HSSFWorkbook();
// 创建一个专门用来存放地区信息的隐藏sheet页
Sheet hideSheet = book.createSheet("site_sheet");
book.setSheetHidden(book.getSheetIndex("site_sheet"), true);
int rowId = 0;
// 设置第一行,存省的信息
Row proviRow = hideSheet.createRow(rowId++);
proviRow.createCell(0).setCellValue("省列表");
for (int i = 0; i < provNameList.size(); i++) {
Cell proviCell = proviRow.createCell(i + 1);
proviCell.setCellValue(provNameList.get(i));
}
// 将具体的数据写入到每一行中,行开头为父级区域,后面是子区域。
for (String key : siteMap.keySet()) {
List son = siteMap.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();
name.setNameName(key);
String formula = "site_sheet!" + range;
name.setRefersToFormula(formula);
}
// 创建需要用户填写的数据页
// 设计表头
Sheet sheet1 = book.createSheet("sheet1");
Row row0 = sheet1.createRow(0);
row0.createCell(0).setCellValue("省");
row0.createCell(1).setCellValue("市");
row0.createCell(2).setCellValue("区");
// 省规则
DVConstraint provConstraint = DVConstraint.createExplicitListConstraint(provNameList.toArray(new String[] {}));
CellRangeAddressList provRangeAddressList = new CellRangeAddressList(1, 1, 0, 0);
DataValidation provinceDataValidation = new HSSFDataValidation(provRangeAddressList, provConstraint);
provinceDataValidation.createErrorBox("error", "请选择正确的省份");
sheet1.addValidationData(provinceDataValidation);
// 市以规则,此处仅作一个示例
// "INDIRECT($A$" + 2 + ")" 表示规则数据会从名称管理器中获取key与单元格 A2 值相同的数据,如果A2是浙江省,那么此处就是
// 浙江省下的区域信息。
DVConstraint formula = DVConstraint.createFormulaListConstraint("INDIRECT($A$" + 2 + ")");
CellRangeAddressList rangeAddressList = new CellRangeAddressList(1, 10, 1, 1);
DataValidation cacse = new HSSFDataValidation(rangeAddressList, formula);
cacse.createErrorBox("error", "请选择正确的市");
sheet1.addValidationData(cacse);
// 区规则
formula = DVConstraint.createFormulaListConstraint("INDIRECT($B$" + 2 + ")");
rangeAddressList = new CellRangeAddressList(1, 10, 2, 2);
cacse = new HSSFDataValidation(rangeAddressList, formula);
cacse.createErrorBox("error", "请选择正确的区");
sheet1.addValidationData(cacse);
book.setActiveSheet(1);
FileOutputStream os = null;
try {
os = new FileOutputStream("D:/testCascade2003.xls");
book.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(os);
}
}
/**
*
* @param offset 偏移量,如果给0,表示从A列开始,1,就是从B列
* @param rowId 第几行
* @param colCount 一共多少列
* @return 如果给入参 1,1,10. 表示从B1-K1。最终返回 $B$1:$K$1
*
* @author denggonghai 2016年8月31日 下午5:17:49
*/
public static String getRange(int offset, int rowId, int colCount) {
char start = (char)('A' + offset);
if (colCount <= 25) {
char end = (char)(start + colCount - 1);
return "$" + start + "$" + rowId + ":$" + end + "$" + rowId;
} else {
char endPrefix = 'A';
char endSuffix = 'A';
if ((colCount - 25) / 26 == 0 || colCount == 51) {// 26-51之间,包括边界(仅两次字母表计算)
if ((colCount - 25) % 26 == 0) {// 边界值
endSuffix = (char)('A' + 25);
} else {
endSuffix = (char)('A' + (colCount - 25) % 26 - 1);
}
} else {// 51以上
if ((colCount - 25) % 26 == 0) {
endSuffix = (char)('A' + 25);
endPrefix = (char)(endPrefix + (colCount - 25) / 26 - 1);
} else {
endSuffix = (char)('A' + (colCount - 25) % 26 - 1);
endPrefix = (char)(endPrefix + (colCount - 25) / 26);
}
}
return "$" + start + "$" + rowId + ":$" + en````````
Prefix + endSuffix + "$" + rowId;
}
}
public static void main(String[] args) {
testCascade2003();
}
}
Excel到底有多强大 0.0