中间使用map作为中间处理

将类处理为excel:

1.读取类转为map

//读取btl,转为map
public static Map getBtlMap(String rule, BTLDAO binFile) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i;
Object om;
Map map = null;
List<Map<String, Object>> tempMaps;
Map<String, Object> rsMap = new HashMap();
Object s;
JSONObject row; {//遍历btl
Field[] fields = binFile.getClass().getDeclaredFields();//Object是已经被赋值的对象实例
for (Field field : fields) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
//如果是list类
if (List.class.isAssignableFrom(field.getType())) {
Type t = field.getGenericType();
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Class clz = (Class) pt.getActualTypeArguments()[0];//得到对象list中实例的类型
if (field.get(binFile) != null) {
Class clazz = field.get(binFile).getClass();//获取到属性的值的Class对象
Method m = clazz.getDeclaredMethod("size");
int size = (Integer) m.invoke(field.get(binFile));//调用list的size方法,得到list的长度
tempMaps = new ArrayList<Map<String, Object>>();
for (int i2 = 0; i2 < size; i2++) {//遍历list,调用get方法,获取list中的对象实例
Method getM = clazz.getDeclaredMethod("get", int.class);
if (!getM.isAccessible()) {
getM.setAccessible(true);
s = getM.invoke(field.get(binFile), i2);
map = ComUtil.getKeyAndValue(s);
tempMaps.add(map);
}
}
rsMap.put(field.getName(), tempMaps);
}
}
} else {//否则为普通类
field.setAccessible(true);
Object value = field.get(binFile);
map = ComUtil.getKeyAndValue(value);
rsMap.put(field.getName(), map);
}
}
}
{//检查类数量
String[] cutStrs = new String[] { "bm0", "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" };
for (i = 0; i < cutStrs.length; i++) {
row = getInfoByRootName(rule, cutStrs[i]);
if (row != null && row.getBoolean("ifCycle")) {
if (!(row.getString("Count").equals("one") && row.getString("Count").equals("sumGride"))) {
map.put(row.getString("Count"), ComUtil.getArrayListCapacity((ArrayList<?>) rsMap.get(cutStrs[i])));
// System.out.println(row.getString("Count")+":"+":"+ComUtil.getArrayListCapacity((ArrayList<?>) rsMap.get(cutStrs[i])));
}
}
}
} return rsMap;
}

读取类转为map

2.将map转为excel

 //rule 规则
