1、导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
2、写一个表格的工具类ExcelUtil
public class ExcelUtil {
public static void ExcelUtil(){
//创建表格对象
HSSFWorkbook wb = new HSSFWorkbook();
//通过表格创建sheet
HSSFSheet sheet = wb.createSheet("系统参数列表");
//创建表头
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("用户名");
header.createCell(1).setCellValue("计算机名");
header.createCell(2).setCellValue("计算机域名");
header.createCell(3).setCellValue("Ip");
header.createCell(4).setCellValue("主机名称");
//插入一行表格数据,大家可以建一个been,根据实际需求,直接get设置,
row.createCell(0).setCellValue("插入的内容");
row.createCell(1).setCellValue("插入的内容");
row.createCell(2).setCellValue("插入的内容");
row.createCell(3).setCellValue("插入的内容");
row.createCell(4).setCellValue("插入的内容");
//将wb的exl中的对象写入到文件对象中,表格名字为日期不重复不覆盖
SimpleDateFormat sdfms = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String date = sdfms.format(System.currentTimeMillis());
String data=date+".xls";
//文件路径
File file = new File("H://abc",data);
try {
wb.write(file);
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3、主函数测试一下
public class DemoMain {
public static void main(String[] args) throws IOException {
ExcelUtil.ExcelUtil();
}
}
4、完成可以带对应文件夹去查看