基于easyui利用poi技术实现导出,导入,导出text功能

来源:互联网 发布:js typeof5类型 编辑:程序博客网 时间:2024/05/17 08:49

首先是针对前端的利用ajax请求,我这里用的是play框架做的

 <a href="#" class="easyui-linkbutton" id="id_cancel " onclick="AutomateExcel();"  iconcls="icon-print" plain="true">导出Excel</a>

 function AutomateExcel() {
     
        $.ajax({
            type: "POST",
            url: "index8",
            success: function (data) {
                if (data == "1") {
                   alert("导出成功");
                } else if (data == "-1") {
                   alert("导出失败");
                }
            }
        });
    }

然后进入相关后台进行相关的操作

建议了解下poi和excel的相关知识i\

以下是单表进行相关的操作,对于多表操作就是list保存的时候改为利用MAP来储存,根据他的key得到相关的value值,其他的都一样;

public  static void index8(){
//单表导出
try{
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheets
HSSFSheet sheet = wb.createSheet("人员信息");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//设置列的宽度
sheet.setColumnWidth(4, 3000);
sheet.setColumnWidth(5, 3000);
sheet.setColumnWidth(8, 3000);
sheet.setColumnWidth(9, 3000);

for(int j =1;j<8;j++){
sheet.setColumnWidth(9+j, 3500);
}


HSSFCell cell = row.createCell(0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("密码");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("性别");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("住址");

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<Look> lists = Look.findAll();
 
for(int i =0;i<lists.size();i++){
row = sheet.createRow((int) i + 1);
// 第四步,创建单元格,并设置值
String no = lists.get(i).no;
String name = lists.get(i).name;
String pwd = lists.get(i).pwd;
String sex = lists.get(i).sex;
String age = lists.get(i).age;
String address = lists.get(i).address;
row.createCell(0).setCellValue(no);
row.createCell(1).setCellValue(name);
row.createCell(2).setCellValue(pwd);
if("1".equals(sex)){
row.createCell(3).setCellValue("男");
}else{
row.createCell(3).setCellValue("女");
}
row.createCell(5).setCellValue(age);
row.createCell(6).setCellValue(address);
}
// 第六步,将文件存到指定位置
FileOutputStream fout = new FileOutputStream( "C:/cc/workbook1.xls");
wb.write(fout);
fout.close();

renderText("1");
} catch (Exception e) {
e.printStackTrace();
renderText("-1");
}
}

针对导入excel和导出是大同小异的

//导入excel
public static void main(String[] args) throws IOException{
String filePath = "C:/cc/workbook1.xls";
    //判断是否为excel类型文件
    if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx"))
    {
      System.out.println("文件不是excel类型");
    }
    //文件输入流
    FileInputStream fis =null;
    //创建Workbook 对应一个EXCEL文件
    Workbook wookbook = null;
    try
    {
      //获取一个绝对地址的流
        fis = new FileInputStream(filePath);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    try 
    {
      //2003版本的excel,用.xls结尾
      wookbook = new HSSFWorkbook(fis);//得到工作簿
       
    } 
    catch (Exception ex) 
    {
      //ex.printStackTrace();
      try
      {
        //2007版本的excel,用.xlsx结尾
        
        wookbook = new XSSFWorkbook(fis);//得到工作簿
      } catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
   
    //得到一个工作表
    Sheet sheet = wookbook.getSheetAt(0);
    //获得表头
    Row rowHead = sheet.getRow(0);
    //判断表头是否正确
    if(rowHead.getPhysicalNumberOfCells() != 3)
    {
      System.out.println("表头的数量不对!");
    }
    //获得数据的总行数
    int totalRowNum = sheet.getLastRowNum();
    //要获得属性
    String name = "";
    String latitude = "";
    List<Look> list=null;
     //获得所有数据
    //获得第i行对象
    Row row =null;
    for(int i = 1 ; i <= totalRowNum ; i++)
    {
      //获得第i行对象
       row = sheet.getRow(i);
     
      //获得获得第i行第0列的 String类型对象
      Cell cell = row.getCell((short)0);
      name = i+cell.getStringCellValue().toString();
      //获得一个数字类型的数据
      cell = row.getCell((short)1);
      latitude =i+ cell.getStringCellValue().toString();
      System.out.println("读取行:" + row.getRowNum()); 
      System.out.println("编号:"+name+",姓名:"+latitude);
//       list=new ArrayList<Look>();
//      Look a =new Look();
//      a.no=name;
//      a.name=latitude;
//      list.add(a);
//      }
//    FileWriter output = new FileWriter("D:test1.txt");
// BufferedWriter bf = new BufferedWriter(output);
// for (Look l : list) {
// bf.write(l.no+"\t\n"+l.name);
// }
//   bf.flush();// 此处很关键,如果不写该语句,是不能从缓冲区写到文件里的
//    
    }
  }

以下为导出为text文件的相关内容,因为数据源很多,根据自己的需求进行相关操作

List<Look> lists = Look.findAll();
try {
FileWriter output = new FileWriter("D:test.txt");
BufferedWriter bf = new BufferedWriter(output);

for (Look l : lists) {
bf.write(l.no+"\t\n"+l.name+"\t\n"+l.pwd+l.sex+"\t\n"+l.address);
}
bf.flush();// 此处很关键,如果不写该语句,是不能从缓冲区写到文件里的
renderText("1");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
renderText("-1");
}




}