[b]JAR包:
JDK1.6和jxl.jar
需求:
EXCEL文件类似如下:
见:附件
XML文件类似如下:
见:附件[/b]
Model类(以防参数太多而建立的类)
Title借口(关键词借口)
Excel2Xml类
最后实现附件中的样式,简单的用这个方法可能有点复杂,建议这样做,对以后改进有很好的帮助!
JDK1.6和jxl.jar
需求:
EXCEL文件类似如下:
见:附件
XML文件类似如下:
见:附件[/b]
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();
}
}
}
最后实现附件中的样式,简单的用这个方法可能有点复杂,建议这样做,对以后改进有很好的帮助!