读写Excel的常用三种技术
- POI(Apache)
- JXL
- FASTEXCEL
用JXL生成excel
- 导包:由于我是用maven的,所以我就到pom加入:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
- java代码:
public static void main(String[] args){
String[] title = {"id","name","sex"};
File file = new File("d:/jxl_text.xls");
try {
file.createNewFile();
WritableWorkbook workBook = Workbook.createWorkbook(file);
WritableSheet sheet = workBook.createSheet("Sheet1",0);
Label label = null;
for (int i = 0;i<title.length;i++){
label = new Label(i,0,title[i]);
sheet.addCell(label);
}
for (int i = 1;i<10;i++){
label = new Label(0,i,"a"+1);
sheet.addCell(label);
label = new Label(1,i,"user"+1);
sheet.addCell(label);
label = new Label(2,i,"男");
sheet.addCell(label);
}
workBook.write();
workBook.close();
} catch (IOException e) {
e.printStackTrace();
}catch (WriteException e){
e.printStackTrace();
}
}