学会如何用Java操作Excel文件,有一个文本文件,可以将里面内容读

学会如何用Java操作Excel文件,有一个文本文件,可以将里面内容读

import java.io.*;
import java.util.ArrayList;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class NewT {
public static int v=0;
public void painting(String test1,String test2) throws Exception {
Workbook rwb = Workbook.getWorkbook(new File(test1));
Sheet sheet = rwb.getSheet("Sheet1");// 由指定sheet名获得工作簿表单对象
OutputStream os = new FileOutputStream(test2);//输出的Excel文件URL
WritableWorkbook wwb = Workbook.createWorkbook(os);//创建可写工作薄
WritableSheet ws = wwb.createSheet("sheet1", 1);//创建可写工作表
int rowss = sheet.getRows();
int columns = sheet.getColumns();
ArrayList list =new ArrayList();
int j;
int h;

//DAO dao=new DAO();
for (j = 0; j < rowss; j++) {

for (h = 0; h < columns; h++) {

Cell cellg = sheet.getCell(0, j);
String g = cellg.getContents();
Cell cellh = sheet.getCell(h, 0);
String s = cellh.getContents();
if (s.equals("end")&&g.endsWith("end")) {
for (int k = 4; k < h; k++) {
for (int x = 1; x < j; x++) {

//System.out.println("");
Cell cells1 = sheet.getCell(0, x);//物料长代码
Cell cells2 = sheet.getCell(1, x);//项目型号
Cell cells3 = sheet.getCell(2, x);//描述
Cell cells4 = sheet.getCell(3, x); //总数
Cell cells5 = sheet.getCell(k, 0);//地址
Cell cells6 = sheet.getCell(k,x);//局点数

String cell1 = cells1.getContents();
String cell2 = cells2.getContents();
String cell3 = cells3.getContents();
String cell4 = cells4.getContents();
String cell5 = cells5.getContents();
String cell6 = cells6.getContents();
if(cell6.equals("0")){
System.out.print("");
}
else{

v++;
//System.out.print("行"+x+" 列"+k+" " );
//System.out.print(cell1+" ");
//System.out.print(cell2+" ");
// System.out.print(cell3+" ");
//System.out.print(cell4+" ");
// System.out.print(cell5+" ");
// System.out.print(cell6+" ");
Label a=new Label(0,v,cell1);
// System.out.print(cell1);
Label b=new Label(1,v,cell5);
// System.out.print(cell5);
Label c=new Label(2,v,cell6);
Label d=new Label(3,v,cell2);
Label e=new Label(4,v,cell3);
Label f=new Label(5,v,cell4);
// System.out.print(v+" ");
// System.out.println(cell6);
ws.addCell(a);
ws.addCell(b);
ws.addCell(c);
ws.addCell(d);
ws.addCell(e);
ws.addCell(f);

}

}
}
//list.add(si);
wwb.write();
wwb.close();
os.close();
//System.out.print("h:" + h);
//System.out.println("j:" + j);
}
}
}
}

public static void main(String args[]) throws Exception {

NewT t = new NewT();
//t.painting();

}
}
加分哦!不过这个要用到jxl去网站下载就好
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-24
第2个回答  2010-05-24
学习下JExcel吧,对你有帮助的。
相似回答