从Excel中读取内容写入txt文件中

package operateExcel;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;


public class ReadDescription {


public static void main(String[] args) {
// TODO Auto-generated method stub
//execl file
File excelFile=new File("C:\\documents\\在研项目\\多层分类\\中文新闻信息分类与代码\\classificationDescription.xls");
File newTxtFile=null; //new .txt file
String newTxtFilePath="C:\\file\\program file\\a"; //the path of new .txt file


try {
           Workbook book = Workbook.getWorkbook(excelFile);
           // 获得第一个工作表对象(Sheet)
           Sheet sheet = book.getSheet(0);
           
           Integer lineNumber=1; //the line number
           Integer lineFlag=7119; //the total line number
           while(lineNumber<lineFlag){
           // 得到第0列第lineNumber行的单元格
           Cell code = sheet.getCell(0, lineNumber);
           String codeStr = code.getContents().trim();
           
           // 得到第1列第lineNumber行的单元格
           Cell className = sheet.getCell(1, lineNumber);
           String classNameStr = className.getContents().trim();


           // 得到第1列第lineNumber行的单元格
           Cell description = sheet.getCell(2, lineNumber);
           String descriptionStr = description.getContents().trim();
           
           if(codeStr!=""&&!codeStr.equals("")){ //if code is not the null String
            String txtFileName=codeStr+".txt"; //the name of txtFile
           
            /*create file and write content*/
            newTxtFile=new File(newTxtFilePath,txtFileName);
            newTxtFile.createNewFile();
               FileWriter fw=new FileWriter(newTxtFile);
               BufferedWriter bw=new BufferedWriter(fw);
               
               bw.write(classNameStr);
               bw.newLine();
               bw.write(descriptionStr);
               
               bw.flush();
               bw.close();
           }
           
           
           lineNumber++; //line number add 1
           }
           
           book.close();
       } catch (Exception e) {
           System.out.println(e);
       }
}


}

参考文章:http://www.cnblogs.com/sunzhenxing19860608/archive/2010/12/27/1918128.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值