首先需下载poi java包,添加至构建路径,
写处理方法:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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;
import java.awt.AWTException;
/*************************excel处理程序-开始*******************************************/
/**
* 获取Excel中的数据(Excel 2003)
* @param file 文件路径
* @param beginCell 开始列
* @param endCell 结束列
* @throws IOException
*/
public static List<List> getExcelValue(String file, int beginCell, int endCell)throws IOException{
InputStream is = new FileInputStream(file);
Workbook workbook = null ;
if(file.endsWith(".xls")){
workbook = new HSSFWorkbook(is);
}else if(file.endsWith(".xlsx")){
workbook = new XSSFWorkbook(is);
}
List<List> excelList = new ArrayList<List>();
Sheet sheet = workbook.getSheetAt(0); //操作第一个表格,其他的sheet不管
//按行循环
for(int rownum=0;rownum<=sheet.getLastRowNum();rownum++){
Row row = sheet.getRow(rownum);//获取行
List<String> rowList = new ArrayList<String>(); //每行的数据放一个List里
//每行中按列循环
for(int cellnum=beginCell;cellnum<=endCell;cellnum++){
Cell cell = row.getCell(cellnum); //获取操作的单元格
if(cell != null){ //单元格不为空,则将单元格中数据放入list中
rowList.add(getCellValue(cell));
}else{//单元格为空,则向list中放入空字符串
rowList.add("");
}
}
excelList.add(rowList);//将一行的数据放入excelList中
}
return excelList;
}
// }
/**
* 获取Excel中的数据(Excel 2003),未传递开始与结尾的列数,所以获取所有
* @param file 文件路径
* @throws IOException
*/
public static List<List> getExcelValue(String file)throws IOException{
InputStream is = new FileInputStream(file);
Workbook workbook = null ;
if(file.endsWith(".xls")){
workbook = new HSSFWorkbook(is);
}else if(file.endsWith(".xlsx")){
workbook = new XSSFWorkbook(is);
}
List<List> excelList = new ArrayList<List>();
Sheet sheet = workbook.getSheetAt(0); //操作第一个表格,其他的sheet不管
//按行循环
for(int rownum=0;rownum<=sheet.getLastRowNum();rownum++){
Row row = sheet.getRow(rownum);//获取行
int beginCell = 0;
int endCell = sheet.getRow(0).getPhysicalNumberOfCells();//获取该表格中最大列数
List<String> rowList = new ArrayList<String>(); //每行的数据放一个List里
//每行中按列循环
for(int cellnum=beginCell;cellnum<=endCell;cellnum++){
Cell cell = row.getCell(cellnum); //获取操作的单元格
if(cell != null){ //单元格不为空,则将单元格中数据放入list中
rowList.add(getCellValue(cell));
}else{//单元格为空,则向list中放入空字符串
rowList.add("");
}
}
excelList.add(rowList);//将一行的数据放入excelList中
}
return excelList;
}
// }
private static String getCellValue(Cell cell) {
if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf((int)cell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(cell.getStringCellValue());
}
}
/**
* 当List<List> 内容写入Excel中
* @param dataList
*/
public static void writeExcel(List<List> dataList, String filePath){
try{
FileOutputStream os = new FileOutputStream(filePath);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0); //行
HSSFCell cell = row.createCell(0); //列
//将所有数据以行为单位的进行循环
for(int i=0; i<dataList.size(); i++){ //循环行
List rowList = dataList.get(i);
row = sheet.createRow(i);
//将某行的所有数据以单元格为单位进行循环
for(int j=0; j<rowList.size(); j++){ //循环列
String cellValue = String.valueOf(rowList.get(j)); //获取某单元格内容
cell = row.createCell(j); //创建对应的单元格
cell.setCellValue(cellValue); //写入内容
}
}
workbook.write(os);
os.flush();
os.close();
}catch (Exception e) {
e.printStackTrace();
}
}
/*************************excel处理程序-结束*******************************************/
使用:
writeExcel(csyl_dataList, test_report_FilePath); //输出excel文件
来源:https://www.cnblogs.com/kitty-zhou/p/5439527.html