读取个Excel文档,再根据条件输出到txt里,怎么控制输出格式呢,希望大神帮忙,了啦啦啦啦啦!
4条回答 默认 最新
关注 用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();
}
}}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 基于卷积神经网络的声纹识别
- ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
- ¥100 为什么这个恒流源电路不能恒流?
- ¥15 有偿求跨组件数据流路径图
- ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
- ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
- ¥15 CSAPPattacklab
- ¥15 一直显示正在等待HID—ISP
- ¥15 Python turtle 画图
- ¥15 stm32开发clion时遇到的编译问题