poi读写Excel文件,兼容新旧版本的简单例子
来源:互联网 发布:一根网线接电话和网络 编辑:程序博客网 时间:2024/09/21 10:55
导入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();
}
}
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
- poi读写Excel文件,兼容新旧版本的简单例子
- Java POI 读写Excel 文件简单实现
- POI简单读写excel
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写excel文件
- poi读写Excel文件
- POI读写Excel文件
- poi读写Excel文件
- Poi读写Excel文件
- POI导出到Excel的简单例子
- POI导出到Excel的简单例子
- POI导出到Excel的简单例子
- POI导出到Excel的简单例子
- 使用PYTHON3000读写Excel文件简单例子
- 剑指offer-面试题4 替换空格
- Android系统架构——揭开Android系统框架的面纱
- 荣耀6 Plus执行adb dumpsys中正在运行的service带有解释说明
- 通过Bmob云后端来实现一个完整的app
- 面试体会
- poi读写Excel文件,兼容新旧版本的简单例子
- 多态的理解(父类引用指向子类对象)
- C++作业3
- 1001. A+B Format (20)
- javascript简单算法
- 政府采购的主体
- 2017-03-23 中青宝 预计会低开
- c#学习笔记_结构体
- RabbitMQ基础内容详细介绍