java读取读取excel文件并复制文件并复制(copy)文件到指定目录示例文件到指定目录示例
主要介绍了java读取excel文件并复制文件到指定目录示例,需要的朋友可以参考下
复制代码 代码如下:
mport java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
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;
public class DeployByExcel {
private static Logger logger= Logger.getLogger(DeployByExcel.class);
static final int BUFFER = 8192;
//Excel
private HSSFWorkbook workbook ;
/**
* 读取Excel文件并将文件列表放到list中
* @param sheetNumber
* @param dir excel文件所在目录
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public List<String> getDatasInSheet(int sheetNumber,File dir) throws FileNotFoundException, IOException{
File[] files = dir.listFiles();
List<String> result = new ArrayList<String>();
for(File f : files)
{
if(!f.getName().toLowerCase().endsWith(".xls"))
{
continue;
}
workbook = new HSSFWorkbook(new FileInputStream(f));
//获得指定的表
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//获得数据总行数
int rowCount = sheet.getLastRowNum();
logger.info("found excel rows count: " + rowCount);
if (rowCount < 1) {
return result;
}
//逐行读取数据
for (int rowIndex = 4; rowIndex <= rowCount; rowIndex++) {
//获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
List<Object> rowData = new ArrayList<Object>();
//获得本行中单元格的个数
int columnCount = row.getLastCellNum();
//获得本行中各单元格中的数据
HSSFCell cell = row.getCell(1);