Commit e622b47678ed04e18dce5323956d7aa76ec13385

Authored by zq
1 parent b3d374a8

excel导出

src/main/java/com/bsth/util/ReportUtils.java
@@ -8,6 +8,8 @@ import java.util.HashMap; @@ -8,6 +8,8 @@ import java.util.HashMap;
8 import java.util.Iterator; 8 import java.util.Iterator;
9 import java.util.List; 9 import java.util.List;
10 import java.util.Map; 10 import java.util.Map;
  11 +import java.util.regex.Matcher;
  12 +import java.util.regex.Pattern;
11 13
12 import org.apache.poi.hssf.usermodel.HSSFCell; 14 import org.apache.poi.hssf.usermodel.HSSFCell;
13 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 15 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@@ -67,8 +69,8 @@ public class ReportUtils { @@ -67,8 +69,8 @@ public class ReportUtils {
67 continue; 69 continue;
68 } 70 }
69 // 取得每列的内容,如果列内容是$key$格式,则替换内容 71 // 取得每列的内容,如果列内容是$key$格式,则替换内容
70 - key = cell.getStringCellValue();  
71 - if (key.indexOf("$") != -1 || key.indexOf("#list#") != -1) { 72 + key = getCellValue(cell);
  73 + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) {
72 // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 74 // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板
73 // * 例如:模板行格式 #list#0_0 $Car.id$ 75 // * 例如:模板行格式 #list#0_0 $Car.id$
74 // * 第一个0表示需要在list中取iterator的索引值 76 // * 第一个0表示需要在list中取iterator的索引值
@@ -88,8 +90,8 @@ public class ReportUtils { @@ -88,8 +90,8 @@ public class ReportUtils {
88 i += rowCount; 90 i += rowCount;
89 break; 91 break;
90 } else { 92 } else {
91 - // 直接填充数据的列,从对象数组中取得值  
92 - getValueAndSetCellValue(cell, key, tArray); 93 + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值
  94 + getValueAndSetCellValue(cell, key, tArray, new String[]{""});
93 } 95 }
94 } 96 }
95 97
@@ -106,6 +108,47 @@ public class ReportUtils { @@ -106,6 +108,47 @@ public class ReportUtils {
106 } 108 }
107 } 109 }
108 110
  111 + public String getCellValue(HSSFCell cell) {
  112 + int cellType = 0;
  113 + String result = "";
  114 + double d;
  115 + if(cell != null) {
  116 + // 获取列数据类型
  117 + cellType = cell.getCellType();
  118 + // 不同列数据类型,取值方法不同
  119 + switch(cellType) {
  120 + // String
  121 + case HSSFCell.CELL_TYPE_STRING:
  122 + result = cell.getStringCellValue().toString();
  123 + break;
  124 + // numeric类型,excel中,日期格式会转成数字格式存储
  125 + case HSSFCell.CELL_TYPE_NUMERIC:
  126 + result = cell.getNumericCellValue()+"";
  127 + break;
  128 + // 公式类型
  129 + case HSSFCell.CELL_TYPE_FORMULA:
  130 + result = cell.getCellFormula() + "";
  131 + break;
  132 + // boolean类型
  133 + case HSSFCell.CELL_TYPE_BOOLEAN:
  134 + result = cell.getBooleanCellValue() + "";
  135 + break;
  136 + // 空格
  137 + case HSSFCell.CELL_TYPE_BLANK:
  138 + result = null;
  139 + break;
  140 + // 错误值
  141 + case HSSFCell.CELL_TYPE_ERROR:
  142 + result = null;
  143 + break;
  144 + default :
  145 + System.out.println("其它");
  146 + break;
  147 + }
  148 + }
  149 + return result;
  150 + }
  151 +
