如何在Java中将Excel转化为XML格式文件

 我来答
歪_听得到吗
2017-10-23 · TA获得超过4004个赞
知道大有可为答主
回答量:1887
采纳率:44%
帮助的人:989万
展开全部
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.sppm.kds.entity.Policy;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ImportTest {

@Test
public void test01(){
File file = new File("E:\\import\\语料.xls");
readExcelAddXML(file,"E:\\import");
}

public void readExcelAddXML(File f,String path){
OutputStreamWriter pw = null;
try {
List<Policy> list = readExcelFileOutList(f);
//String tempFile = "E:\\import\\test.xml";
File file = new File(path,"tianlong.xml");
if (!file.exists()) {
file.createNewFile();
}
//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw = new OutputStreamWriter(new FileOutputStream(file),"utf-8");

pw.write("<docs>"+"\n");
pw.write("<lst>"+"\n");
pw.write("<num>"+list.size()+"</num>"+"\n");
pw.write("</lst>"+"\n");
for(int i=0;i <list.size();i++){
pw.write("<doc>"+"\n");
pw.write("<id>"+list.get(i).getId()+"</id>"+"\n");
pw.write("<title>"+list.get(i).getTitle()+"</title>"+"\n");
pw.write("<department>"+list.get(i).getDepatment()+"</department>"+"\n");
pw.write("<release_date>"+list.get(i).getRelease_date()+"</release_date>"+"\n");
pw.write("<timeliness>"+list.get(i).getTimeliness()+"</timeliness>"+"\n");
pw.write("<province>"+list.get(i).getProvince()+"</province>"+"\n");
pw.write("<category>"+list.get(i).getCategory()+"</category>"+"\n");
pw.write("<effect_level>"+list.get(i).getEffect_level()+"</effect_level>"+"\n");
pw.write("<source>"+list.get(i).getSource()+"</source>"+"\n");
pw.write("<content>"+list.get(i).getContent()+"</content>"+"\n");
pw.write("<post_date>"+list.get(i).getPost_date()+"</post_date>"+"\n");
pw.write("<issued_number>"+list.get(i).getIssued_number()+"</issued_number>"+"\n");
pw.write("<implement_date>"+list.get(i).getImplement_date()+"</implement_date>"+"\n");
pw.write("</doc>");
pw.flush();
}
pw.write("\n"+"</docs>");
pw.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
pw.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}

private static List<Policy> readExcelFileOutList(File f) {
System.out.println("进入到readExcelFileOutList()");
List<Policy> list = new ArrayList<Policy>();
InputStream is = null;
try{
is = new FileInputStream(f);
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();//得到列数

System.out.println("行:"+num_row+"列:"+num_column);
for(int j=1;j < num_row ;j++){
Cell[] cell = rs.getRow(j);//得到第j行的所有值

for(int column_index=0;column_index<num_column;column_index++){
String id = cell[column_index++].getContents();
String title= cell[column_index++].getContents();
String department = cell[column_index++].getContents();
String release_date = cell[column_index++].getContents();
String timeliness= cell[column_index++].getContents();
String province = cell[column_index++].getContents();
String category= cell[column_index++].getContents();
String effect_level = cell[column_index++].getContents();
String source = cell[column_index++].getContents();
String content = cell[column_index++].getContents();
String post_date= cell[column_index++].getContents();
String issued_number = cell[column_index++].getContents();
String implement_date = cell[column_index++].getContents();
list.add(new Policy(id, title, department, release_date, timeliness, province, category, effect_level, source, content, post_date,issued_number,implement_date));
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {
if(is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式