import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
class Student {
public int s_no;
public String s_name;
public int s_class;
}
public class Rat_Contact_001 {
public static void javaToExcel(ArrayList studentArr)
throws IOException {
//使用StringBuilder比String的效率比高,占用计算机资源没有这么多。
//最后输出前,转成字符串就可以了。
StringBuilder excelXMLStringBuilder = new StringBuilder("");
//构造好excelXML的头
excelXMLStringBuilder
.append("<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=“urn:schemas-microsoft-com?spreadsheet” xmlns:o=“urn:schemas-microsoft-com?office” xmlns:x=“urn:schemas-microsoft-com?excel” xmlns:ss=“urn:schemas-microsoft-com?spreadsheet” xmlns:html=“http://www.w3.org/TR/REC-html40”><Worksheet ss:Name=“学生表搜索”>
//输出好表头
excelXMLStringBuilder
.append("<Data ss:Type=“String”>测试test<Data ss:Type=“String”>姓名111<Data ss:Type=“String”>班级");
//构造出每一行
for (int i = 0; i < studentArr.size(); i++) {
//先构造出节点
excelXMLStringBuilder.append("");
Student student = studentArr.get(i);
//再不停地构造好每一个节点
excelXMLStringBuilder.append("<Data ss:Type=“String”>"
+ student.s_no
+ “<Data ss:Type=“String”>”
+ student.s_name
+ “<Data ss:Type=“String”>”
+ student.s_class + “”);
excelXMLStringBuilder.append("");
}
//再构造好excelXML的尾
excelXMLStringBuilder.append("
//最后把这个字符串打印到c:\学生表.xml就完事了
//false代表覆盖输出,不是在此文件的末尾继续输出
PrintWriter printwriter = new PrintWriter(new FileWriter(“c:\学生表搜素.xml”,
false));
//输出前把excelXMLStringBuilder转化成字符串
printwriter.print(excelXMLStringBuilder + “”);
//清空输出缓冲区
printwriter.flush();
//必须关闭文件输出流,Java才会在文件打印出字符串,也就是二进制流,
printwriter.close();
}
public static void main(String args[]) throws IOException {
ArrayList<Student> studentArr = new ArrayList<Student>();
Student s1 = new Student();
s1.s_no = 1;
s1.s_name = "中文";
s1.s_class = 102;
studentArr.add(s1);
Student s2 = new Student();
s2.s_no = 2;
s2.s_name = "是没有";
s2.s_class = 101;
studentArr.add(s2);
Student s3 = new Student();
s3.s_no = 3;
s3.s_name = "问题的!";
s3.s_class = 103;
studentArr.add(s3);
javaToExcel(studentArr);
System.out.println("学生表.xml已生成,该xml是专门以excel打开的xml");
}
}