重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

JavaScriptUpload&Download

http://www.blogjava.net/hawk8359/archive/2008/02/19/180623.html[@more@]
JavaScript Upload & Download{
JavaScript Upload & DownloadJavaScript Upload & Download/**//*
JavaScript Upload & Download * 把数据库中的字段导入到Excel ,并生成Excel文档
JavaScript Upload & Download **/
JavaScript Upload & Downloadpublic ActionForward getDownload(ActionMapping actionMapping,
JavaScript Upload & Download ActionForm actionForm, HttpServletRequest request,
JavaScript Upload & DownloadJavaScript Upload & Download HttpServletResponse response)throws ExceptionJavaScript Upload & Download{
JavaScript Upload & Download Form fm= (Form) actionForm;
JavaScript Upload & Download// Excel 文件存放在服务器的相对路径下
JavaScript Upload & Download String outputFile= request.getRealPath("/tmp/Excel.xls");
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & DownloadtryJavaScript Upload & Download{
JavaScript Upload & Download// 创建新的Excel 工作簿
JavaScript Upload & Download HSSFWorkbook workbook=new HSSFWorkbook();
JavaScript Upload & Download// 在Excel 工作簿中建一工作表
JavaScript Upload & Download HSSFSheet sheet= workbook.createSheet("Sheet1");
JavaScript Upload & Download// 设置单元格格式(文本)
JavaScript Upload & Download HSSFCellStyle cellStyle= workbook.createCellStyle();
JavaScript Upload & Download cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
JavaScript Upload & Download
JavaScript Upload & Download// 在索引0的位置创建行(第一行)
JavaScript Upload & Download HSSFRow row= sheet.createRow((short)0);
JavaScript Upload & Download
JavaScript Upload & Download HSSFCell cell1= row.createCell((short)0);// 第一列
JavaScript Upload & Download HSSFCell cell2= row.createCell((short)1);
JavaScript Upload & Download HSSFCell cell3= row.createCell((short)2);
JavaScript Upload & Download// 定义单元格为字符串类型
JavaScript Upload & Download cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download
JavaScript Upload & Download cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download// 在单元格中输入数据
JavaScript Upload & Download cell1.setCellValue("姓名");
JavaScript Upload & Download cell2.setCellValue("性别");
JavaScript Upload & Download cell3.setCellValue("年龄");
JavaScript Upload & Download
JavaScript Upload & Download Connection connection= session.connection();
JavaScript Upload & Download
JavaScript Upload & Download String sql="Select t.name, t.sex, t.age from table t where t.sex = ?";
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & DownloadtryJavaScript Upload & Download{
JavaScript Upload & Download PreparedStatement ps= connection.prepareStatement(sql);
JavaScript Upload & Download ps.setString(1, fm.getSex());// 传入查询条件
JavaScript Upload & Download ResultSet rs= ps.executeQuery();// 查询结果存入rs
JavaScript Upload & Download connection.commit();// 执行SQL
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Downloadwhile (rs.next())JavaScript Upload & Download{
JavaScript Upload & Download//设置j行从第二行开始
JavaScript Upload & Downloadint j=1;
JavaScript Upload & Download row= sheet.createRow((short) j);
JavaScript Upload & Download//设置i列从第二列开始
JavaScript Upload & DownloadJavaScript Upload & Downloadfor (int i=1; i<=3; i++)JavaScript Upload & Download{
JavaScript Upload & Download HSSFCell cell= row.createCell((short) (i-1));
JavaScript Upload & Download// 设置单元格格式
JavaScript Upload & Download cell.setCellStyle(cellStyle);
JavaScript Upload & Download cell.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell.setCellValue(rs.getString(i));
JavaScript Upload & Download }
JavaScript Upload & Download
JavaScript Upload & Download j++;
JavaScript Upload & Download }
JavaScript Upload & Download
JavaScript Upload & Download request.setAttribute("message","文件生成成功!");
JavaScript Upload & DownloadJavaScript Upload & Download }catch (SQLException e)JavaScript Upload & Download{
JavaScript Upload & Download request.setAttribute("message","创建文件失败!");
JavaScript Upload & Download e.printStackTrace();
JavaScript Upload & Download }
JavaScript Upload & Download// 删除路径下同名的Excel 文件
JavaScript Upload & Download File path=new File(outputFile);
JavaScript Upload & Download path.delete();
JavaScript Upload & Download
JavaScript Upload & Download// 新建一输出文件流
JavaScript Upload & Download FileOutputStream fOut=new FileOutputStream(outputFile);
JavaScript Upload & Download// 把相应的Excel 工作簿存盘
JavaScript Upload & Download workbook.write(fOut);
JavaScript Upload & Download// 操作结束,关闭文件
JavaScript Upload & Download fOut.flush();
JavaScript Upload & Download fOut.close();
JavaScript Upload & Download//该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空)
JavaScript Upload & Download request.getSession().setAttribute("Download", outputFile);
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download }catch (Exception ioexception)JavaScript Upload & Download{
JavaScript Upload & Download request.setAttribute("message","创建文件失败!");
JavaScript Upload & Downloadreturn actionMapping.findForward("outJSP");
JavaScript Upload & Download }
JavaScript Upload & Download
JavaScript Upload & Downloadreturn actionMapping.findForward("outJSP");
JavaScript Upload & Download }
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download/**//*
JavaScript Upload & Download * 从Excel文件中读取数据,并导入到数据库中
JavaScript Upload & Download **/
JavaScript Upload & Downloadpublic ActionForward getUpload(ActionMapping actionMapping,
JavaScript Upload & Download ActionForm actionForm, HttpServletRequest request,
JavaScript Upload & DownloadJavaScript Upload & Download HttpServletResponse response)throws ExceptionJavaScript Upload & Download{
JavaScript Upload & Download// 获取excel 文件
JavaScript Upload & Download Form fm= (Form) actionForm;
JavaScript Upload & Download FormFile formfile= fm.getUploadfile();
JavaScript Upload & Download InputStream inputstream= formfile.getInputStream();
JavaScript Upload & Download fm.clear();// 清空
JavaScript Upload & Download Session session= HibernateSession.currentSession();
JavaScript Upload & Download ArrayList list=new ArrayList();
JavaScript Upload & Downloadint input=0;//导入记数
JavaScript Upload & Download String name=null;
JavaScript Upload & Download String sex=null;
JavaScript Upload & Download String age=null;
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & DownloadtryJavaScript Upload & Download{
JavaScript Upload & Download//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
JavaScript Upload & Download HSSFWorkbook hssfworkbook=new HSSFWorkbook(inputstream);
JavaScript Upload & Download HSSFSheet hssfsheet= hssfworkbook.getSheetAt(0);//第一个工作表
JavaScript Upload & Download HSSFRow hssfrow= hssfsheet.getRow(0);//第一行
JavaScript Upload & Download
JavaScript Upload & Download//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
JavaScript Upload & DownloadJavaScript Upload & Download for (int i=0; i< hssfworkbook.getNumberOfSheets(); i++)JavaScript Upload & Download{
JavaScript Upload & Download hssfsheet= hssfworkbook.getSheetAt(i);
JavaScript Upload & Download
JavaScript Upload & Download //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
JavaScript Upload & DownloadJavaScript Upload & Download for (int j=1; j< hssfsheet.getPhysicalNumberOfRows(); j++)JavaScript Upload & Download{
JavaScript Upload & Download hssfrow= hssfsheet.getRow(j);
JavaScript Upload & Download //判断是否还存在需要导入的数据
JavaScript Upload & DownloadJavaScript Upload & Download if (hssfrow==null)JavaScript Upload & Download{
JavaScript Upload & Download System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
JavaScript Upload & Download break;
JavaScript Upload & Download }
JavaScript Upload & DownloadJavaScript Upload & Download /** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
JavaScript Upload & DownloadJavaScript Upload & Download if (hssfrow.getCell((short)0)==null)JavaScript Upload & Download{
JavaScript Upload & Download name="";
JavaScript Upload & DownloadJavaScript Upload & Download }elseif (hssfrow.getCell((short)0).getCellType()==0)JavaScript Upload & Download{
JavaScript Upload & Download name=new Double(hssfrow.getCell((short)0).getNumericCellValue()).toString();
JavaScript Upload & Download }
JavaScript Upload & Download //如果EXCEL表格中的数据类型为字符串型
JavaScript Upload & DownloadJavaScript Upload & Download elseJavaScript Upload & Download{
JavaScript Upload & Download name= hssfrow.getCell((short)0).getStringCellValue().trim();
JavaScript Upload & Download }
JavaScript Upload & DownloadJavaScript Upload & Download /** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
JavaScript Upload & Download //姓名
JavaScript Upload & DownloadJavaScript Upload & Download if(hssfrow.getCell((short)1)==null)JavaScript Upload & Download{
JavaScript Upload & Download sex="";
JavaScript Upload & DownloadJavaScript Upload & Download }elseif(hssfrow.getCell((short)1).getCellType()==0)JavaScript Upload & Download{
JavaScript Upload & Download sex=new Double(hssfrow.getCell((short)1).getNumericCellValue()).toString();
JavaScript Upload & Download }
JavaScript Upload & Download //如果EXCEL表格中的数据类型为字符串型
JavaScript Upload & DownloadJavaScript Upload & Download elseJavaScript Upload & Download{
JavaScript Upload & Download sex= hssfrow.getCell((short)1).getStringCellValue().trim();
JavaScript Upload & Download }
JavaScript Upload & DownloadJavaScript Upload & Download /** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
JavaScript Upload & Download //姓名
JavaScript Upload & DownloadJavaScript Upload & Download if(hssfrow.getCell((short)1)==null)JavaScript Upload & Download{
JavaScript Upload & Download age="";
JavaScript Upload & DownloadJavaScript Upload & Download }elseif(hssfrow.getCell((short)1).getCellType()==0)JavaScript Upload & Download{
JavaScript Upload & Download
网站名称:JavaScriptUpload&Download
链接地址:http://cqcxhl.com/article/posiih.html
在线咨询
服务热线
服务热线:028-86922220
TOP