1. 程式人生 > >POI解析文件內容(txt,doc,docx,xls,xlsx,ppt,pdf)

POI解析文件內容(txt,doc,docx,xls,xlsx,ppt,pdf)

/**
	 * 讀取xls檔案內容
	 *
	 * @param file
	 *            想要讀取的檔案物件
	 * @return 返回檔案內容
	 */
	public static String xls2String(File file) {
		String result = "";
		try {
			FileInputStream fis = new FileInputStream(file);
			StringBuilder sb = new StringBuilder();
			jxl.Workbook rwb = Workbook.getWorkbook(fis);
			Sheet[] sheet = rwb.getSheets();
			for (int i = 0; i < sheet.length; i++) {
				Sheet rs = rwb.getSheet(i);
				for (int j = 0; j < rs.getRows(); j++) {
					Cell[] cells = rs.getRow(j);
					for (int k = 0; k < cells.length; k++)
						sb.append(cells[k].getContents());
				}
			}
			fis.close();
			result += sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

POI解析xlsx文件內容: