Ethon

为什么要有方法,因为懒惰是一种美德。

   :: 首页  :: 新随笔  ::  ::  :: 管理
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();
        }
    }
}

 效果:

 

posted on 2019-08-11 15:52  Ethon  阅读(23642)  评论(1编辑  收藏  举报