一.文件上传到数据库
1.Jquery相关文件a.jquery-1.8.3.jsb.jquery.ocupload-1.1.2.js2.poi相关jar包,可从网上下载核心包:poi-3.9-20121203.jar(版本可根据需要选择)相关包:根据需要选择是否导入3.上传学生表Student实例及相关代码
//学生模型
public class Student{
private String name;
private String gender;
private int age;
......
}
- //对应数据库表t_student
第一步:前端定义上传按钮
- <input type="button" id="upload" value = "上传"/>
第二步:前端调用js
action:"uploadAction_test.action"//上传请求 name:filename//上传文件名称 params:{}//传递请求参数,可选 onSelect:function(selef,element){}//根据需要选择触发事件 onComplete:function (data, self, element) {}//上传完毕后的回调
- $(function(){
- $("#upload").upload({
- });
- });
第三步:服务端处理action请求
- public class uploadAction extends ActionSupport implements ModelDriven<Sudent> {
- //模型驱动
- private Studnet student = new Student();
- public Studnet getModel() {
- return student;
- }
- private File filename;//对应js中的filename
- public void setFilename(File filename) {
- this.filename=filename;
- }
- public String test ()
- //定义list集合用来封装存储数据
- List<Studnet> list = new ArrayList<Studnet> ();
- //关联excle表,创建excel操作对象
- HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
- //拿到excel表中的sheet
- HSSFSheet sheetAt = workbook.getSheetAt(0);
// 循环遍历sheet,拿到表中的每一行 for (Row row : sheetAt) { //去掉标题行 if(row.getRowNum()==0){ continue; } //拿到每一行并封装成对象,注意拿到的都是Sting类型 String name = row.getCell(0).getStringCellValue(); String age = row.getCell(1).getStringCellValue();
String gender = row.getCell(2).getStringCellValue();
//封装成对象
Student student = new Student (name,Integer.parseInt(age),gender)
//存储到集合
list.add(student);
}
//调用服务层,将集合中的元素取出存到数据库即可
new service().importXls(list);
}
......省略服务层以及dao层的代码
二.导出下载数据到excel 表
第一步:前端下载请求
<input type="button" id = "download" value="导出"/>
$(function(){
$("#download").click(function(){
window.location.href="downloadAction_exportXls.action";
});
});
第二步:服务端处理请求
public class DownloadAction extends ActionSupport impelemts ModelDriven<Student> {
public String exportXls() {
//通过服务层和dao层,查询数据库,获得需要导出的数据
List<Studnet> list = daoImpl.getStudnets()//简写直接调用dao
//创建excel操作对象
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet = workbook.createSheet("学生数据");
//设置表格的标题行
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("姓名");
headRow.createCell(1).setCellValue("年龄");
headRow.createCell(2).setCellValue("性别");
//遍历集合,将每一条记录写入excel表
for (Student student : list) {
HSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
row.createCell(0).setCellValue(studetn.getName());
row.createCell(1).setCellValue(subarea.getAge());
row.createCell(2).setCellValue(subarea.getGender());
}
//创建输出流
ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
//下载的文件名
String filename = "学生数据.xsl"
//利用工具类处理文件名,做到浏览器兼容
filename=FileUtils.encodeDownloadFilename(filename,ServletActionContext.getRequest().getHead("user-agent");
//设置响应头
ServletActionContext.getResponse().setContentType(ServletActionContext.getServletContext.getMimeType(filename));
ServletActionContext.getRespone().setHeader("content-disposition","attachement;filename="+filename);
workbook.write(os);
}
}
//文件工具类 public class FileUtils { /** * 下载文件时,针对不同浏览器,进行附件名的编码 * * @param filename * 下载文件名 * @param agent * 客户端浏览器 * @return 编码后的下载附件名 * @throws IOException */ public static String encodeDownloadFilename(String filename, String agent) throws IOException { if (agent.contains("Firefox")) { // 火狐浏览器 filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?="; filename = filename.replaceAll("\r\n", ""); } else { // IE及其他浏览器 filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+"," "); } return filename; } }