1. 程式人生 > >java 讀寫 excle 完整版

java 讀寫 excle 完整版

pom.xml

    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- 處理excel和上面功能是一樣的-->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.10</version>
        </dependency>

程式碼部分

  1 package com.example.demo;
  2 
  3 import org.apache.log4j.Logger;
  4 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6 import org.apache.poi.ss.usermodel.*;
  7 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
8 9 import java.io.*; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 15 16 public class ExcelReaderWrite { 17 private static final String EXCEL_XLS = "xls"; 18 private static final String EXCEL_XLSX = "xlsx"; 19
private static String filePath; 20 private static String sheetName; 21 private static Workbook workBook; 22 private static Sheet sheet; 23 public Object[][] results; 24 private List<List<String>> listData; 25 ExcelReaderWrite(String filePath, String sheetName) { 26 this.filePath = filePath; 27 this.sheetName = sheetName; 28 innit(); 29 } 30 31 public void innit() { 32 workBook = getWorkbok(); 33 System.out.println(workBook.toString()); 34 sheet = workBook.getSheet(sheetName); 35 System.out.println(sheet.toString()); 36 } 37 38 public Workbook getWorkbok() { 39 try { 40 File file = new File(filePath); 41 if (file.exists()) { 42 FileInputStream in = new FileInputStream(file); 43 if (file.getName().endsWith(EXCEL_XLS)) { //Excel&nbsp;2003 44 return new HSSFWorkbook(in); 45 } else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010 46 return new XSSFWorkbook(in); 47 } 48 } else { 49 System.out.println(filePath + "不存在 !"); 50 } 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 return null; 55 } 56 57 public void setCellData(int rowNum, int colNum, String content) { 58 rowNum += 1; 59 colNum += 1; 60 FileOutputStream out = null; 61 if (null == sheet.getRow(rowNum)) { 62 Row row = sheet.createRow(rowNum); 63 if (null == row.getCell(colNum)) { 64 row.createCell(colNum).setCellValue(content); 65 } else { 66 row.getCell(colNum).setCellValue(content); 67 } 68 } else { 69 sheet.getRow(rowNum).createCell(colNum).setCellValue(content); 70 } 71 72 try { 73 out = new FileOutputStream(filePath); 74 workBook.write(out); 75 out.flush(); 76 77 } catch (IOException e) { 78 e.printStackTrace(); 79 } finally { 80 try { 81 out.flush(); 82 out.close(); 83 System.out.println("-----寫入成功!------"); 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 87 } 88 89 } 90 91 private String getCellValue(Cell cell) { 92 String cellValue = ""; 93 DataFormatter formatter = new DataFormatter(); 94 if (cell != null) { 95 switch (cell.getCellType()) { 96 case Cell.CELL_TYPE_NUMERIC: 97 if (HSSFDateUtil.isCellDateFormatted(cell)) { 98 cellValue = formatter.formatCellValue(cell); 99 } else { 100 double value = cell.getNumericCellValue(); 101 int intValue = (int) value; 102 cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value); 103 } 104 break; 105 case Cell.CELL_TYPE_STRING: 106 cellValue = cell.getStringCellValue(); 107 break; 108 case Cell.CELL_TYPE_BOOLEAN: 109 cellValue = String.valueOf(cell.getBooleanCellValue()); 110 break; 111 case Cell.CELL_TYPE_FORMULA: 112 cellValue = String.valueOf(cell.getCellFormula()); 113 break; 114 case Cell.CELL_TYPE_BLANK: 115 cellValue = ""; 116 break; 117 case Cell.CELL_TYPE_ERROR: 118 cellValue = ""; 119 break; 120 default: 121 cellValue = cell.toString().trim(); 122 break; 123 } 124 } 125 return cellValue.trim(); 126 } 127 128 public String getCellData(String sheetName, int rowNum, int colNum) { 129 if (rowNum <= 0 || colNum <= 0) { 130 return null; 131 } else if (workBook.getSheet(sheetName) == null || workBook.getSheet(sheetName).getRow(rowNum - 1) == null) { 132 133 return filePath + " 中 " + sheetName + " 不存在,或者" + rowNum + " 行不存在 ! "; 134 } else { 135 return getCellValue(workBook.getSheet(sheetName).getRow(rowNum - 1).getCell(colNum - 1)); 136 } 137 138 } 139 public String getCellData(int rowNum, int colNum) { 140 if (rowNum <= 0 || colNum <= 0) { 141 return null; 142 } else if (workBook.getSheet(sheetName) == null || workBook.getSheet(sheetName).getRow(rowNum - 1) == null) { 143 144 return filePath + " 中 " + sheetName + " 不存在,或者" + rowNum + " 行不存在 ! "; 145 } else { 146 return getCellValue(workBook.getSheet(sheetName).getRow(rowNum - 1).getCell(colNum - 1)); 147 } 148 149 } 150 151 private List<List<String>> getSheetData() { 152 listData = new ArrayList<>(); 153 int numOfRows = sheet.getLastRowNum(); 154 for (int i = 0; i < numOfRows; i++) { 155 Row row = sheet.getRow(i); 156 Map<String, String> map = new HashMap<>(); 157 List<String> list = new ArrayList<>(); 158 if (row != null) { 159 for (int j = 0; j < row.getLastCellNum(); j++) { 160 Cell cell = row.getCell(j); 161 list.add(this.getCellValue(cell)); 162 } 163 } 164 listData.add(list); 165 } 166 167 return listData ; 168 } 169 170 public void printSheetData() { 171 // 測試資料excel資料用 ; 172 List<List<String>> list = getSheetData(); 173 for (int i = 0; i < list.size(); i++) { 174 System.out.println("第 "+(i+1)+" 行有 "+list.get(i).size()+" 單元格有值 : "+list.get(i).toString()); 175 } 176 } 177 178 public static void main(String[] args) { 179 String filePath_1 = "D:/writeExcel.xlsx"; 180 String filePath_2 = "D:/writeExcel97.xls"; 181 String sheetName = "Sheet1"; 182 /* int lastNum_1 = new ExcelReaderWrite(filePath_1, sheetName).getWorkbok().getSheet(sheetName).getLastrowNum(); 183 int lastNum_2 = new ExcelReaderWrite(filePath, sheetName).getWorkbok().getSheet(sheetName).getLastrowNum(); 184 System.out.println(lastNum_1); 185 System.out.println(lastNum_2); 186 for (int i = 0; i <10 ; i++) { 187 new ExcelReaderWrite(filePath_1, sheetName).setCellData(i,3,filePath_1+"_"+String.valueOf(System.currentTimeMillis())); 188 new ExcelReaderWrite(filePath_2, sheetName).setCellData(i,3,filePath_2+"_"+String.valueOf(System.currentTimeMillis())); 189 } 190 String dataValue= new ExcelReaderWrite(filePath_1, sheetName).getCellData(sheetName, 1, 1); 191 System.out.println(dataValue); 192 */ 193 194 new ExcelReaderWrite(filePath_1, sheetName).printSheetData(); 195 new ExcelReaderWrite(filePath_2, sheetName).printSheetData(); 196 } 197 198 199 }
View Code