//首先要引入3个包://poi-2.5.1-final-20040804.jar//poi-contrib-2.5.1-final-20040804.jar//poi-scratchpad-2.5.1-final-20040804.jar //代码:import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionForm;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForward;import org.apache.struts.action.Action;import org.apache.poi.hssf.usermodel.*;import javax.servlet.ServletOutputStream;import java.util.List;import java.io.*;import java.util.Iterator;public class QuestionnaireToExcelAction extends Action ...{ //指定生成文件的类型 private static final String CONTENT_TYPE = "application/vnd.ms-excel"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ...{ //获得记录 List list = QuestionnaireProxy.getall(); Iterator it = list.iterator(); Questionnaire que = null; //设定类型 response.setContentType(CONTENT_TYPE); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); //设定文件名字 wb.setSheetName(0, "qustionnaire"); HSSFCell cell = null; for (short i = 0; i < (list.size()+1); i++) ...{ HSSFRow row = s.createRow(i); //第一行 if(i==0) ...{ cell = row.createCell(Short.parseShort("0")); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("字段1"); cell = row.createCell(Short.parseShort("1")); cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置字体 cell.setCellValue("字段2"); }else ...{ //以下几行 que = (Questionnaire) it.next(); cell = row.createCell(Short.parseShort("0")); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("" + que.get字段1); cell = row.createCell(Short.parseShort("1")); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("" + que.get字段2); } } ServletOutputStream stream = null; try ...{ stream = response.getOutputStream(); } catch (IOException ex1) ...{ } try ...{ wb.write(stream); } catch (IOException ex2) ...{ } try ...{ stream.flush(); stream.close(); } catch (IOException ex) ...{ } return mapping.findForward(""); }}