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

来源:互联网 发布:一根网线接电话和网络 编辑:程序博客网 时间:2024/06/11 00:25
导入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