2. 2.jxl对Excel表格的认识:每个单元格的位置认为是由一个二维坐标(i,j)给定,其中i表示列,j表示行,并且从上到下递增,从左到右递增。
3. 本代码读取text.xls的头三列,按行输出到result.txt
稍微改变格式
- import java.io.File;
- import java.io.FileOutputStream;
- import jxl.*;
- public class Read_excel{
- public static void main(String[] args) {
- int i;
- Sheet sheet;
- Workbook book;
- Cell cell1,cell2,cell3;
- String content;
- try {
- //t.xls为要读取的excel文件名
- book= Workbook.getWorkbook(new File("e:\\test.xls"));
- //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
- sheet=book.getSheet(0);
- //获取左上角的单元格
- cell1=sheet.getCell(0,0);
- System.out.println("标题:"+cell1.getContents());
- File outFile=new File("e:\\result.txt");
- FileOutputStream fos=new FileOutputStream(outFile);
- i=1;
- while(true)
- {
- //获取每一行的单元格
- cell1=sheet.getCell(0,i);//(列,行)
- cell2=sheet.getCell(1,i);
- cell3=sheet.getCell(2,i);
- if("".equals(cell1.getContents())==true) //如果读取的数据为空
- break; System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents());
- content=cell1.getContents()+"\t"+cell2.getContents()+"0000&&&&&"+cell3.getContents()+"\r\n";
- byte[] contentInBytes = content.getBytes();
- fos.write(contentInBytes);
- i++;
- }
- book.close();
- fos.close();
- }
- catch(Exception e) { }
- }
- }
No comments:
Post a Comment