https://meilishiyan-song.taobao.com/

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 = "\t";
private static String EXCEL_LINE = "\n";

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();
}
}
}

 

}

posted @ 2017-03-20 15:45  望梦圆  阅读(2206)  评论(0编辑  收藏  举报
https://meilishiyan-song.taobao.com/