poi读写Excel文件,兼容新旧版本的简单例子

导入jar包(poi)后,即可操作
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ExcelUtils {
public static void main(String[] args) throws IOException {
ExcelUtils ef = new  ExcelUtils();
String path = "C:\\Users\\ASUS\\Desktop\\12.xlsx";
String dirPath = "C:\\Users\\ASUS\\Desktop\\13.xlsx";
ef.readExcel(path,dirPath);
ef.create(dirPath);
}
/**
* 读取excel文件并将值写入到制定的表格中
* @param path 
* @throws IOException
*/
public void readExcel(String path,String dirPath) throws IOException {
Workbook workbook = null;
if (null != path) {
String fileType = path.substring(path.lastIndexOf("."), path.length());
FileInputStream fileStream = new FileInputStream(new File(path));
if (".xls".equals(fileType.trim().toLowerCase())) {
workbook = new HSSFWorkbook(fileStream);// 创建 Excel 2003 工作簿对象
} else if (".xlsx".equals(fileType.trim().toLowerCase())) {
workbook = new XSSFWorkbook(fileStream);// 创建 Excel 2007 工作簿对象
}
}
System.out.println("总表页数为:" + workbook.getNumberOfSheets());
for(int i=0;i<workbook.getNumberOfSheets();i++){

//读数据
Sheet sheet = workbook.getSheetAt(0);
System.out.println("表单名字是:"+sheet.getSheetName()); 
int rownum = sheet.getLastRowNum();// 获取总行数
System.out.println(sheet.getSheetName()+"总行数:"+rownum);
for(int m=0;m<=rownum;m++){
Row row = sheet.getRow(m);// 获取表达的第i行
int cellNum = row.getLastCellNum();//获取改行列数
System.out.println("第"+i+"行有"+cellNum+"列");
for(int k=0;k<cellNum;k++){
//获取该行列
Cell cell = row.getCell(k);
//获取该列的值
System.out.println("第"+m+"行,第"+k+"列的值是:"+cell.getStringCellValue());
}

}
//写数据
sheet = workbook.getSheetAt(0);
System.out.println("表单名字是:"+sheet.getSheetName()); 
rownum = sheet.getLastRowNum();// 获取总行数
System.out.println(sheet.getSheetName()+"总行数:"+rownum);
for(int m=0;m<=rownum;m++){
Row row = sheet.getRow(m);// 获取表达的第i行
int cellNum = row.getLastCellNum();//获取改行列数
System.out.println("第"+i+"行有"+cellNum+"列");
for(int k=0;k<cellNum;k++){
//获取该行列
Cell cell = row.getCell(k);
cell.setCellValue("hdfhgdfg");
//获取该列的值
System.out.println("第"+m+"行,第"+k+"列的值是:"+cell.getStringCellValue());
}

}


}
File file = new File("C:\\Users\\ASUS\\Desktop\\13.xlsx");
OutputStream os = new FileOutputStream(file);
os.flush();
workbook.write(os);
os.close();
}
/**
* 创建Excel文件并另存到制定目录
* @param path 写出文件的地址
* @throws IOException
*/
public void create(String path) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("shootss");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("hhhh");
File file = new File(path);
OutputStream os = new FileOutputStream(file);
os.flush();
workbook.write(os);
os.close();
workbook.close();

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhijiesmile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值