步骤:
(1)运用Apache POI ,导入对应的jar包
(2)创建整个工作簿
(3)在工作簿中创建工作表
(4)在工作表中创建行
(5)在行中创建单元格
(6)设置单元格的值
(7)通过字节输出流进行打印
源码:
package com.mgl.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.FileOutputStream;
import java.io.IOException;
/**
* Tony 的 java
**/
public class Demo01 {
public static void main(String[] args) throws IOException {
//进行文件的解析
//1.导入jar包
//2.包含内容: (1)整个表格:工作簿XSSFWorkbook对象 (2)一个表格XSSFSheet
// (3)行:row (4)单元格:Cell
//创建一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//创建要给工作表
XSSFSheet sheet = workbook.createSheet();
//创建一行(多行)
XSSFRow row = sheet.createRow(0);
XSSFRow row2 = sheet.createRow(1);
//创建单元格,创建多个单元格
XSSFCell cell1 = row.createCell(0);
XSSFCell cell2 = row.createCell(1);
XSSFCell cell3 = row2.createCell(0);
XSSFCell cell4 = row2.createCell(1);
//设置单元格的值
cell1.setCellValue("123");
cell2.setCellValue("abc");
cell3.setCellValue("你好");
cell4.setCellValue("世界");
System.out.println(workbook);
//用字节 输出流进行打印
FileOutputStream fos = new FileOutputStream("bb.xlsx");
workbook.write(fos);
System.out.println("执行成功");
}
}