package excelDemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 20));
list.add(new Student("张三1", 23));
list.add(new Student("张三2", 24));
write(list);
}
public static void write(List<Student> list) {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet名称");
for(int i = 0;i<list.size();i++) {
Row row = sheet.createRow(i);
Cell name = row.createCell(0);
Cell age = row.createCell(1);
name.setCellValue(list.get(i).getName());
age.setCellValue(list.get(i).getAge());
}
OutputStream os = null;
try {
os = new FileOutputStream(new File("C:\\Users\\Windows\\Desktop\\demo.xlsx"));
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}