JavaScript Upload & Download

http://www.blogjava.net/hawk8359/archive/2008/02/19/180623.html[@more@] struts1.x的例子,struts2.x可以参考自己修改

1.action的写法

None.gif import java.io. * ;
None.gif import java.sql. * ;
None.gif import java.util.ArrayList;
None.gif
None.gif import javax.servlet.http.HttpServletRequest;
None.gif import javax.servlet.http.HttpServletResponse;
None.gif
None.gif import org.apache.poi.hssf.usermodel. * ;
None.gif import org.apache.struts.action. * ;
None.gif import org.apache.struts.upload.FormFile;
None.gif import org.apache.commons.beanutils.BeanUtils;
None.gif
ExpandedBlockStart.gifContractedBlock.gif public class Action dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*
InBlock.gif * 把数据库中的字段导入到Excel ,并生成Excel文档
ExpandedSubBlockEnd.gif **/

InBlock.gifpublic ActionForward getDownload(ActionMapping actionMapping,
InBlock.gif ActionForm actionForm, HttpServletRequest request,
ExpandedSubBlockStart.gifContractedSubBlock.gif HttpServletResponse response) throws Exception dot.gif{
InBlock.gif Form fm = (Form) actionForm;
InBlock.gif// Excel 文件存放在服务器的相对路径下
InBlock.gif String outputFile = request.getRealPath("/tmp/Excel.xls");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif// 创建新的Excel 工作簿
InBlock.gif HSSFWorkbook workbook = new HSSFWorkbook();
InBlock.gif// 在Excel 工作簿中建一工作表
InBlock.gif HSSFSheet sheet = workbook.createSheet("Sheet1");
InBlock.gif// 设置单元格格式(文本)
InBlock.gif HSSFCellStyle cellStyle = workbook.createCellStyle();
InBlock.gif cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
InBlock.gif
InBlock.gif// 在索引0的位置创建行(第一行)
InBlock.gif HSSFRow row = sheet.createRow((short) 0);
InBlock.gif
InBlock.gif HSSFCell cell1 = row.createCell((short) 0);// 第一列
InBlock.gif HSSFCell cell2 = row.createCell((short) 1);
InBlock.gif HSSFCell cell3 = row.createCell((short) 2);
InBlock.gif// 定义单元格为字符串类型
InBlock.gif cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
InBlock.gif cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
InBlock.gif cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
InBlock.gif
InBlock.gif cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
InBlock.gif cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
InBlock.gif cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
InBlock.gif// 在单元格中输入数据
InBlock.gif cell1.setCellValue("姓名");
InBlock.gif cell2.setCellValue("性别");
InBlock.gif cell3.setCellValue("年龄");
InBlock.gif
InBlock.gif Connection connection = session.connection();
InBlock.gif
InBlock.gif String sql = "Select t.name, t.sex, t.age from table t where t.sex = ?";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif PreparedStatement ps = connection.prepareStatement(sql);
InBlock.gif ps.setString(1, fm.getSex());// 传入查询条件
InBlock.gif ResultSet rs = ps.executeQuery();// 查询结果存入rs
InBlock.gif connection.commit();// 执行SQL
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifwhile (rs.next()) dot.gif{
InBlock.gif//设置j行从第二行开始
InBlock.gif int j = 1;
InBlock.gif row = sheet.createRow((short) j);
InBlock.gif//设置i列从第二列开始
ExpandedSubBlockStart.gifContractedSubBlock.gif for (int i = 1; i <= 3; i++) dot.gif{
InBlock.gif HSSFCell cell = row.createCell((short) (i-1));
InBlock.gif// 设置单元格格式
InBlock.gif cell.setCellStyle(cellStyle);
InBlock.gif cell.setCellType(HSSFCell.CELL_TYPE_STRING);
InBlock.gif cell.setEncoding(HSSFCell.ENCODING_UTF_16);
InBlock.gif cell.setCellValue(rs.getString(i));
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif j++;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif request.setAttribute("message", "文件生成成功!");
ExpandedSubBlockStart.gifContractedSubBlock.gif }
catch (SQLException e) dot.gif{
InBlock.gif request.setAttribute("message", "创建文件失败!");
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

InBlock.gif// 删除路径下同名的Excel 文件
InBlock.gif File path = new File(outputFile);
InBlock.gif path.delete();
InBlock.gif
InBlock.gif// 新建一输出文件流
InBlock.gif FileOutputStream fOut = new FileOutputStream(outputFile);
InBlock.gif// 把相应的Excel 工作簿存盘
InBlock.gif workbook.write(fOut);
InBlock.gif// 操作结束,关闭文件
InBlock.gif fOut.flush();
InBlock.gif fOut.close();
InBlock.gif//该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空)
InBlock.gif request.getSession().setAttribute("Download", outputFile);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif }
catch (Exception ioexception) dot.gif{
InBlock.gif request.setAttribute("message", "创建文件失败!");
InBlock.gifreturn actionMapping.findForward("outJSP");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifreturn actionMapping.findForward("outJSP");
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*
InBlock.gif * 从Excel文件中读取数据,并导入到数据库中
ExpandedSubBlockEnd.gif **/

InBlock.gifpublic ActionForward getUpload(ActionMapping actionMapping,
InBlock.gif ActionForm actionForm, HttpServletRequest request,
ExpandedSubBlockStart.gifContractedSubBlock.gif HttpServletResponse response) throws Exception dot.gif{
InBlock.gif// 获取excel 文件
InBlock.gif Form fm = (Form) actionForm;
InBlock.gif FormFile formfile = fm.getUploadfile();
InBlock.gif InputStream inputstream = formfile.getInputStream();
InBlock.gif fm.clear();// 清空
InBlock.gif Session session = HibernateSession.currentSession();
InBlock.gif ArrayList list = new ArrayList();
InBlock.gifint input = 0; //导入记数
InBlock.gif String name = null;
InBlock.gif String sex = null;
InBlock.gif String age = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
InBlock.gif HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
InBlock.gif HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一个工作表
InBlock.gif HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
InBlock.gif
InBlock.gif//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
ExpandedSubBlockStart.gifContractedSubBlock.gif for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) dot.gif{
InBlock.gif hssfsheet = hssfworkbook.getSheetAt(i);
InBlock.gif
InBlock.gif//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
ExpandedSubBlockStart.gifContractedSubBlock.gif for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) dot.gif{
InBlock.gif hssfrow = hssfsheet.getRow(j);
InBlock.gif//判断是否还存在需要导入的数据
ExpandedSubBlockStart.gifContractedSubBlock.gif if (hssfrow == null) dot.gif{
InBlock.gif System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
ExpandedSubBlockStart.gifContractedSubBlock.gifif (hssfrow.getCell((short) 0) == null) dot.gif{
InBlock.gif name = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else if (hssfrow.getCell((short) 0).getCellType() == 0) dot.gif{
InBlock.gif name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
ExpandedSubBlockEnd.gif }

InBlock.gif//如果EXCEL表格中的数据类型为字符串型
ExpandedSubBlockStart.gifContractedSubBlock.gif else dot.gif{
InBlock.gif name = hssfrow.getCell((short) 0).getStringCellValue().trim();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
InBlock.gif//姓名
ExpandedSubBlockStart.gifContractedSubBlock.gif if(hssfrow.getCell((short) 1) == null)dot.gif{
InBlock.gif sex = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else if(hssfrow.getCell((short) 1).getCellType() == 0) dot.gif{
InBlock.gif sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
ExpandedSubBlockEnd.gif }

InBlock.gif//如果EXCEL表格中的数据类型为字符串型
ExpandedSubBlockStart.gifContractedSubBlock.gif else dot.gif{
InBlock.gif sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
InBlock.gif//姓名
ExpandedSubBlockStart.gifContractedSubBlock.gif if(hssfrow.getCell((short) 1) == null)dot.gif{
InBlock.gif age = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif }
else if(hssfrow.getCell((short) 1).getCellType() == 0) dot.gif{
InBlock.gif

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69498/viewspace-1009161/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/69498/viewspace-1009161/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值