package test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelOperate {
public static void main(String[] args) throws Exception {
File path=new File("f://test/");
if(!path.exists()&&!path.isDirectory()){
path.mkdirs();
}
File outFile=new File("f://test/test.xls");
if(!path.exists()){
outFile.createNewFile();
}
/*
* 写入Excel
*/
WritableWorkbook book=Workbook.createWorkbook(outFile);
WritableSheet sheet=book.createSheet("测试第一单",0);
String[] title={"姓名","年龄","地址"};
String[] user1={"张三","21","湖南"};
String[] user2={"李四","45","广西"};
String[] user3={"王五","34","浙江"};
List<String[]> list=new ArrayList<String[]>();
list.add(title);
list.add(user1);
list.add(user2);
list.add(user3);
int rows=list.size();
int columns=title.length;
for(int i=0;i<rows;i++){
String[] userInfo=list.get(i);
for(int j=0;j<columns;j++){
Label label=new Label(j,i,userInfo[j]);
sheet.addCell(label);
}
}
book.write();
book.close();
/*
* 读取Excel
*/
Workbook wBook=Workbook.getWorkbook(outFile);
Sheet[] sheets=wBook.getSheets();
Sheet wSheet=sheets[0];
int wColumns=wSheet.getColumns();
int wRows=wSheet.getRows();
for(int m=0;m<wRows;m++){
for(int n=0;n<wColumns;n++){
Cell cell=wSheet.getCell(n,m);
System.out.print(cell.getContents()+"\t");
}
System.out.println("");
}
wBook.close();
}
}
简单jxl的Excel读写操作
最新推荐文章于 2024-07-27 03:47:04 发布