Jxl创建Excel文件和解析Excel文件

import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook; public class JxlExpExcel {
/**
* Jxl创建Excel文件
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用数组定义一个表头
String[] title={"id","name","sex"};
//创建Excel文件
File file=new File("d:/jxl_test.xls");
try {
file.createNewFile();
//创建工作薄
WritableWorkbook workbook=Workbook.createWorkbook(file);
//创建sheet
WritableSheet sheet=workbook.createSheet("sheet1", 0);
Label lable=null;
for (int i = 0; i < title.length; i++) {
lable=new Label(i,0,title[i]);
sheet.addCell(lable);
}
//追加数据
for (int i = 1; i < 100; i++) {
lable=new Label(0,i,"a"+i);
sheet.addCell(lable);
lable=new Label(1,i,"user"+i);
sheet.addCell(lable);
lable=new Label(2,i,"男");
sheet.addCell(lable);
}
//写入数据
workbook.write();
//关闭流
workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  

import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook; public class JxlReadExcel {
/**
* 解析Excel文件
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//创建workbook
Workbook workbook=Workbook.getWorkbook(new File("d:/jxl_test.xls"));
//获取第一个工作表sheet
Sheet sheet=workbook.getSheet(0);
//获取数据
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
Cell cell=sheet.getCell(j,i);
System.out.print(cell.getContents()+" ");
}
System.out.println();
}
//关闭流
workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  

上一篇:Find Successor & Predecessor in BST


下一篇:JXL包大解析;Java程序生成excel文件和解析excel文件内容