• excel转为txt文件 简单代码


    package cn.com.mcd.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    /**
    * Excel格式转txt
    * @author soya.song
    *
    */
    public class UtilExcelToTxt {
    private static UtilExcelToTxt instance = null;
    private static String EXCEL_LINE_DELIMITER = " ";
    private static String EXCEL_LINE = " ";

    private UtilExcelToTxt(){}
    /**
    * 生成实例对象
    * @return
    */
    public static UtilExcelToTxt getInstance(){
    if (instance == null) {
    synchronized (UnZipApp.class) {
    if (instance == null)
    instance = new UtilExcelToTxt();
    }
    }
    return instance;
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    //window
    String from = "d:\file\customer_master.xlsx";
    String to = "d:\file\";
    // linux
    // String from = "/home/ap/ods/tmp/excel/from/";
    // String to = "/home/ap/ods/tmp/excel/to/";
    UtilExcelToTxt xt = UtilExcelToTxt.getInstance();
    xt.excelToTxt(from, to,"0.txt");
    }
    /**
    * excel转换成txt
    *
    * @param from 源目录
    * @param to 目标目录
    */
    public synchronized void excelToTxt(String from, String to, String newFileName) {
    File newFile = null;
    InputStream is = null;
    String fileName="customer_master.xlsx";
    FileOutputStream out = null;
    XSSFWorkbook book = null;
    XSSFSheet sheet= null;
    try {
    newFile = new File(to + newFileName);
    out = new FileOutputStream(newFile);
    //headStr(out);
    is = new FileInputStream(from);
    book =new XSSFWorkbook(new FileInputStream(new File(from)));
    sheet = book.getSheetAt(0);
    readSheet(sheet, out, fileName);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (book != null)
    book = null;
    if (is != null)
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    // 释放out资源
    try {
    if (out != null)
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    // private void headStr(FileOutputStream out){
    // StringBuffer shead = new StringBuffer();
    // shead.append("DoorCode+"+EXCEL_LINE_DELIMITER+"SHIP_TO"+EXCEL_LINE_DELIMITER+"POSID"+EXCEL_LINE_DELIMITER+Status);
    // try {
    // out.write(shead.toString().getBytes());
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // }
    /**
    * 读Sheet写到txt文件
    *
    * @param sheet
    * @param out
    */
    private void readSheet(XSSFSheet xssfSheet, FileOutputStream out,String fileName) {
    int rowTotalNum = xssfSheet.getLastRowNum();//总行数
    XSSFRow rowFirst=xssfSheet.getRow(0);//第一行
    int cols = rowFirst.getLastCellNum();//总列数
    StringBuffer sBuffer =new StringBuffer();
    if (rowTotalNum > 0) {
    //循环sheet中的所有行
    for (int rowNum = 0; rowNum < rowTotalNum; rowNum++) {
    XSSFRow xssfRow = xssfSheet.getRow(rowNum);
    if (xssfRow != null) {
    //循环一行中的所有列
    for(int j=0;j<cols;j++){
    XSSFCell cell1 = xssfRow.getCell(j);
    sBuffer.append(cell1);//CUST_ACCT_NO
    sBuffer.append(EXCEL_LINE_DELIMITER);
    }
    //一行结束后换行
    sBuffer.append(EXCEL_LINE);//UPLOAD_FILE
    }
    }
    System.out.println(sBuffer.toString());
    //读1行写到txt文件
    try {
    out.write(sBuffer.toString().getBytes());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    }

  • 相关阅读:
    HDU 4832(DP+计数问题)
    mongodb安装与使用
    (hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)
    dba_dependencies查询结果视图
    情绪一点点
    c#基于这些,你已经看到了?(一)-----谁才刚刚开始学习使用
    九. 200创业教训万元获得--“神刻”这是忽悠?
    初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)
    采用CSS3 Media Query技术适应Android平板屏幕分辨率和屏幕像素密度
    线程的上下文
  • 原文地址:https://www.cnblogs.com/songyunxinQQ529616136/p/6589551.html
Copyright © 2020-2023  润新知