讀取個Excel文檔,再根據條件輸出到txt裡,怎麼控制輸出格式呢,希望大神幫忙,了啦啦啦啦啦!
用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();
}
}
}