2008年2月4日 星期一

POI 讀取簡體 excel

POI 讀取 excel


在繁體環境下,一切都非常順利,但簡體的 excel 抓進來的值乍看會有問題,其實是因為我們用OS預設的編碼印出導致的。
其實在繁體環境中的簡體 excel 是以 UNICODE 編碼,因此 POI 也以 UNICODE 讀入,資料並不受影響,若想驗證這點,只需將讀入資料以 UTF-8格式印出文字檔,再以記事本打開驗證。

範例程式:

FileOutputStream fos=null;

OutputStreamWriter osw=null;

try {

fos=new FileOutputStream(new File("d:/t/excel_gb.txt"));

osw=new OutputStreamWriter(fos, "UTF-8");

osw.write(pageName);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {

try {

if(osw!=null)osw.close();

if(fos!=null)fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}