//map 类
//folder 文件夹
//fileName
public static boolean writeBTLExcel(String folder, String fileName, String rule, Map map) {
if (ComUtil.isEmpty(folder) || ComUtil.isEmpty(fileName)) {
return false;
}
// 判断路径是否正确
File file = new File(folder);
if (!file.isDirectory()) {
return false;
}
// 设置文件后缀
if (!fileName.endsWith(".xls")) {
fileName = fileName + ".xls";
} String[] cutStrs = new String[] { "bm0", "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" };
JSONObject jsonRow;
List<DefRule> rs;
List<Map> list;
Map m;
HSSFSheet sheet;
HSSFRow row, row1;
List<String> data;
HSSFPatriarch p;
HSSFComment comment;
// 第一步,创建一个workbook对应一个excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//1加载规则,开始循环
for (int w = 0; w < cutStrs.length; w++) {
jsonRow = getInfoByRootName(rule, cutStrs[w]);
if (jsonRow != null) {
rs = getDefRuleInfosByRow(jsonRow);
if (jsonRow.get("ifCycle").equals("true")) {
list = (List<Map>) map.get(cutStrs[w]);
// 第二步,在workbook中创建一个sheet对应excel中的sheet
sheet = workbook.createSheet(cutStrs[w]);
//绘制批注
p=sheet.createDrawingPatriarch();
// 第三步,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
row = sheet.createRow(0);
// 第四步,创建单元格,设置表头
for (int i = 0; i < rs.size(); i++) {
//添加备注
if(!rs.get(i).getRemark().equals("?")) {
comment=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6));
//输入批注信息
comment.setString(new HSSFRichTextString(rs.get(i).getRemark()));
row.createCell(i).setCellComment(comment);
}
row.createCell(i).setCellValue(rs.get(i).getId());
}
// 第五步,写入实体数据,实际应用中这些数据从数据库得到,对象封装数据,集合包对象。对象的属性值对应表的每行的值
for (int i = 0; i < list.size(); i++) {
row1 = sheet.createRow(i + 1);
// 创建单元格设值
for (int j = 0; j < rs.size(); j++) {
if (list.get(i).containsKey(rs.get(j).getId())) {
row1.createCell(j).setCellValue(list.get(i).get(rs.get(j).getId()).toString());
}
}
}
} else {
if (map.containsKey(cutStrs[w])) {
m = (Map) map.get(cutStrs[w]);
// 第二步,在workbook中创建一个sheet对应excel中的sheet
sheet = workbook.createSheet(cutStrs[w]);
//绘制批注
p=sheet.createDrawingPatriarch();
// 第三步,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
row = sheet.createRow(0);
row1 = sheet.createRow(1);
// 第四步,创建单元格,设置表头
for (int i = 0; i < rs.size(); i++) { //添加备注
if(!rs.get(i).getRemark().equals("?")) {
comment=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6));
//输入批注信息
comment.setString(new HSSFRichTextString(rs.get(i).getRemark()));
row.createCell(i).setCellComment(comment);
}
row.createCell(i).setCellValue(rs.get(i).getId());
row1.createCell(i).setCellValue(m.get(rs.get(i).getId()).toString());
}
}
}
} }
// 将文件保存到指定的位置
try {
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(folder + "\\" + fileName);
workbook.write(fos);
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

将map转为excel

将excel处理为类

1.读取excel,处理为map

//格式表头必须占一行!
public static Map getExcelDataForMap(File file)throws FileNotFoundException, IOException { int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
Map<String, Object> rsMap = new HashMap();
Map tempMap = null;
List<Map<String, Object>> tempMaps = null; POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;HSSFCell headCell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行为标题,不取
tempMaps = new ArrayList<Map<String, Object>>();
for (int rowIndex = 1; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow headR = st.getRow(0);
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize; }
boolean hasValue = false;
tempMap = new HashMap();
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
String headV = "";
cell = row.getCell(columnIndex);
headCell=headR.getCell(columnIndex);
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break; case HSSFCell.CELL_TYPE_BLANK:
break; case HSSFCell.CELL_TYPE_ERROR:
value = "";
break; case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y": "N");
break;
default:
value = "";
}
}
if (headCell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (headCell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
headV = headCell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(headCell)) {
Date date = headCell.getDateCellValue();
if (date != null) {
headV = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
headV = "";
}
} else {
value = new DecimalFormat("0").format(headCell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!headCell.getStringCellValue().equals("")) {
headV = cell.getStringCellValue();
} else {
headV = cell.getNumericCellValue() + "";
}
break; case HSSFCell.CELL_TYPE_BLANK:
break; case HSSFCell.CELL_TYPE_ERROR:
value = "";
break; case HSSFCell.CELL_TYPE_BOOLEAN:
headV = (cell.getBooleanCellValue() == true ? "Y": "N");
break;
default:
headV = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
hasValue = true;
tempMap.put(new String(ComUtil.rightTrim(headV)),new String(ComUtil.rightTrim(value)));
}
if (hasValue) {
tempMaps.add(tempMap);
}
}
rsMap.put(wb.getSheetName(sheetIndex), tempMaps);
}
in.close();
//把bm0的格式转为通用map的格式而非list
rsMap.put("bm0", ((List)rsMap.get("bm0")).get(0));
return rsMap;
}

读取excel,处理为map

2.读取map还原为类

