jxl读取excel代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ExcelImporter
{
/**
* @Function //函数、方法名称
* @Description //测试excell表格
* @Input //输入参数的说明
* @Output //输出参数的说明
* @Return //函数返回值的说明
* @Others //其它说明
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
List list = new ArrayList();
String filePath = "C:/sinye.xls";
InputStream fs = null;
Workbook workBook = null;
try {
// 加载excel文件
fs = new FileInputStream(filePath);
// 得到 workbook
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 取得sheet,如果你的workbook里有多个sheet 可以利用 wb.getSheets()方法来得到所有的。
// getSheets() 方法返回 Sheet[] 数组 然后利用数组来操作。就是多次循环的事。
Sheet sheet = workBook.getSheet(0);//这里只取得第一个sheet的值,默认从0开始
System.out.println(sheet.getColumns());//查看sheet的列
System.out.println(sheet.getRows());//查看sheet的行
Cell cell = null;//就是单个单元格
// 开始循环,取得 cell 里的内容,这里都是按String来取的 为了省事,具体你自己可以按实际类型来取。或者都按
// String来取。然后根据你需要强制转换一下。
for (int j = 0; j < sheet.getColumns(); j++) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < sheet.getRows(); i++) {
cell = sheet.getCell(j, i);
sb.append(cell.getContents());
sb.append(",");//将单元格的每行内容用逗号隔开
}
list.add(sb.toString());//将每行的字符串用一个String类型的集合保存。
}
workBook.close();//记得关闭
//迭代集合查看每行的数据
for(String ss:list){
System.out.println(ss);
}
}
}
接下来,就是写excel了,见代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
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 JxlWriteExcel {
/**
* @param datas 封装着Object[]的列表, 一般是String内容.
* @param title 每个sheet里的标题.
*/
public void writeExcel(OutputStream out, List list, String[] title) {
if(list == null) {
throw new IllegalArgumentException("要写入excel的数据不能为空!");
}
try {
WritableWorkbook workbook = Workbook.createWorkbook(out);
WritableSheet ws = workbook.createSheet("sheet 1", 0);//创建sheet
int rowNum = 0; //要写的行,jxl操作excel时,第一行是从0开始,以此类推
if(title != null) {
putRow(ws, 0, title);//压入标题
rowNum = 1;
}
for(int i=0; i
Object[] cells = (Object[]) list.get(i);
putRow(ws, rowNum, cells); //压一行到sheet
}
workbook.write();
workbook.close(); //一定要关闭, 否则没有保存Excel
} catch (RowsExceededException e) {
System.out.println("jxl write RowsExceededException: "+e.getMessage());
} catch (WriteException e) {
System.out.println("jxl write WriteException: "+e.getMessage());
} catch (IOException e) {
System.out.println("jxl write file i/o exception!, cause by: "+e.getMessage());
}
}
private void putRow(WritableSheet ws, int rowNum, Object[] cells) throws RowsExceededException, WriteException {
for(int j=0; j
Label cell = new Label(j, rowNum, ""+cells[j]);
ws.addCell(cell);
}
}
}
调用写excel的测试代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
//组装写入excel的数据
for(int i=0;i<10;i++){
String[] data = {"开心"+i,"2"+i};
list.add(data);
}
try {
OutputStream out = new FileOutputStream(new File("c:\\sinye.xls"));
JxlWriteExcel jxlExcelWriter = new JxlWriteExcel();
jxlExcelWriter.writeExcel(out, list, new String[] {"姓名", "年龄"});
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}