java解析excel文件为txt文件

java解析excel文件,并将其以建松格式保存到txt文件中。
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){

        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值