struts1.x的例子,struts2.x可以参考自己修改
1.action的写法
- import java.io.*;
- import java.sql.*;
- import java.util.ArrayList;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.poi.hssf.usermodel.*;
- import org.apache.struts.action.*;
- import org.apache.struts.upload.FormFile;
- import org.apache.commons.beanutils.BeanUtils;
-
- public class Action {
-
-
-
- public ActionForward getDownload(ActionMapping actionMapping,
- ActionForm actionForm, HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- Form fm = (Form) actionForm;
-
- String outputFile = request.getRealPath("/tmp/Excel.xls");
-
- try {
-
- HSSFWorkbook workbook = new HSSFWorkbook();
-
- HSSFSheet sheet = workbook.createSheet("Sheet1");
-
- HSSFCellStyle cellStyle = workbook.createCellStyle();
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
-
-
- HSSFRow row = sheet.createRow((short) 0);
-
- HSSFCell cell1 = row.createCell((short) 0);
- HSSFCell cell2 = row.createCell((short) 1);
- HSSFCell cell3 = row.createCell((short) 2);
-
- cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
-
- cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
-
- cell1.setCellValue("姓名");
- cell2.setCellValue("性别");
- cell3.setCellValue("年龄");
-
- Connection connection = session.connection();
-
- String sql = "Select t.name, t.sex, t.age from table t where t.sex = ?";
-
- try {
- PreparedStatement ps = connection.prepareStatement(sql);
- ps.setString(1, fm.getSex());
- ResultSet rs = ps.executeQuery();
- connection.commit();
-
- while (rs.next()) {
-
- int j = 1;
- row = sheet.createRow((short) j);
-
- for (int i = 1; i <= 3; i++) {
- HSSFCell cell = row.createCell((short) (i-1));
-
- cell.setCellStyle(cellStyle);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell.setCellValue(rs.getString(i));
- }
-
- j++;
- }
-
- request.setAttribute("message", "文件生成成功!");
- } catch (SQLException e) {
- request.setAttribute("message", "创建文件失败!");
- e.printStackTrace();
- }
-
- File path = new File(outputFile);
- path.delete();
-
-
- FileOutputStream fOut = new FileOutputStream(outputFile);
-
- workbook.write(fOut);
-
- fOut.flush();
- fOut.close();
-
- request.getSession().setAttribute("Download", outputFile);
-
- } catch (Exception ioexception) {
- request.setAttribute("message", "创建文件失败!");
- return actionMapping.findForward("outJSP");
- }
-
- return actionMapping.findForward("outJSP");
- }
-
-
-
-
- public ActionForward getUpload(ActionMapping actionMapping,
- ActionForm actionForm, HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- Form fm = (Form) actionForm;
- FormFile formfile = fm.getUploadfile();
- InputStream inputstream = formfile.getInputStream();
- fm.clear();
- Session session = HibernateSession.currentSession();
- ArrayList list = new ArrayList();
- int input = 0;
- String name = null;
- String sex = null;
- String age = null;
-
- try {
-
- HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
- HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);
- HSSFRow hssfrow = hssfsheet.getRow(0);
-
-
- for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
- hssfsheet = hssfworkbook.getSheetAt(i);
-
-
- for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
- hssfrow = hssfsheet.getRow(j);
-
- if (hssfrow == null) {
- System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
- break;
- }
-
- if (hssfrow.getCell((short) 0) == null) {
- name = "";
- } else if (hssfrow.getCell((short) 0).getCellType() == 0) {
- name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
- }
-
- else {
- name = hssfrow.getCell((short) 0).getStringCellValue().trim();
- }
-
-
- if(hssfrow.getCell((short) 1) == null){
- sex = "";
- } else if(hssfrow.getCell((short) 1).getCellType() == 0) {
- sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
- }
-
- else {
- sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
- }
-
-
- if(hssfrow.getCell((short) 1) == null){
- age = "";
- } else if(hssfrow.getCell((short) 1).getCellType() == 0) {
- age = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
- }
-
- else {
- age = hssfrow.getCell((short) 1).getStringCellValue().trim();
- }
-
- name = name.trim();
- sex = sex.toUpperCase();
-
- if (name.equals("")) {
- error.setName(name);
- error.setMessage("姓名不能为空");
-
- list.add(error);
- continue;
- } else {
- fm.setName(name);
- fm.setSex(sex);
- fm.setAge(age);
-
- session.save(fm);
- }
-
- input++;
- }
- }
-
- session.saveObjs(list.toArray());
- } catch () {
-
- }
- }
- }
2.Form的写法
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.upload.FormFile;
-
- public class Form extends ActionForm {
-
- private FormFile _flddo;
-
- public void setUploadfile(FormFile formfile) {
- _flddo = formfile;
- }
-
- public FormFile getUploadfile() {
- return _flddo;
- }
-
- public void clear() {
- _flddo = null;
- }
- }
3.上传页面Upload.jsp
- <%@ page contentType="text/html; charset=GBK" language="java"%>
- <%@ taglib uri="http://jakarta./struts/tags-html" prefix="html"%>
-
- <html>
- <html:form action="/Action.do?method=getUpload" method="POST" enctype="multipart/form-data">
- <html:file property="uploadfile" size="80%" />
- <input type="button" value="导 入" onclick="upload(this.form)" class="buttonGray">
- </html:form>
- </html>
-
- <script language="javascript">
- function upload(obj)
- {
- if(confirm("您现在选择的是XXX,您确定要导入吗?"))
- {
- var uploadfile = document.all.uploadfile.value;
- if((null == uploadfile) ||( "" == uploadfile))
- {
- alert("上传文件没有指定!");
- return false;
- }
- obj.action = '<html:rewrite page="/Action.do?method=getUpload"/>';
- obj.submit();
- }
- }
- </script>
4.下载页面Download.jsp
- <%@ page contentType="text/html; charset=GBK"%>
- <%@ taglib uri="http://jakarta./struts/tags-html" prefix="html" %>
- <%@ taglib uri="http://jakarta./struts/tags-bean" prefix="bean" %>
-
- <%
-
- String download = (String) request.getSession().getAttribute("Download");
-
- request.getSession().removeAttribute("Download");
- %>
-
- <html>
- 下传文件 <a href="<%=download %>" name="下载">下载</a>
- </html>