java解析excel文件为txt文件
2016-11-09 19:04
501 查看
java解析excel文件,并将其以建松格式保存到txt文件中。
1、maven配置
2、java代码实现
1、maven配置
<!-- poi--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <!-- jsckson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.2</version> </dependency>
2、java代码实现
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; 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.poifs.filesystem.POIFSFileSystem; import java.io.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; /** * Created by xxx on 2016/11/8. */ public class ExcelToTxt { private static Map<Integer, String> keyMapName = new LinkedHashMap<Integer, String>(); private static Map<String, String> keyMapContext = new LinkedHashMap<String, String>(); static ConcurrentLinkedQueue<String> queues = new ConcurrentLinkedQueue<String>(); static ObjectMapper objectMapper = new ObjectMapper() .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); public static void main(String[] args){ try { HSSFWorkbook wb = null; POIFSFileSystem fs = null; String filePath = "F:\\datafortag\\quan-2016-11-08.xls"; fs = new POIFSFileSystem(new FileInputStream(new File(filePath))); wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Integer rowNum; HSSFRow rowName = sheet.getRow(0); for (int cellNum = rowName.getFirstCellNum(); cellNum < rowName.getLastCellNum(); cellNum++){ keyMapName.put(cellNum, rowName.getCell(cellNum).getStringCellValue()); } for(rowNum = 2; rowNum <= sheet.getLastRowNum(); rowNum++){ HSSFRow row = sheet.getRow(rowNum); if(row != null){ for (int cellNum = row.getFirstCellNum(); cellNum< row.getLastCellNum(); cellNum++){ if(row.getCell(cellNum).getStringCellValue() != null && !"".equals(row.getCell(cellNum).getStringCellValue())) { keyMapContext.put(keyMapName.get(cellNum), row.getCell(cellNum).getStringCellValue()); } } } if(keyMapContext != null && keyMapContext.size() > 0) { queues.add(objectMapper.writeValueAsString(keyMapContext)); } } File file = new File("F:\\datafortag\\quan-2016-11-08.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStream = new OutputStreamWriter(fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStream); int i = 0; while (true){ if(!queues.isEmpty()) { String context = queues.poll(); if(context == null) continue; bufferedWriter.write(context); bufferedWriter.newLine(); i++; System.out.println("total :" + i); } if(queues.isEmpty()){ try{ bufferedWriter.flush(); bufferedWriter.close(); outputStream.flush(); outputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); } catch (Exception e){ e.printStackTrace(System.out); } break; } } } catch (Exception e){ } } }
相关文章推荐
- JAVA JXL -----excel文件上传和解析
- java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
- java 解析txt,conf 文件
- 批量处理txt文本文件到Excel文件中去----java
- Java读取导入Excel文件解析
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- java通过poi解析Excel文件,直接读取客户端文件
- Java数据导出(写)Excel文件 解析
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
- Java数据导入(读)Excel文件 解析
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- Java解析Excel文件
- java导入Excel包含多张sheet表的.xls .xslx .txt三种后缀格式的文件
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
- java解析txt文件
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java解析txt文件返回文件内容
- Java+Flex+POI 解析Excel文件(读取文件中想要的内容)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例