lingting1995 2015-04-19 12:26 采纳率: 47.4%
浏览 1802
已采纳

读取个Excel文档,再根据条件输出到txt里,用Java写,求大神相助

读取个Excel文档,再根据条件输出到txt里,怎么控制输出格式呢,希望大神帮忙,了啦啦啦啦啦!

  • 写回答

4条回答 默认 最新

  • 漫长学习路 博客专家认证 2015-04-20 08:34
    关注

    用poi吧,先读取excel表格数据,然后再写入到txt里面;
    给你个案例你看看
    public class CreateExcel {
    private static List getstudent() throws Exception{
    List list=new ArrayList();
    SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd");
    Student stu=new Student(13, "张三", 14, df.parse("2015-3-3"));
    Student stu1=new Student(17, "张三", 18, df.parse("2015-3-4"));
    Student stu2=new Student(23, "张三", 21, df.parse("2015-3-5"));
    list.add(stu);
    list.add(stu1);
    list.add(stu2);
    return list;

    }
    public static void main(String[] args) {
    //第一步,创建一个webbook,对应一个excel文件
    HSSFWorkbook wb=new HSSFWorkbook();
    //第二步,在webbook中添加一个sheet,对应excel文件中的sheet
    HSSFSheet sheet=wb.createSheet("学生表一");
    //第三步,在sheet中添加表头第0行
    HSSFRow row=sheet.createRow((int)0);
    //第四步,创建单元格,并设置表头 设置表头居中
    HSSFCellStyle style=wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中表格
    HSSFCell cell=row.createCell((short)0);
    cell.setCellValue("学号");
    cell.setCellStyle(style);
    cell=row.createCell((short)1);
    cell.setCellValue("姓名");

    cell.setCellStyle(style);

    cell = row.createCell((short) 2);

    cell.setCellValue("年龄");

    cell.setCellStyle(style);

    cell = row.createCell((short) 3);

    cell.setCellValue("生日");

    cell.setCellStyle(style);
    //第五步,写入实体数据
    try {
    List list=CreateExcel.getstudent();
    for(int i=0;i<list.size();i++){
    row=sheet.createRow((int)i+1);
    Student str=(Student) list.get(i);
    //第六步创建单元格,并设置值
    row.createCell(0).setCellValue(str.getId());
    row.createCell(1).setCellValue(str.getName());
    row.createCell(2).setCellValue(str.getAge());
    row.createCell(3).setCellValue(new SimpleDateFormat("yyy-mm-dd").format(str.getBirth()));
    }
    //第七步将文件存到指定位置
    FileOutputStream fout=new FileOutputStream("D:/student.txt");
    wb.write(fout);
    fout.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作