//写入一个Excel文件
import java.io.*;
import jxl.*;
import java.lang.Boolean;
public class WriteToExcel{
public Boolean run(String targetFile){
try{
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(targetFile));
jxl.write.WritableSheet ws = wwb.createSheet("MySheet1", 0);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
jxl.write.Label labelC = new jxl.write.Label(j, i, "Cell["+i+", "+j+"]");
ws.addCell(labelC);
}
}
wwb.write();
wwb.close();//记得关闭以释放空间
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
}
//读取一个Excel文件(上面代码创建的),并在控制台上输出内容
import java.io.*;
import jxl.*;
import jxl.Workbook;
import java.lang.Boolean;
public class ReadFromExcel {
public Boolean run(String targetFile){
try{
InputStream is = new FileInputStream(targetFile);
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
Cell cell = rs.getCell(j, i);//第一位是列号,第二位是行号
System.out.print(cell.getContents() + "\r");
}
System.out.println();
}
rwb.close();
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
}