没啥难度,不过留着存档
server端struts2,利用poi生成excel:
client端flex,demo:
server端struts2,利用poi生成excel:
public void excel() {
System.out.println("come on!");
HttpServletResponse response = ServletActionContext.getResponse();
try {
//设置返回类型为excel
response.setContentType("application/vnd.ms-excel; charset=UTF-8");
//设置返回文件名为aaa.xls
response.setHeader("Content-Disposition", "filename=aaa.xls");
response.setHeader("Cache-Control", "no-cache");
//利用poi生成excel
int index = 1;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(index++);
HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString str = new HSSFRichTextString("1");
cell.setCellValue(str);
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
str = new HSSFRichTextString("2");
cell.setCellValue(str);
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
str = new HSSFRichTextString("3");
cell.setCellValue(str);
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
str = new HSSFRichTextString("4");
cell.setCellValue(str);
//将生成的excel通过response返回
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
client端flex,demo:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.navigateToURL;
public function export(url:String):void{
var u:URLRequest = new URLRequest(url);
url.method = URLRequestMethod.POST;
navigateToURL(url,"_blank");
}
]]>
</mx:Script>
<mx:Button label="导出excel" click="export('http://localhost:8080/webtest/abc_excel.action')"/>
</mx:Application>