轉自:http://dongisland.iteye.com/blog/1432019
環境
JDK1.6 和 jxl.jar
這兩個都可以在網上下,我下的這個jxl (http://ishare.iask.sina.com.cn/f/14559561.html?from=dl) 可以對Excel2010操作
Model類(以防參數太多而建立的類)
package com.island;public class User { /** * UID */ private static final long serialVersionUID = 346821702783141852L; private String username; private String usermail; private String password; public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUsermail() {return usermail;}public void setUsermail(String usermail) {this.usermail = usermail;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }
Title借口(關鍵詞借口)
package com.island;public interface UserKeys { public static final String USER_NAME_VALUE = "使用者名稱";public static final String USER_EMAIL_VALUE = "使用者郵箱";public static final String USER_PASSWORD_VALUE = "使用者密碼";}
Excel2Xml類
package com.island;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;public class Excel2XML { /** * The output stream to write to */ private OutputStream out; /** * The encoding to write */ private String encoding; public Excel2XML(OutputStream out, String enc) { encoding = enc; this.out = out; if (encoding == null || !encoding.equals("UnicodeBig")) { encoding = "UTF8"; } } /** * 讀取EXCEL檔案,存到formBean放到ArrayList返回 * */ public static ArrayList readExcel(File file) { ArrayList userList = new ArrayList(); Workbook wb = null; try { wb = Workbook.getWorkbook(file); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(wb == null) return null; Sheet[] sheets = wb.getSheets(); if(sheets != null && sheets.length > 0) { for(int i=0; i<sheets.length; i++) { int rowNum = sheets[i].getRows(); int columnNum = sheets[i].getColumns(); Cell[] title = null; for(int row=0; row<rowNum; row ++) { if(row == 0){ title = sheets[i].getRow(row); } else { Cell[] rowContent = sheets[i].getRow(row); User user = paserUnit(title,rowContent); userList.add(user); } } } } wb.close(); return userList; } /** * 根據給的資訊生產XML * @param filmList * @throws IOException */ public void generateXML(ArrayList userList) throws IOException { try { OutputStreamWriter osw = new OutputStreamWriter(out, encoding); BufferedWriter bw = new BufferedWriter(osw); bw.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); bw.newLine(); Iterator it= userList.iterator(); bw.write("<user_info_root>"); bw.newLine(); while(it.hasNext()) { User user = (User) it.next(); bw.write("<user_info>"); bw.newLine(); bw.write("<name>"+user.getUsername()+"</name>"); bw.newLine(); bw.write("<email>"+user.getUsermail()+"</email>"); bw.newLine(); bw.write("<password>"+user.getPassword()+"</email>"); bw.newLine(); bw.write("</user_info>"); bw.newLine(); } bw.write("</user_info_root>"); bw.flush(); bw.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * 按EXCEL的表頭,解析EXCEL的每一行資料,放入formBean * @param title * @param rowContent * @return */ public static User paserUnit(Cell[] title, Cell[] rowContent) { User userContent = new User(); for(int i=0; i<rowContent.length; i++) { Cell filmCell = title[i]; String user = filmCell.getContents(); System.out.println(user); Cell filmContentCell = rowContent[i]; String userValue = filmContentCell.getContents(); if(UserKeys.USER_NAME_VALUE.equals(user)) { userContent.setUsername(userValue); } if(UserKeys.USER_EMAIL_VALUE.equals(user)) { userContent.setUsermail(userValue); } if(UserKeys.USER_PASSWORD_VALUE.equals(user)) { userContent.setPassword(userValue); } } return userContent; } /** * 整型驗證 * @param str * @return */ public static Integer validateInteger(String str){ Integer result = null; if(str == null || str.equals("")) { result = new Integer("0"); return result; } else { return new Integer(str); } } /** * 字元編碼轉換 * @param str * @return */ public static String StringConersion(String str) { String result = null; byte context[]; try { context = str.getBytes("UTF-8"); result = new String(context, "gb2312" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } /** * EXCEL到XML的產生 * @param excelFile * @param xmlFile */ public static void Exce2XMLComplete(File excelFile, File xmlFile) { ArrayList list = Excel2XML.readExcel(excelFile); FileOutputStream fos; try { fos = new FileOutputStream(xmlFile); Excel2XML obj = new Excel2XML(fos,"utf-8"); try { obj.generateXML(list); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { File file = new File("D:\\user.xls"); ArrayList list = Excel2XML.readExcel(file); FileOutputStream fos; try { fos = new FileOutputStream(new File("D:\\user.xml")); Excel2XML obj = new Excel2XML(fos,"utf-8"); try { obj.generateXML(list); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } }}
最後實現附件中的樣式,簡單的用這個方法可能有點複雜,建議這樣做,對以後改進有很好的協助!