java读取xls内容并写入txt
package document;
import java.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileWriter;
importjava.io.InputStream;
importjava.text.SimpleDateFormat;
import java.util.Date;import jxl.Cell;
importjxl.Sheet;
import jxl.Workbook;/**
*将excel中的内容复制到txt中
*@authorDanielCooger
*<ahref="mailto:[email protected]">daniel</a>
*/
public class Xls {private static String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
privatestaticStringxls="d:\\doc\\XLS"+date+".txt";
privatestaticStringpath="d:\\document\\1.xls";
publicstaticvoidmain(String[]args){
newXls().readExcel(path,xls,true);
}
//读取Excel内的数据并写入目标文件中
publicbooleanreadExcel(Stringpath,Stringxls,booleanappend){
try{
InputStreamis=newFileInputStream(path);
Workbookbook=Workbook.getWorkbook(is);
Sheetsheet=book.getSheet(0);
//得到所有的行数
Integerrows=sheet.getRows();
//得到所有的列数
Integercolus=sheet.getColumns();
Cellcell;
for(inti=0;i<rows;i++){
for(inta=0;a<colus;a++){
cell=sheet.getCell(a,i);
new Xls().insert(xls, cell.getContents(), append);/**
*换行显示
*newXls().insert(xls,cell.getContents()+"\r\n",append);
*/
}
}
book.close();
returntrue;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}public void insert(String path, String content, boolean append) {
BufferedWriterbw;
Filefile;
try{
booleanaddstr=append;
file=newFile(path);
//创建文件输出流写入文件
FileWriterfw=newFileWriter(file,addstr);
bw=newBufferedWriter(fw);
//将文本内容写入文件
fw.write(content);
fw.flush();
fw.close();
}catch(Exceptione){
e.getMessage();
}
}
}