1.poi 读写excel

1.准备eclipse

2.配置maven

3.导入poi的jar包

pom文件添加

<!-- 操作excel -->
  <dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.14</version>
  </dependency>
  </dependencies>



源码如下:


package test.pom.api;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class Test {
public static void main(String[] args) {
System.out.println("111");
String excelPath = "C:/Users/yvon/Desktop/1.xlsx";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(excelPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建一个工作簿
Workbook wb = new HSSFWorkbook();
// 创建sheet页
Sheet Sheet1 = wb.createSheet("三年级(1)班学生名单");
/* Sheet Sheet2 = wb.createSheet("三年级(2)班学生名单"); */
/* Sheet Sheet3 = wb.createSheet("三年级(3)班学生名单"); */


// 使用Sheet创建行
Row Row1 = Sheet1.createRow(0);


/*
* Cell c1 = Row1.createCell(0); c1.setCellValue(6);

* Cell c2 = Row1.createCell(1); c2.setCellValue(1.2);

* Cell c3 = Row1.createCell(2); c3.setCellValue("我爱宝宝金!!!");
*/


for (int i = 0; i < 10; i++) {
Cell c3 = Row1.createCell(i);
c3.setCellValue("我爱宝宝金!!!");
}


try {
// 写出到目标文件
wb.write(fos);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


getWorkbook();
}


public static void getWorkbook() {
String excelPath = "C:/Users/yvon/Desktop/1.xlsx";
FileInputStream fis = null;
POIFSFileSystem fs = null;
Workbook wb1 = null;
try {
fis = new FileInputStream(excelPath);
// 将文件流解析成 POI 文档
fs = new POIFSFileSystem(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


try {
wb1 = new HSSFWorkbook(fs);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
* Sheet Sheet2 = wb1.createSheet("三年级(2)班学生名单"); Row Row1 =
* Sheet2.createRow(0);
*/
Sheet Sheet2 = wb1.getSheet("三年级(1)班学生名单");
Row Row1 = Sheet2.getRow(0);


for (int i = 0; i < 10; i++) {
Cell c3 = Row1.getCell(i);
// c3.setCellValue("我爱宝宝金!!!");
System.out.println(c3.getStringCellValue());
}
try {


fis.close();
wb1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值