Java读取excel文件生成xml文档和JSON文档

1.读取Excel文档中的内容


注意:刚开始在网上下了jlx.rar文件,没注意直接把jlx.rar当成jar包导进去了,在写代码时候纠结了,死活找不到相关jxl.jar包下面的类!各位看客注意了导包的时候一定要导入.jar包   rar与jar  真的好像  眼睛睁大  放亮了呵呵!

jlx.jar包下载地址:

package com.jun.excel;

import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
 
public class ImportUser
 {
     public static void main(String[] args)
     {
       readExcelFile();
     }
//读取d://test.xls文件
	private static void readExcelFile() {
		// TODO Auto-generated method stub
		 try
         {
            String sourcefile = "d://test.xls";
             InputStream is = new FileInputStream(sourcefile);
             Workbook rwb = Workbook.getWorkbook(is);
           //获得总的Sheets,得到sheet的层数
             Sheet[] sheets = rwb.getSheets();
             int sheetLen = sheets.length;
             //获得第一个Sheets 的结果
            jxl.Sheet rs = rwb.getSheet(0); 
            int num_row = rs.getRows();//得到行数
            int num_column=rs.getColumns();//得到列数
            for(int j=0;j < num_row ;j++)
             {
                Cell[] cell  = rs.getRow(j);//得到第j行的所有值
                
                for(int column_index=0;column_index<num_column;column_index++){
                 String value  = cell[column_index].getContents();//得到第j行,第column_indexs列的值
                System.out.print(""+""+rs.getRow(j)[column_index].getContents()+"   ");
                
                }
                System.out.println("");
             }
         }
         catch(Exception ex)
        {
               ex.printStackTrace();
         }
	}
 }

2.生成xml文档

生成XML文档类的主要代码:

package com.jun.createxml;

import java.io.FileOutputStream;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.jun.excel.ImportUser;

public class CreateXML {
	DocumentBuilderFactory dbf  = null;
	DocumentBuilder db = null;
	Document doc = null;
	TransformerFactory tff = null;
	Transformer tf = null;
	Source in = null;
	Result out = null;
	String xmlpath = null;
	
	public CreateXML()throws Exception
	{
		
		dbf = DocumentBuilderFactory.newInstance();//实例化工厂类
		dbf.setValidating(false);//不进行有效性检查
		dbf.setNamespaceAware(true);
		
		db = dbf.newDocumentBuilder();//实例化DocumentBuilder类
		
		doc = (Document) db.newDocument();//实例化Document类
		
		tff = TransformerFactory.newInstance();
		tf = tff.newTransformer();
		
		in=new DOMSource((Node) doc);
	}

/*=====================================================
 *============生成xml文件
 *=====================================================*/
	public void ProuduceXml(String axmlpath)
	{
		
		try
		{
		
		Element users = ((org.w3c.dom.Document) doc).createElement("users");//生产根元素students		
		((Node) doc).appendChild(users);//将根元素添加到根节点后面
		
		
		for(int i=0;i<ImportUser.userList.size();i++){
			Element user = ProuduceElement(ImportUser.userList.get(i).getName(), ImportUser.userList.get(i).getAge(),ImportUser.userList.get(i).getAddress(),ImportUser.userList.get(i).getPhone());
			
		    users.appendChild(user);
			
		}
		
		
		xmlpath = axmlpath;
		out=new StreamResult(new FileOutputStream(xmlpath+"tr.xml"));//生成输出源
		tf.transform(in,out); 
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
	
	public Element ProuduceElement(String aname, String _age,String _address,String _phone) throws Exception
	{
		
		Element user = ((org.w3c.dom.Document) doc).createElement("student");//生成student元素(students的子元素)
		
		Element name = ((org.w3c.dom.Document) doc).createElement("name");//创建name子元素
		name.appendChild(((org.w3c.dom.Document) doc).createTextNode(aname));//在name元素后添加文本节点
		user.appendChild(name);//添加student的子元素name
		
		
		//设置student的属性id的值
		
		
		Element age = doc.createElement("age");
		age.appendChild(doc.createTextNode(_age));
		user.appendChild(age);
		
		Element address = doc.createElement("address");
		age.appendChild(doc.createTextNode(_address));
		user.appendChild(address);
		
		
		Element phone= doc.createElement("phone");
		age.appendChild(doc.createTextNode(_phone));
		user.appendChild(address);
		return user;
}
}




在main函数中调用的方法:

//创建XML文档
private static void createXMLFile() {
        // TODO Auto-generated method stub
    
    try
    {
    
    CreateXML xml=new CreateXML();        
    xml.ProuduceXml(xmlpath);
    }
    catch(Exception e)
    {
        System.out.println("main:\n"+e.getMessage());
    }
        
        
    }





3.生成json文档

你可能感兴趣的:(Java读取excel文件生成xml文档和JSON文档)