java如何讀取、寫入Excel文件(將內容轉成任意的bean對象)

將StudentInfo.xlsx裏的內容讀取出來並封裝到StudentInfo類的對象中

StudentInfo類的字段分別對應了excel中的列名,合成結果如下

 

利用反射可以很好地實現對任意類的數據封裝。實現如下

public class ExcelUtil {
 
	public static <T> List<T> parseFromExcel(String path, Class<T> aimClass) {
		return parseFromExcel(path, 0, aimClass);
	}
 
	@SuppressWarnings("deprecation")
	public static <T> List<T> parseFromExcel(String path, int firstIndex, Class<T> aimClass) {
		List<T> result = new ArrayList<T>();
		try {
			FileInputStream fis = new FileInputStream(path);
			Workbook workbook = WorkbookFactory.create(fis);
            //對excel文檔的第一頁,即sheet1進行操作
			Sheet sheet = workbook.getSheetAt(0);
			int lastRaw = sheet.getLastRowNum();
			for (int i = firstIndex; i < lastRaw; i++) {
                //第i行
				Row row = sheet.getRow(i);
				T parseObject = aimClass.newInstance();
				Field[] fields = aimClass.getDeclaredFields();
				for (int j = 0; j < fields.length; j++) {
					Field field = fields[j];
					field.setAccessible(true);
					Class<?> type = field.getType();
                    //第j列
					Cell cell = row.getCell(j);
					if (cell == null)
						continue;
                    //很重要的一行代碼,如果不加,像12345這樣的數字是不會給你轉成String的,只會給你轉成double,而且會導致cell.getStringCellValue()報錯
					cell.setCellType(Cell.CELL_TYPE_STRING);
					String cellContent = cell.getStringCellValue();
					cellContent = "".equals(cellContent) ? "0" : cellContent;
					if (type.equals(String.class)) {
						field.set(parseObject, cellContent);
					} else if (type.equals(char.class) || type.equals(Character.class)) {
						field.set(parseObject, cellContent.charAt(0));
					} else if (type.equals(int.class) || type.equals(Integer.class)) {
						field.set(parseObject, Integer.parseInt(cellContent));
					} else if (type.equals(long.class) || type.equals(Long.class)) {
						field.set(parseObject, Long.parseLong(cellContent));
					} else if (type.equals(float.class) || type.equals(Float.class)) {
						field.set(parseObject, Float.parseFloat(cellContent));
					} else if (type.equals(double.class) || type.equals(Double.class)) {
						field.set(parseObject, Double.parseDouble(cellContent));
					} else if (type.equals(short.class) || type.equals(Short.class)) {
						field.set(parseObject, Short.parseShort(cellContent));
					} else if (type.equals(byte.class) || type.equals(Byte.class)) {
						field.set(parseObject, Byte.parseByte(cellContent));
					} else if (type.equals(boolean.class) || type.equals(Boolean.class)) {
						field.set(parseObject, Boolean.parseBoolean(cellContent));
					}
				}
				result.add(parseObject);
			}
			fis.close();
			return result;
		} catch (
 
		Exception e) {
			e.printStackTrace();
			System.err.println("An error occured when parsing object from Excel. at " + this.getClass());
		}
		return result;
	}
 
 
	public static void main(String[] args) throws Exception {
         //參數裏的5表示有效行數從第5行開始
		 List<StudentInfo> studentInfos = ExcelUtil.parseFromExcel("C:\\Users\\unive\\Desktop\\StudentInfo.xlsx", 5,
		 StudentInfo.class);
		 for (int i = 0; i < studentInfos.size(); i++) {
		     System.err.println(studentInfos.get(i).toString());
                 }
         }
  }

 

引用的jar包是Apache POI,版本3.17.x,更新於2017年9月,使用的人數是近年來最多的,Maven座標如下

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

-----------------補充------------------

雖然是很久前的文章了,當時只寫了個讀入的,後面又寫了個寫入的,用了一年多了,確實挺方便,今天正好有空,補上來

public class ExcelUtil {
    
    // 帶標題寫入Excel
	public static <T> void writeExcelWithTitle(List<T> beans, String path) {
		writeExcel(beans,path,true);		
	}
	
	// 僅把數據寫入Excel
	public static <T> void writeExcel(List<T> beans, String path) {
		writeExcel(beans,path,false);
	}
	
	private static <T> void writeExcel(List<T> beans, String path, boolean writeTitle) {
		if(beans == null || beans.size() == 0) return;
		Workbook workbook = new HSSFWorkbook();
		FileOutputStream fos = null;
		int offset = writeTitle ? 1 : 0;
		try {
			Sheet sheet = workbook.createSheet();
			for (int i = 0; i < beans.size() + offset; ++i) {
				if(writeTitle && i == 0) {createTitle(beans, sheet);continue;}
				Row row = sheet.createRow(i);
				T bean = beans.get(i - offset);
				Field[] fields = bean.getClass().getDeclaredFields();
				for (int j = 0; j < fields.length; j++) {
					Field field = fields[j];
					field.setAccessible(true);
					Cell cell = row.createCell(j);
					//Date,Calender都可以 使用  +"" 操作轉成字符串
					cell.setCellValue(field.get(bean)+"");
				}
			}
		    fos = new FileOutputStream(path);
			workbook.write(fos);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				fos.close();
				workbook.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static <T> void createTitle(List<T> beans,Sheet sheet){ 
		Row row = sheet.createRow(0);
		T bean = beans.get(0);
		Field[] fields = bean.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			field.setAccessible(true);
			Cell cell = row.createCell(i);
			cell.setCellValue(field.getName());
		}
	}

    public static void main(String[] args) {
        // scenics這裏是自定義景點集合, 不用管
        // 帶標題寫入excel
        ExcelUtil.writeExcelWithTitle(scenics,
				"C:\\Users\\unive\\Documents\\景點信息\\scenics.xlsx");

        // 不帶標題寫入excel
        ExcelUtil.writeExcel(scenics,
				"C:\\Users\\unive\\Documents\\景點信息\\scenicsWithoutTitle.xlsx");
    }	
}

帶標題寫入excel的結果

不帶標題寫入的結果

不過值得一提的是,直接將生成的excel通過navicat導入數據庫會報錯,因爲生成的excel似乎沒有官方的那麼標準,所以解決辦法是新建一個excel文件,把數據複製過來,再讓navicat把這個新建的excel導入到數據庫即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章