import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.ArrayList; import java.util.List; public class WriteExcel { /** * 向Excel中寫入數據 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { List<Student> stuList = new ArrayList<Student>(); stuList.add(new Student(12,"lili","深圳南山")); stuList.add(new Student(13,"liming","深圳寶安")); stuList.add(new Student(14,"chengming","深圳羅湖")); String filePath = "E:\\ExcelData.xlsx"; boolean flag = fileExist(filePath); if (flag){ writeExcel(stuList,filePath); }else { File file = new File(filePath); writeExcel(stuList,filePath); } } //判斷文件是否存在 public static boolean fileExist(String filePath){ boolean flag = false; File file = new File(filePath); flag = file.exists(); return flag; } //向Excel中寫數據 public static void writeExcel(List<Student> list ,String filePath){ XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("student"); XSSFRow firstRow = sheet.createRow(0);//第一行表頭 XSSFCell cells[] = new XSSFCell[3]; String[] titles = new String[]{"age","name","address"}; //循環設置表頭信息 for (int i=0;i<3;i++){ cells[0]=firstRow.createCell(i); cells[0].setCellValue(titles[i]); } //遍歷list,將數據寫入Excel中 for (int i=0;i<list.size();i++){ XSSFRow row = sheet.createRow(i+1); Student student = list.get(i); XSSFCell cell = row.createCell(0); //第一列 cell.setCellValue(student.getAge()); cell=row.createCell(1); //第二列 cell.setCellValue(student.getName()); cell=row.createCell(2); //第三列 cell.setCellValue(student.getAddress()); } OutputStream out = null; try { out = new FileOutputStream(filePath); workbook.write(out); out.close(); } catch (Exception e){ e.printStackTrace(); } } }
效果:
二、向Excel中追加數據
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; public class AddExcel { /** * 向Excel中追加內容 * @param args * @throws Exception */ public static void main(String[] args) throws Exception{ List<Student> stuList2 = new ArrayList<Student>(); stuList2.add(new Student(15,"小明","深圳南山")); stuList2.add(new Student(16,"小王","深圳寶安")); stuList2.add(new Student(17,"小張","深圳羅湖")); FileInputStream in = new FileInputStream("E:\\ExcelData.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(in); XSSFSheet sheet = workbook.getSheetAt(0); XSSFRow row=sheet.getRow(1); FileOutputStream out = new FileOutputStream("E:\\ExcelData.xlsx"); //從第二行開始追加列 /*row=sheet.getRow(1); row.createCell(3).setCellValue("AAA"); row.createCell(4).setCellValue("BBB");*/ //追加列數據 for(int i=0;i<stuList2.size();i++){ Student student = stuList2.get(i); row = sheet.getRow(i+1); row.createCell(3).setCellValue(student.getAge()); row.createCell(4).setCellValue(student.getName()); row.createCell(5).setCellValue(student.getAddress()); } /*//追加行數據 row=sheet.createRow((short)(sheet.getLastRowNum()+1)); //在現有行號后追加數據 row.createCell(0).setCellValue("測試數據"); //設置第一個(從0開始)單元格的數據 row.createCell(1).setCellValue("haha"); //設置第二個(從0開始)單元格的數據*/ try { out.flush(); workbook.write(out); out.close(); }catch (Exception e){ e.printStackTrace(); } } }
效果: