Commit b687b9b85023a86827e84c47dc5293881cd944c1

Authored by gaoming
1 parent 62aa3297
Exists in master

.

build.gradle
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.37'
  62 + version '1.1.38'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/UtilExcel.java
... ... @@ -167,6 +167,66 @@ public class UtilExcel {
167 167 }
168 168 }
169 169  
  170 +
  171 + /**
  172 + * 创建并保存excel表 sheet
  173 + * @param sheetName
  174 + * @param data
  175 + * @param path
  176 + */
  177 + public static void saveExcelContailSheet(List<String> sheetList, List<List<List<Object>>> dataList, String path) throws Exception{
  178 + if(sheetList.size() != dataList.size()){
  179 + throw new Exception("sheet size != excel size");
  180 + }
  181 + Workbook wb = UtilExcel.getWorkbook(path, false);
  182 + //创建Excel工作簿对象
  183 +
  184 + for (int j = 0;j< dataList.size(); j++) {
  185 + List<List<Object>> data = dataList.get(j);
  186 + Sheet sheet = wb.createSheet(sheetList.get(j));//创建Excel工作表对象
  187 + for(int i=0; i<data.size(); ++i){
  188 + Row row = sheet.createRow(i); //创建Excel工作表的行
  189 + List<Object> dataRow = data.get(i);
  190 + if(dataRow != null){
  191 + for(int k=0; k<dataRow.size(); ++k){
  192 + Cell cell = row.createCell(k);
  193 + Object dataCell = dataRow.get(k);
  194 + if(dataCell != null){
  195 + if(dataCell.getClass().isPrimitive()){
  196 + cell.setCellValue(Double.valueOf(dataCell.toString()));
  197 + }else if(dataCell.getClass().getSimpleName().equals("Date")){
  198 + cell.setCellValue((Date)dataCell);
  199 + }else{
  200 + cell.setCellValue(dataCell.toString());
  201 + }
  202 + }
  203 + }
  204 + }
  205 + }
  206 + }
  207 +
  208 + try {
  209 + FileOutputStream fileOut;
  210 + File tempFile = new File(path);
  211 + if(!tempFile.exists()){
  212 + File parentFile = tempFile.getParentFile();
  213 + if(!parentFile.exists()){
  214 + parentFile.mkdirs();
  215 + }
  216 + if(!tempFile.createNewFile()){
  217 + return;
  218 + }
  219 + }
  220 + fileOut = new FileOutputStream(tempFile);
  221 + wb.write(fileOut);
  222 + wb.close();
  223 + fileOut.close();
  224 + } catch (Exception e) {
  225 + // TODO Auto-generated catch block
  226 + e.printStackTrace();
  227 + }
  228 + }
  229 +
170 230 /**
171 231 * 创建并保存excel表
172 232 * @param sheetName
... ... @@ -444,18 +504,18 @@ public class UtilExcel {
444 504  
445 505 public static void main(String args[]){
446 506 //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx";
447   - String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xls";
448   - List<List<Object>> data = null;
449   -
450   - try {
451   - data = UtilExcel.readExcel(filepath);
452   - System.out.println(data);
453   - System.out.println(data.size());
454   - System.out.println(UtilExcel.readExcelAllSheetMap(filepath));
455   - } catch (Exception e) {
456   - // TODO Auto-generated catch block
457   - e.printStackTrace();
458   - }
  507 +// String filepath = "C:\\Users\\gaoming\\Desktop\\qwer.xls";
  508 +// List<List<Object>> data = null;
  509 +//
  510 +// try {
  511 +// data = UtilExcel.readExcelAllSheetMap(filepath).get("0");
  512 +// System.out.println(data);
  513 +// System.out.println(data.size());
  514 +// System.out.println(UtilExcel.readExcelAllSheetMap(filepath));
  515 +// } catch (Exception e) {
  516 +// // TODO Auto-generated catch block
  517 +// e.printStackTrace();
  518 +// }
459 519 // List<Short> styleList = new ArrayList<Short>();
460 520 // for(int i=0; i<data.size(); ++i){
461 521 // if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index));
... ... @@ -465,11 +525,33 @@ public class UtilExcel {
465 525 // }
466 526 // System.out.println("");
467 527 // }
468   -// try {
469   -// UtilExcel.saveExcel("测试", data, "D:\\12345.xlsx", styleList);
470   -// } catch (Exception e) {
471   -// // TODO Auto-generated catch block
472   -// e.printStackTrace();
473   -// }
  528 + try {
  529 + List<String> headerList = new ArrayList<String>();
  530 + headerList.add("shhe1");
  531 + headerList.add("shhe2");
  532 + List<List<List<Object>>> dataList = new ArrayList<List<List<Object>>>();
  533 + List<List<Object>> list1 = new ArrayList<List<Object>>();
  534 + List<Object> list11 = new ArrayList<Object>();
  535 + list11.add("hahaha");
  536 + list11.add("hahaha");
  537 + list11.add("hahaha");
  538 + list1.add(list11);
  539 +
  540 + List<List<Object>> list2 = new ArrayList<List<Object>>();
  541 + List<Object> list22 = new ArrayList<Object>();
  542 + list22.add("hahaha2");
  543 + list22.add("hahaha2");
  544 + list22.add("hahaha2");
  545 + list2.add(list22);
  546 + list2.add(list22);
  547 +
  548 + dataList.add(list1);
  549 + dataList.add(list2);
  550 +
  551 + UtilExcel.saveExcelContailSheet(headerList, dataList, "C:\\Users\\gaoming\\Desktop\\qwer.xls");
  552 + } catch (Exception e) {
  553 + // TODO Auto-generated catch block
  554 + e.printStackTrace();
  555 + }
474 556 }
475 557 }
... ...