http://www.blogjava.net/hawk8359/archive/2008/02/19/180623.html[@more@]

{


/**//*

* 把数据库中的字段导入到Excel ,并生成Excel文档

**/

public ActionForward getDownload(ActionMapping actionMapping,

ActionForm actionForm, HttpServletRequest request,


HttpServletResponse response)throws Exception

{

Form fm= (Form) actionForm;

// Excel 文件存放在
服务器的相对路径下

String outputFile= request.getRealPath("/tmp/Excel.xls");


try

{

// 创建新的Excel 工作簿

HSSFWorkbook workbook=new HSSFWorkbook();

// 在Excel 工作簿中建一工作表

HSSFSheet sheet= workbook.createSheet("Sheet1");

// 设置单元格格式(文本)

HSSFCellStyle cellStyle= workbook.createCellStyle();

cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));

// 在索引0的位置创建行(第一行)

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();// 查询结果存入rs

connection.commit();// 执行SQL


while (rs.next())

{

//设置j行从第二行开始

int j=1;

row= sheet.createRow((short) j);

//设置i列从第二列开始


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();

}

// 删除路径下同名的Excel 文件

File path=new File(outputFile);

path.delete();

// 新建一输出文件流

FileOutputStream fOut=new FileOutputStream(outputFile);

// 把相应的Excel 工作簿存盘

workbook.write(fOut);

// 操作结束,关闭文件

fOut.flush();

fOut.close();

//该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空)

request.getSession().setAttribute("Download", outputFile);


}catch (Exception ioexception)

{

request.setAttribute("message","创建文件失败!");

return actionMapping.findForward("outJSP");

}

return actionMapping.findForward("outJSP");

}


/**//*

* 从Excel文件中读取数据,并导入到数据库中

**/

public ActionForward getUpload(ActionMapping actionMapping,

ActionForm actionForm, HttpServletRequest request,


HttpServletResponse response)throws Exception

{

// 获取excel 文件

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

{

//通过得到的文件输入流inputstream创建一个HSSFWordbook对象

HSSFWorkbook hssfworkbook=new HSSFWorkbook(inputstream);

HSSFSheet hssfsheet= hssfworkbook.getSheetAt(0);//第一个工作表

HSSFRow hssfrow= hssfsheet.getRow(0);//第一行

//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数


for (int i=0; i< hssfworkbook.getNumberOfSheets(); i++)

{

hssfsheet= hssfworkbook.getSheetAt(i);

//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数


for (int j=1; j< hssfsheet.getPhysicalNumberOfRows(); j++)

{

hssfrow= hssfsheet.getRow(j);

//判断是否还存在需要导入的数据


if (hssfrow==null)

{

System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");

break;

}


/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/


if (hssfrow.getCell((short)0)==null)

{

name="";


}elseif (hssfrow.getCell((short)0).getCellType()==0)

{

name=new Double(hssfrow.getCell((short)0).getNumericCellValue()).toString();

}

//如果EXCEL表格中的数据类型为字符串型


else

{

name= hssfrow.getCell((short)0).getStringCellValue().trim();

}


/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/

//姓名


if(hssfrow.getCell((short)1)==null)

{

sex="";


}elseif(hssfrow.getCell((short)1).getCellType()==0)

{

sex=new Double(hssfrow.getCell((short)1).getNumericCellValue()).toString();

}

//如果EXCEL表格中的数据类型为字符串型


else

{

sex= hssfrow.getCell((short)1).getStringCellValue().trim();

}


/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/

//姓名


if(hssfrow.getCell((short)1)==null)

{

age="";


}elseif(hssfrow.getCell((short)1).getCellType()==0)

{
当前名称:JavaScriptUpload&Download
本文链接:
http://njwzjz.com/article/posiih.html