java poi 创建Excel表格 并 插入数据

java poi 创建Excel表格 并 插入数据

首先引入poi jar包
poi.xml

  <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>
 XSSFWorkbook workBook = new XSSFWorkbook();   //new一个表
 XSSFSheet sheet = workBook.createSheet("sheet"); //添加一个sheet
 //创建行
XSSFRow row = sheet.createRow(0);
 //创建单元格,第一行标题
 XSSFCell cell0 = row.createCell(0);
 XSSFCell cell1 = row.createCell(1);
 XSSFCell cell2 = row.createCell(2);
 XSSFCell cell3 = row.createCell(3);
 XSSFCell cell4 = row.createCell(4);
 XSSFCell cell5 = row.createCell(5);
 XSSFCell cell6 = row.createCell(6);
 XSSFCell cell7 = row.createCell(7);
 XSSFCell cell8 = row.createCell(8);
 XSSFCell cell9 = row.createCell(9);
 cell0.setCellValue("标题1");
 cell1.setCellValue("标题2");
 cell2.setCellValue("标题3");
 cell3.setCellValue("标题4");
 cell4.setCellValue("标题5");
 cell5.setCellValue("标题6");
 cell6.setCellValue("标题7");
 cell7.setCellValue("标题8");
 cell8.setCellValue("标题9");
 cell9.setCellValue("标题10");
 cell6.setCellType(HSSFCellStyle.ALIGN_LEFT);  //单元格内容居左
//添加更多行同上原理一样

 FileOutputStream outputStream = new FileOutputStream(new File("url"+ "文件名"));//url和文件名换成自己的
 workBook.write(outputStream); //输出文件
 workBook.close();//关闭工作簿

自己项目实例,绝对有效

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值