WebWork中除了默認(rèn)支持的幾中視圖外還可以自己來(lái)定義需要的視圖,如JFreeChart,Excel等
這里生成Excel用的是POI的API
WebWork中定義ResultType視圖類型只需要繼承Result接口
代碼如下
package com.customer.resulttype;
import com.opensymphony.xwork.Result;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.webwork.ServletActionContext;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
public class ExcelResult implements Result{
??? private HSSFWorkbook workbook;
??? private String filename;
??? private String contenttype;
??? public void execute(ActionInvocation invocation) throws Exception {
??????? if(contenttype==null)
??????????? contenttype = "application/ms-excel";
??????? if (workbook==null)
??????????? workbook = (HSSFWorkbook) invocation.getStack().findValue("workbook");
??????
??????? HttpServletResponse response = ServletActionContext.getResponse();
??????? response.setContentType(contenttype);
??????? response.setHeader("Content-Disposition","attachment;Filename="+filename+".xls");
??????? OutputStream os = response.getOutputStream();
??????? workbook.write(os);
??????? os.flush();
??????? os.close();
??? }
??? public void setWorkbook(HSSFWorkbook workbook) {
??????? this.workbook = workbook;
??? }
??? public void setFilename(String filename) {
??????? this.filename = filename;
??? }
??? public void setContenttype(String contenttype) {
??????? this.contenttype = contenttype;
??? }
}
視圖做完之后做如下配置運(yùn)行測(cè)試
package com.customer.action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.webwork.ServletActionContext;
import com.dboperate.ResultGather;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
public class ExportExcelAction extends ActionSupport {
??? private HSSFWorkbook workbook;
??? public String execute() throws Exception {
??????? return SUCCESS;
??? }
??? public String product() throws Exception {
??????? try {
??????????? workbook = new HSSFWorkbook();
??????????? HSSFSheet sheet = workbook.createSheet();
??????????? workbook.setSheetName(0, "廠商產(chǎn)品", (short) 1);
??????????? HSSFRow row = sheet.createRow((short) 0);
??????????? HSSFCell cell0 = row.createCell((short) 0);
??????????? HSSFCell cell1 = row.createCell((short) 1);
??????????? HSSFCell cell2 = row.createCell((short) 2);
??????????? HSSFCell cell3 = row.createCell((short) 3);
??????????? HSSFCell cell4 = row.createCell((short) 4);
??????????? HSSFCell cell5 = row.createCell((short) 5);
??????????? HSSFCell cell6 = row.createCell((short) 6);
??????????? HSSFCell cell7 = row.createCell((short) 7);
??????????? HSSFCell cell8 = row.createCell((short) 8);
??????????? HSSFCell cell9 = row.createCell((short) 9);
??????????? cell0.setEncoding(HSSFCell.ENCODING_UTF_16);//這里是設(shè)置編碼保證中文正常顯示
??????????? cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell9.setEncoding(HSSFCell.ENCODING_UTF_16);
??????????? cell0.setCellValue("廠商名");
??????????? cell1.setCellValue("產(chǎn)品名");
??????????? cell2.setCellValue("重量");
??????????? cell3.setCellValue("星級(jí)");
??????????? cell4.setCellValue("parama");
??????????? cell5.setCellValue("paramb");
??????????? cell6.setCellValue("paramc");
??????????? cell7.setCellValue("paramd");
??????????? cell8.setCellValue("狀態(tài)");
??????????? cell9.setCellValue("備注");
??????? } catch (Exception e) {
??????? }
??????? return SUCCESS;
??? }
??? public HSSFWorkbook getWorkbook() {
??????? return workbook;
??? }
}
Xwork.xml中配置加入
??????? <result-type default="true" name="freemarker"
??????????? <result-type name="excel" class="com.customer.resulttype.ExcelResult"/>
??????? </result-types>
?<action name="exportExcel" class="com.customer.action.ExportExcelAction">
??????????? <result name="success" type="excel">
??????????????? <param name="filename">productparam>
???????????????
??????????? </result>
??????? </action>