public static BTLDAO getBtlByExcelMap(String rule,Map rsMaps) throws Exception {
BTLDAO btl=new BTLDAO();
BtlModule0 bi;
StringBuilder buf = new StringBuilder();
String cutStr = "";
int cutSumCt = 1;//总循环次数
int mapW = 0, mapH = 0, i,j;
JSONObject row;
List<DefRule> rs;
Map biMap = null;
Object rsO = null;
List list; { //得到基础信息
cutStr = "bm0";
row = getInfoByRootName(rule, cutStr);
rs = getDefRuleInfosByRow(row);
bi = new BtlModule0();
biMap = (Map) (rsMaps.get(cutStr));
bi=(BtlModule0) ComUtil.mapToJBean(BtlModule0.class, biMap);
btl.setBm0(bi);
biMap = ComUtil.JBeanToMap(bi);
}
{ //重复读取所有基本信息
Object[] objects = new Object[] { (new BtlModule1()), (new BtlModule2()), (new BtlModule3()), (new BtlModule4()), (new BtlModule5()), (new BtlModule6()), (new BtlModule7()), (new BtlModule8()), (new BtlModule9()), (new BtlModule10()), (new BtlModule11()), (new BtlModule12()), (new BtlModule13()), (new BtlModule14()), (new BtlModule15()), (new BtlModule16()), (new BtlModule17()),
(new BtlModule18()), (new BtlModule19()), (new BtlModule20()) };
String[] cutStrs = new String[] { "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" };
for (i = 0; i < cutStrs.length; i++) {
row = getInfoByRootName(rule, cutStrs[i]);
if (row != null) { //List<DefRule> rs, List list,Map map
rs = getDefRuleInfosByRow(row);
list=(List)rsMaps.get(cutStrs[i]); // getBtlMoudleList(List<DefRule> rs, List list,Map map)
ComUtil.setVal(btl, "set" + ComUtil.UpperInitial(cutStrs[i]), getBtlMoudleList(rs,list,objects[i]));
//ComUtil.setVal(btl, "set" + ComUtil.UpperInitial(cutStrs[i]), rsMap.get("T")); }
}
}
return btl; }

读取map还原为类

12 将类处理为excel,再将excel处理为类(界限计划3)的更多相关文章

  1. 爬取表格类网站数据并保存为excel文件

    本文转载自以下网站:50 行代码爬取东方财富网上市公司 10 年近百万行财务报表数据 https://www.makcyun.top/web_scraping_withpython6.html 主要学 ...

  2. ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类

    ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:读取文本文件,常见多边形要素 ...

  3. 按键精灵如何批量复制文本,再往excel里面一次性粘贴?

    原帖地址 http://zhidao.baidu.com/link?url=M2A9E1JF7wAzjtxMQG9uiW_PvP39HVlfwn6zDMzk9m6U05JA37SrgDcrVXg_c9 ...

  4. ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel

    方法一: 1. 下载PHPExcel并保存在如下位置: 2. 在控制器中引用 vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel ...

  5. thinkphp用phpexcel读取excel,并修改列中的值,再导出excel,带往excel里写入图片

    <?php class GetpriceAction extends AdministratorAction { // 文件保存路径 protected $savepath; // 允许上传的文 ...

  6. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  7. NPOI操作excel之读取excel数据

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.  一.下载引用 去NPOI官网http://npoi.codeplex. ...

  8. 基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------> 可以返回派生类对象的引用或指针

      您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. ...

  9. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

随机推荐

  1. Java中字符串为什么不以\0结尾

    Java中字符串为什么不以\0结尾 其实这个问题没有什么好说的,Java里面一切都是对象,是对象的话,字符串肯定就有长度,即然有长度,编译器就可以确定要输出的字符个数,当然也就没有必要去浪费那1字节的 ...

  2. hql 条件查询 返回空的一种情况

    为何会出现查询为空,但是查询整个表没问题的情况呢? 这里是没有分清字符串和变量 原来写的是, String hql = "from ClientInfoModel where clientI ...

  3. 2019.10.22 csp-s模拟测试82 反思总结

    重来重来,刚刚就当什么都没发生 今天的题属实有些迷惑,各种意义上…总之都很有难度吧.不满归不满,这套题的确不是什么没有意义的题目. 为了考验自己的学习能力记忆力,决定不写题解,扔个代码完事了 其实是懒 ...

  4. case 和decode的区别

    区别: decode是pl/sql语法,只能在oracle中使用,case when是标准SQL的语法,哪儿都能用,也就是说移植性更强. decode像是case when的精简版,当要实现的功能比较 ...

  5. android中的http框架,使其更加简单易用

    Afinal 是一个android的sqlite orm 和 ioc 框架. Afinal 是一个android的sqlite orm 和 ioc 框架.同时封装了android中的http框架,使其 ...

  6. Hdu 1068 最小路径覆盖

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. Kafka 集群安装

    Kafka 集群安装 环境: Linux 7.X kafka_2.x 在linux操作系统中,kafka安装在 /u04/app目录中 1. 下载 # wget https://mirrors.cnn ...

  8. Python数据分析与展示[第三周](pandas数据类型操作)

    数据类型操作 如何改变Series/ DataFrame 对象 增加或重排:重新索引 删除:drop 重新索引 .reindex() reindex() 能够改变或重排Series和DataFrame ...

  9. system_service进程里 调用SystemManager.getService("activity") 直接返回ams的引用?

    我们知道ActivityManager是运行在system_service进程里的,但是最近看代码发现在这个进程的其他服务线程里为了获取AMS调用: ActivityManagerService am ...

  10. qt获取本机用户名

    //获取用户名 QString getUserName() { #if 1 QStringList envVariables; envVariables << "USERNAME ...