工具类的下载地址: http://www.andykhan.com/jexcelapi/download.html
工具类的使用:
public class JxlUtil {
/**
* 将一个二维数组的数据写到excel文件中
* @param filepath
* @param datas
* @throws IOException
* @throws RowsExceededException
* @throws WriteException
*/
public static void writeExcel(String filepath,String[][] datas) throws IOException, RowsExceededException, WriteException{
WritableWorkbook wwbook = Workbook.createWorkbook( new File(filepath)) ;
WritableSheet ws = wwbook.createSheet("tablendata", 0);
Label label;
for(int row=0;row<datas.length;row++){
String[] column_data =datas[row];
for(int column=0;column<column_data.length;column++){
label = new Label(column,row,datas[row][column],getPlainCF());
ws.addCell( label );
}
}
/*
* 设置 某行的高 某列的宽
*/
ws.setColumnView( 0, 30);
ws.setRowView(1, 400);
wwbook.write();
wwbook.close();
System.out.println( "结束!");
}
/**
* 设置单元格的格式
* @return
* @throws WriteException
*/
public static CellFormat getPlainCF() throws WriteException{
/*
* 设置单元格的字体
*/
WritableFont wf = new WritableFont( WritableFont.TIMES ) ;
wf.setColour( Colour.LIGHT_ORANGE);
/*
* 设置单元格的格式
*/
WritableCellFormat wcf = new WritableCellFormat(wf) ;
wcf.setAlignment( Alignment.RIGHT );
wcf.setBackground( Colour.WHITE);
wcf.setBorder( Border.ALL, BorderLineStyle.THIN);
wcf.setOrientation(Orientation.HORIZONTAL);
wcf.setVerticalAlignment(VerticalAlignment.TOP);
return wcf;
}
/**
* 这种方法 每次循环到该行的最后一个 速度快一些
* @param filepath
* @return
* @throws IOException
* @throws BiffException
*/
public static List readExcel(String filepath) throws IOException, BiffException{
List list = new ArrayList();
List list_temp ;
Workbook wwbook = Workbook.getWorkbook( new File(filepath)) ;
Sheet ws = wwbook.getSheet(0 ) ;
int row_count = ws.getRows();//的都总行数
for(int i=0;i<row_count;i++){
Cell[] cells = ws.getRow(i); //得到每行的单元格数组
list_temp = new ArrayList();
for(int j=0;j<cells.length;j++){
System.out.print(cells[j].getContents() );
list_temp.add( cells[j].getContents()); //得到每个cell中的数据
}
System.out.println();
list.add(list_temp );
}
return list;
}
/**
* 每行都运行到该 表格的最大一行 读取慢
* @param filename
* @throws BiffException
* @throws IOException
*/
public static void readExc(File filename) throws BiffException, IOException{
Workbook wb = Workbook.getWorkbook(filename);
Sheet s = wb.getSheet(0);//第1个sheet
Cell c = null;
int row = s.getRows();//总行数
int col = s.getColumns();//总列数
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
c = s.getCell(j,i);
System.out.print(c.getContents()+" ");
}
System.out.println();
}
}
public static void main(String[] args) throws RowsExceededException, WriteException, IOException, BiffException {
// String[][] datas= {{"1","","3"},{"1","2","3"},{"1","2","3"}};
// writeExcel("d:/test.xls", datas);
// File f = new File("d:/test.xls");
List list = readExcel("d:/test1.xls");
// readExc(new File("d:/test1.xls"));
// for(int i=0;i<list.size();i++){
// List list2 = (List) list.get(i);
// for(int j=0;j<list2.size();j++){
// System.out.print( list2.get(j)+" ");
// }
// System.out.println( );
// }
}
}