HTML:
导出
Struts.xml
true
application/vnd.ms-excel;charset=GBK
excelStream
attachment;filename=${fileName}
20480
true 允许编码
application/vnd.ms-excel;charset=GBK 内容类型:就是excel,后面的charset是编码格式
excelStream 输入流的名字定义,随便定义
attachment;filename=${fileName}输出时的文件名
UserAction:
public class UserAction{
private InputStream excelStream;
private String fileName = "golf.xls";//导出excel
public String exportExcel(){
try{
StringBuffer sb = new StringBuffer();
//\t表示下一列,\n表示下一行
sb.append("title1").append("\t").append("title2").append("\n");
//导入的数据进行迭代,拼成字符串
for(;;){
sb.append("content1").append("\t").append("content2").append("\n");
}
//将字符串放入流中
excelStream = new ByteArrayInputStream(sb.toString().getBytes("GBK"),
0,
sb.toString().getBytes("GBK").length);
}catch(Exception e){
}
return "excel";
}
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
或者设置文件名fileName, 文件名fileName 对应 getFileName() 首字母小写
public String getFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");
String fileName = (sf.format(new Date()).toString())+ "项目信息.xls";
try {
fileName = new String(downloadFileName.getBytes(),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
public void setFileName(String fileName){
this.fileName = fileName;
}
/show.asp?id=276