81,095
社区成员
发帖
与我相关
我的任务
分享
<%
String txt = (String)request.getAttribute("txt");
response.setContentType("application/txt");
response.setHeader("Content-disposition", "attachment;filename=table.txt");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(txt.getBytes());
} catch (IOException e) {
throw e;
} finally {
if (bos != null)
bos.close();
}
%>
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExportUtil {
/**
* @param dataSource
* @param file
*/
public void export(List<MyBean> dataSource,String file){
FileWriter fw=null;
MyBean myBean=null;
String city=null;
String area_code=null;
String start_gt=null;
String hlr_name=null;
StringBuffer row=null;
try{
fw=new FileWriter(file);
row=new StringBuffer();
row.append("city\t\t").append("area_code\t\t").append("start_gt\t\t").append("hlr_name"+"\n");
fw.write(row.toString());
for(int i=0;i<dataSource.size();i++){
row=new StringBuffer();
myBean=dataSource.get(i);
city=myBean.getCity();
area_code=myBean.getArea_code();
start_gt=myBean.getStart_gt();
hlr_name=myBean.getHlr_name();
row.append(city+"\t\t").append(area_code+"\t\t").append(start_gt+"\t\t").append(hlr_name+"\n");
fw.write(row.toString());
}
fw.flush();
}catch(IOException e){
}finally{
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]){
List dataSource=new ArrayList<MyBean>();
dataSource.add(new MyBean("金华","579","861373892","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373893","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373894","金华HLR3"));
dataSource.add(new MyBean("金华","579","861373895","金华HLR3"));
ExportUtil exportUtil=new ExportUtil();
exportUtil.export(dataSource, "F:/JavaScript/软件说明.txt");
}
}
class MyBean{
//假设你的数据库字段都是varchar类型的
private String city;
private String area_code;
private String start_gt;
private String hlr_name;
public MyBean() {
super();
}
public MyBean(String city, String area_code, String start_gt, String hlr_name) {
super();
this.city = city;
this.area_code = area_code;
this.start_gt = start_gt;
this.hlr_name = hlr_name;
}
public String getArea_code() {
return area_code;
}
public void setArea_code(String area_code) {
this.area_code = area_code;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getHlr_name() {
return hlr_name;
}
public void setHlr_name(String hlr_name) {
this.hlr_name = hlr_name;
}
public String getStart_gt() {
return start_gt;
}
public void setStart_gt(String start_gt) {
this.start_gt = start_gt;
}
}