1. 程式人生 > >poi實現excel讀取,實現增加超連結,對於office 2007完美支援

poi實現excel讀取,實現增加超連結,對於office 2007完美支援

 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {
	private static String findFileName;
	private static String fileName;
	private static String findFileInitName;

	/**
	 * 讀取office 2007 xlsx
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	public static void loadXlsx(String filePath) throws IOException {
		// 構造 XSSFWorkbook 物件,strPath 傳入檔案路徑
		XSSFWorkbook xwb = null;
		try {
			xwb = new XSSFWorkbook(filePath);
		} catch (IOException e) {
			System.out.println("讀取檔案出錯");
			e.printStackTrace();
		}
		// 讀取第一章表格內容
		XSSFSheet sheet = xwb.getSheetAt(0);
		// 定義 row、cell
		XSSFRow row;
		File file = null;

		// 迴圈輸出表格中的內容
		for (int i = sheet.getFirstRowNum() + 1; i < sheet
				.getPhysicalNumberOfRows(); i++) {
			row = sheet.getRow(i);
			XSSFCell cell = row.getCell(11);
			if (cell != null) {
				findFile(file,cell);
			}
		}
		FileOutputStream fileOut = new FileOutputStream("d:/2.xlsx");
		xwb.write(fileOut);
		fileOut.close();
	}

	private static void findFile(File file,XSSFCell cell ) {
		// TODO Auto-generated method stub

		fileName = "" + cell.getRawValue();
		findFileInitName = "TEST/專利/" + fileName;

		// excel
		findFileName = findFileInitName + ".xlsx";
		fileName = "d:\\" + findFileName;

		file = new File(fileName + ".xlsx");
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// excel
		findFileName = findFileInitName + ".xls";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// doc
		findFileName = findFileInitName + ".doc";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// docx
		findFileName = findFileInitName + ".docx";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// pdf
		findFileName = findFileInitName + ".pdf";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// zip
		findFileName = findFileInitName + ".zip";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}
		// rar
		findFileName = findFileInitName + ".rar";
		fileName = "d:\\" + findFileName;

		file = new File(fileName);
		if (file.exists()) {
			setLink(cell, findFileName);
			return;
		}

	}

	public static void setLink(XSSFCell cell, String fileName) {
		XSSFHyperlink link = cell.getHyperlink();
		if (link == null) {
			link = new Excel074Link(Excel074Link.LINK_FILE);
			link.setAddress(fileName);
			// cell.setCellFormula("HYPERLINK(/"d:/1.xlsx/")");
			cell.setHyperlink(link);
		}
	}

	public static void main(String[] args) throws IOException {
		loadXlsx("d:\\TEST\\專利資源資訊.xlsx");
	}
}