109 /** 152 /**
110 * 根据iterator,以及模板中的标识,填充模板 153 * 根据iterator,以及模板中的标识,填充模板
111 * 154 *
@@ -132,6 +175,7 @@ public class ReportUtils { @@ -132,6 +175,7 @@ public class ReportUtils {
132 HSSFRow newRow = null; 175 HSSFRow newRow = null;
133 int tmpCellNum = 0; 176 int tmpCellNum = 0;
134 int k = 0; 177 int k = 0;
  178 + int listIndex = 0;
135 // 取得模板行 179 // 取得模板行
136 HSSFRow orgRow = sheet.getRow(index); 180 HSSFRow orgRow = sheet.getRow(index);
137 try { 181 try {
@@ -154,7 +198,7 @@ public class ReportUtils { @@ -154,7 +198,7 @@ public class ReportUtils {
154 tmpCellNum = newRow.getLastCellNum(); 198 tmpCellNum = newRow.getLastCellNum();
155 for (int l = 0; l < tmpCellNum; l++) { 199 for (int l = 0; l < tmpCellNum; l++) {
156 cell = newRow.getCell(l); 200 cell = newRow.getCell(l);
157 - key = cell.getStringCellValue(); 201 + key = getCellValue(cell);
158 /** 202 /**
159 * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 203 * 如果单无格内容为#list#,表示该行是模板行 #list#0_0
160 * 第一个0表示需要在list中取iterator的索引值 204 * 第一个0表示需要在list中取iterator的索引值
@@ -170,8 +214,10 @@ public class ReportUtils { @@ -170,8 +214,10 @@ public class ReportUtils {
170 if (key.trim().indexOf(" ") != -1) { 214 if (key.trim().indexOf(" ") != -1) {
171 key = key.split(" ")[1]; 215 key = key.split(" ")[1];
172 } 216 }
173 - getValueAndSetCellValue(cell, key, obj); 217 + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""});
174 } 218 }
  219 + // list的数量
  220 + listIndex ++;
175 } 221 }
176 } catch (Exception e) { 222 } catch (Exception e) {
177 e.printStackTrace(); 223 e.printStackTrace();
@@ -182,45 +228,58 @@ public class ReportUtils { @@ -182,45 +228,58 @@ public class ReportUtils {
182 /** 228 /**
183 * 取到相应的值并填入相应的列中 229 * 取到相应的值并填入相应的列中
184 * 230 *
185 - * @param cell  
186 - * 列  
187 - * @param key  
188 - * 列内容  
189 - * @param obj  
190 - * 数据源对象 231 + * @param cell 列
  232 + * @param key 列内容
  233 + * @param obj 数据源对象
  234 + * @param args 其他参数 数组
  235 + * 数组内容:
  236 + * 0位:在list中的第几条数据
191 */ 237 */
192 - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj) { 238 + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) {
193 try { 239 try {
194 // 保有存单元格的内容 240 // 保有存单元格的内容
195 String cellValue = key = key.replace("\\n", ""); 241 String cellValue = key = key.replace("\\n", "");
196 String tmpKey; 242 String tmpKey;
197 - // 循环截取两个$中间的内容,反射出值  
198 - while (key.indexOf("$") != -1) {  
199 - key = key.substring(key.indexOf("$") + 1);  
200 - // 取两个$中间的内容  
201 - tmpKey = key.substring(0, key.indexOf("$"));  
202 - key = key.substring(key.indexOf("$") + 1);  
203 - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值  
204 - if (tmpKey.indexOf(".") != -1) {  
205 - String className = tmpKey.substring(0, tmpKey.indexOf("."));  
206 - // 取得类的全限定名  
207 - String classWholeName = packaegName + className;  
208 - String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);  
209 - // 如果obj是数组,循环判断哪个对象是对应的  
210 - if (obj instanceof Object[]) {  
211 - Object[] objs = (Object[]) obj;  
212 - for (int k = 0; k < objs.length; k++) {  
213 - if (objs[k].getClass().getName().equals(classWholeName)) {  
214 - cellValue = cellValue.replace("$" + tmpKey  
215 - + "$", getKeyValue(objs[k], fieldName)  
216 - + "");  
217 - } else if(objs[k].getClass().getName().equals("java.util.HashMap")){  
218 - Map<String,Object> map = (HashMap<String,Object>)objs[k];  
219 - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); 243 + // 判断单元格内容是否是公式
  244 + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
  245 + Pattern p=Pattern.compile("(\\d+)"); //
  246 + Matcher m=p.matcher(key);
  247 + if(m.find()){
  248 + cellValue = key.replace(m.group(1),
  249 + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+"");
  250 + cell.setCellFormula(cellValue);
  251 + return;
  252 + }
  253 + }else{//其他格式
  254 +
  255 + // 循环截取两个$中间的内容,反射出值
  256 + while (key.indexOf("$") != -1) {
  257 + key = key.substring(key.indexOf("$") + 1);
  258 + // 取两个$中间的内容
  259 + tmpKey = key.substring(0, key.indexOf("$"));
  260 + key = key.substring(key.indexOf("$") + 1);
  261 + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值
  262 + if (tmpKey.indexOf(".") != -1) {
  263 + String className = tmpKey.substring(0, tmpKey.indexOf("."));
  264 + // 取得类的全限定名
  265 + String classWholeName = packaegName + className;
  266 + String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1);
  267 + // 如果obj是数组,循环判断哪个对象是对应的
  268 + if (obj instanceof Object[]) {
  269 + Object[] objs = (Object[]) obj;
  270 + for (int k = 0; k < objs.length; k++) {
  271 + if (objs[k].getClass().getName().equals(classWholeName)) {
  272 + cellValue = cellValue.replace("$" + tmpKey
  273 + + "$", getKeyValue(objs[k], fieldName)
  274 + + "");
  275 + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){
  276 + Map<String,Object> map = (HashMap<String,Object>)objs[k];
  277 + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+"");
  278 + }
220 } 279 }
  280 + } else if (obj.getClass().getName().equals(classWholeName)) {
  281 + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");
221 } 282 }
222 - } else if (obj.getClass().getName().equals(classWholeName)) {  
223 - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + "");  
224 } 283 }
225 } 284 }
226 } 285 }
@@ -271,16 +330,16 @@ public class ReportUtils { @@ -271,16 +330,16 @@ public class ReportUtils {
271 srr.setXlName("abc11"); 330 srr.setXlName("abc11");
272 331
273 ScheduleRealInfo sr = new ScheduleRealInfo(); 332 ScheduleRealInfo sr = new ScheduleRealInfo();
274 - sr.setId((long) 22);  
275 - sr.setXlName("abc22"); 333 + sr.setZdsj("06:10");
  334 + sr.setZdsjActual("06:25");
276 dataList.add(sr); 335 dataList.add(sr);
277 sr = new ScheduleRealInfo(); 336 sr = new ScheduleRealInfo();
278 - sr.setId((long) 33);  
279 - sr.setXlName("abc33"); 337 + sr.setZdsj("06:20");
  338 + sr.setZdsjActual("");
280 dataList.add(sr); 339 dataList.add(sr);
281 list.add(dataList.iterator()); 340 list.add(dataList.iterator());
282 341
283 - ee.excelReplace(list, new Object[] { srr }, "D:\\55.xls", 342 + ee.excelReplace(list, new Object[] { srr }, "D:\\waybill.xls",
284 "D:\\22.xls"); 343 "D:\\22.xls");
285 System.out.println("ok"); 344 System.out.println("ok");
286 } catch (Exception e) { 345 } catch (Exception e) {