//1.写入excel
Workbook();
结果如下图: 详见代码文章末尾代码内方法: Workbook();
//2.读取excel
Readbook();
结果如下图: 详见代码文章末尾代码内方法: Readbook();
//3.写入车辆excel---------------该方法为实战用例
Ceatbook();
结果如下图: 详见代码文章末尾代码内方法: Ceatbook();
//4.list循环写入车辆excel---------------该方法为实战用例
ForCeatbook();
结果如下图: 详见代码文章末尾代码内方法: ForCeatbook();
- public class Excelpoi {
-
- public static void main(String[] args) throws IOException {
- //1.写入excel
- Workbook();
- //2.读取excel
- Readbook();
- //3.写入车辆excel
- Ceatbook();
- //4.循环写入车辆excel
- ForCeatbook();
- }
-
- /**
- * 写入excel
- *
- * @param
- * @throws IOException
- */
- public static void Workbook() throws IOException {
- //1.打开excel
- XSSFWorkbook workbook = new XSSFWorkbook();
- //2.创建sheet页
- XSSFSheet sheet1 = workbook.createSheet("sheet1");//sheet:工作表
- //3.创建行,索引从0开始
- XSSFRow row = sheet1.createRow(0);//row 行
- //4.创建单元格,索引从0开始
- XSSFCell cell = row.createCell(0);//cell:单元格
- //5.向单元格中插入数据
- cell.setCellValue("你好");
- //6.将数据写入文件,相当于另存为
- workbook.write(new FileOutputStream("F:\\ExcelPoi\\excelpoi写入.xlsx"));
- System.out.println("生成excel成功");
-
- }
-
- /**
- * 读取excel
- *
- * @param
- * @throws IOException
- */
- public static void Readbook() throws IOException {
- //1.使用excel打开指定的xlsx文件
- XSSFWorkbook fw = new XSSFWorkbook("F:\\ExcelPoi\\excelpoi写入.xlsx");
- //2.获取第一个sheet页
- XSSFSheet sheet1 = fw.getSheetAt(0);
- //3.获取第0行
- XSSFRow row = sheet1.getRow(0);
- //4.获取第0个单元格
- XSSFCell cell = row.getCell(0);
- //5.获取单元格中的数据
- System.out.println(cell.getStringCellValue());
- System.out.println("读取成功");
-
-
- }
-
- /**
- * 创建一个车辆excel
- *
- * @throws IOException
- */
- public static void Ceatbook() throws IOException {
- /*1.创建EXCEL文件*/
- XSSFWorkbook workbook = new XSSFWorkbook();
-
- /*2.创建sheet页面*/
- XSSFSheet sheet1 = workbook.createSheet("sheet1");
- sheet1.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
-
- /*3.创建车辆信息头*/
- XSSFRow row = sheet1.createRow(0);
- XSSFCell cell = row.createCell(0);
- XSSFCellStyle textAlignCenter = workbook.createCellStyle();
- textAlignCenter.setAlignment(HorizontalAlignment.CENTER);//横着对其
- textAlignCenter.setVerticalAlignment(VerticalAlignment.CENTER);//上下对齐
- cell.setCellStyle(textAlignCenter);
- cell.setCellValue("车辆信息");
-
- /*4.创建表头*/
- XSSFRow titleRow = sheet1.createRow(1);
- XSSFCell idCell = titleRow.createCell(0);
- idCell.setCellValue("编号");
- XSSFCell nameCell = titleRow.createCell(1);
- nameCell.setCellValue("车牌");
- XSSFCell ageCell = titleRow.createCell(2);
- ageCell.setCellValue("颜色");
-
-
- /*5.创建数据*/
- XSSFRow dataRow1 = sheet1.createRow(2);
- XSSFCell idCell1 = dataRow1.createCell(0);
- idCell1.setCellValue("1");
- XSSFCell nameCell1 = dataRow1.createCell(1);
- nameCell1.setCellValue("京A11111");
- XSSFCell ageCell1 = dataRow1.createCell(2);
- ageCell1.setCellValue("black");
-
- XSSFRow dataRow2 = sheet1.createRow(3);
- XSSFCell idCell2 = dataRow2.createCell(0);
- idCell2.setCellValue("2");
- XSSFCell nameCell2 = dataRow2.createCell(1);
- nameCell2.setCellValue("京A22222");
- XSSFCell ageCell2 = dataRow2.createCell(2);
- ageCell2.setCellValue("red");
-
-
- workbook.write(new FileOutputStream("F:\\ExcelPoi\\车辆信息.xlsx"));
-
- System.out.println("车辆写入成功");
-
-
- }
-
- /**
- * 循环创建一个车辆excel
- *
- * @throws IOException
- */
- public static void ForCeatbook() throws IOException {
- /*1.创建EXCEL文件*/
- XSSFWorkbook workbook = new XSSFWorkbook();
-
- /*2.创建sheet页面*/
- XSSFSheet sheet1 = workbook.createSheet("sheet1");
- sheet1.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
-
- /*3.创建车辆信息头*/
- XSSFRow row = sheet1.createRow(0);
- XSSFCell cell = row.createCell(0);
- XSSFCellStyle textAlignCenter = workbook.createCellStyle();
- textAlignCenter.setAlignment(HorizontalAlignment.CENTER);
- textAlignCenter.setVerticalAlignment(VerticalAlignment.CENTER);
- cell.setCellStyle(textAlignCenter);
- cell.setCellValue("车辆信息");
-
- /*4.创建表头*/
- XSSFRow titleRow = sheet1.createRow(1);
- XSSFCell idCell = titleRow.createCell(0);
- idCell.setCellValue("编号");
- XSSFCell nameCell = titleRow.createCell(1);
- nameCell.setCellValue("车牌");
- XSSFCell ageCell = titleRow.createCell(2);
- ageCell.setCellValue("颜色");
-
-
-
-
- /*5.创建数据*/
- ArrayList
vehicle = new ArrayList<>(); - vehicle.add(new Vehicle(1, "京A11111", "black"));
- vehicle.add(new Vehicle(2, "京A22222", "white"));
- vehicle.add(new Vehicle(3, "京A33333", "red"));
- vehicle.add(new Vehicle(4, "京A44444", "yellow"));
-
-
- for (int i = 0; i < vehicle.size(); i++) {
- XSSFRow dataRow1 = sheet1.createRow(2 + i);
- XSSFCell idCell1 = dataRow1.createCell(0);
- idCell1.setCellValue(vehicle.get(i).getId());
- XSSFCell nameCell1 = dataRow1.createCell(1);
- nameCell1.setCellValue(vehicle.get(i).getPlateNo());
- XSSFCell ageCell1 = dataRow1.createCell(2);
- ageCell1.setCellValue(vehicle.get(i).getColor());
- }
-
- workbook.write(new FileOutputStream("F:\\ExcelPoi\\循环车辆信息.xlsx"));
-
- System.out.println("循环车辆写入成功");
-
-
- }
- }