import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.*;
public class POITest {
//使用POI向Excel文件写入数据,并且通过输出流将创建的文件写出
@Test
public void test3() throws IOException {
//在内存中创建一个文件
XSSFWorkbook excel = new XSSFWorkbook();
XSSFSheet sheet = excel.createSheet("深圳十大好公司");
//在工作表中来创建行对象
XSSFRow title = sheet.createRow(0);
//在行中创建单元格对象
title.createCell(0).setCellValue("公司名");
title.createCell(1).setCellValue("地址");
title.createCell(2).setCellValue("公司年龄");
XSSFRow dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("腾讯");
dataRow.createCell(1).setCellValue("深圳南山");
dataRow.createCell(2).setCellValue(23);
XSSFRow dataRow2 = sheet.createRow(2);
dataRow2.createCell(0).setCellValue("华为");
dataRow2.createCell(1).setCellValue("深圳龙岗");
dataRow2.createCell(2).setCellValue(28);
//创建一个输出流,通过输出流将内存中的excell文件写到磁盘
FileOutputStream out = new FileOutputStream(new File("e:\\hello.xlsx"));
excel.write(out);
out.flush();
excel.close();
}
}
使用POI向Excel文件写入数据,并且通过输出流将创建的文件写出
最新推荐文章于 2024-05-14 06:33:05 发布