strus导出文件的常用方法
1.配置strus
<action name="exportDevicesTemplateForExcel" method="exportDevicesTemplateForExcel"
class="deviceAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=utf-8</param>
<param name="inputName">returnFile</param>
<param name="contentDisposition">attachment;filename="${fileName}.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
2.把生成的文件做成输出流
public class ExcelUtil {
/**
* 将Workbook写入到InputStream
*
* @param workbook
* HSSFWorkbook
* @param fileName
* String 文件名
*
* */
public static InputStream workbook2InputStream(HSSFWorkbook workbook) {
// this.setFileName(URLEncoder.encode(fileName, "UTF-8"));
InputStream excelStream = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
baos.flush();
byte[] by = baos.toByteArray();
excelStream = new ByteArrayInputStream(by, 0, by.length);
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return excelStream;
}
}