首页 > 代码库 > hadoop读取Excel文件转换成txt文件
hadoop读取Excel文件转换成txt文件
功能说明:从hdfs读取excel文件,经过poi转换成txt文本文件并输出成hdfs文件
一、引入jar包
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
二、代码实现
package operator.excel;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @ClassName: ExcelInputFormat
* @Description:
* @Author: mashiwei
* @Date: 2017/6/30
*/
public class ExcelInputFormat {
public static void main(String[] args) throws IOException {
Configuration config = new Configuration();
InputStream inputStream;
String[] strArrayofLines;
String output = "/kettle/excel/test.txt";
String input = "/kettle/excel/test.xls";
FileSystem fileSystem = FileSystem.get(config);
FSDataOutputStream out = fileSystem.create(new Path(output));
inputStream = fileSystem.open(new Path(input));
// strArrayofLines = ExcelParser.parseExcelData(inputStream);
strArrayofLines = ExcelParser.parseExcelData(inputStream,new File(input));
for (String str:strArrayofLines) {
System.out.println("------"+str);
out.writeBytes(str);
out.writeUTF("\n");
}
inputStream.close();
out.close();
}
}
package operator.excel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @ClassName: ExcelParser * @Description: * @Author: mashiwei * @Date: 2017/6/30 */ public class ExcelParser { private static final Log logger = LogFactory.getLog(ExcelParser.class); public static void checkFile(File file) throws IOException{ //判断文件是否存在 if(null == file){ logger.error("文件不存在!"); throw new FileNotFoundException("文件不存在!"); } //获得文件名 String fileName = file.getAbsolutePath(); //判断文件是否是excel文件 if(!fileName.endsWith("xls") && !fileName.endsWith("xlsx")){ logger.error(fileName + "不是excel文件"); throw new IOException(fileName + "不是excel文件"); } } /** * 解析is * * @param is 数据源 * @return String[] */ public static String[] parseExcelData(InputStream is,File file) { //获得文件名 String fileName = file.getAbsolutePath(); try { checkFile(file); } catch (IOException e) { e.printStackTrace(); } // 结果集 List<String> resultList = new ArrayList<String>(); Workbook workbook = null; try { // 获取Workbook if(fileName.endsWith("xls")){ //2003 workbook = new HSSFWorkbook(is); }else if(fileName.endsWith("xlsx")) { //2007 workbook = new XSSFWorkbook(is); } // 获取sheet Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { // 行 Row row = rowIterator.next(); // 字符串 StringBuilder rowString = new StringBuilder(); Iterator<Cell> colIterator = row.cellIterator(); while (colIterator.hasNext()) { Cell cell = colIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: rowString.append(cell.getBooleanCellValue() + ","); break; case Cell.CELL_TYPE_NUMERIC: rowString.append(cell.getNumericCellValue() + ","); break; case Cell.CELL_TYPE_STRING: rowString.append(cell.getStringCellValue() + ","); break; } } String str = rowString.delete(rowString.lastIndexOf(","),rowString.lastIndexOf(",")+1).toString(); resultList.add(str); // resultList.add(rowString.toString()); } } catch (IOException e) { logger.error("IO Exception : File not found " + e); } return resultList.toArray(new String[0]); } }
hadoop读取Excel文件转换成txt文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。