将信息插入到Excel文件的指定位置
步骤
- 创建File
- 和获取文件、建立副本
- 获取表
- 定位到指定位置
- 遍历所有位置
public void insert(String name){
File file = new File("data/aa"+name+".xls");
try {
//obtain the original excel
Workbook wb = Workbook.getWorkbook(_file);
WritableWorkbook book = Workbook.createWorkbook(_file,wb);
//obtain the sheet
WritableSheet sheet = book.getSheet(0);
//Split inserted data
String in_bracket = value_list.substring(1, value_list.length()-1);
String []valuelist = in_bracket.split(",");
WritableCell cell = null;
int Rows = sheet.getRows();
for(int i = 0; i < valuelist.length;i++){
cell = new Label(i, Rows, valuelist[i]);
try {
sheet.addCell(cell);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
book.write();
try {
book.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
声明:
本文是作者实践时的心得记录,在特定项目下生效,不具有普适性