public String exportExcel() {
List<Weibous> list = new ArrayList<Weibous>();
list = weibouserManager.getAll();
System.out.println("查询到有多少对象list.se=" + list.size());
try {
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition",
"attachment;filename=fine.xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
String tmptitle = "客户数据报表"; // 标题
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
// 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setBackground(Colour.AQUA);
wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
// 开始生成主体内容
wsheet.addCell(new Label(0, 2, "编号"));
wsheet.addCell(new Label(1, 2, "用户名"));
wsheet.addCell(new Label(2, 2, "密码"));
for (int i = 0; i < list.size(); i++) {
wsheet.addCell(new Label(0, i + 3, list.get(i).getWbid()));
wsheet.addCell(new Label(1, i + 3, list.get(i).getUsername()));
wsheet.addCell(new Label(2, i + 3, list.get(i).getPassword()));
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.close(); // 关闭流
return"list";
} catch (Exception ex) {
ex.printStackTrace();
return"list";
}
}
public String importExcel() throws BiffException, IOException {
//取得工作薄
Workbook workbook = Workbook.getWorkbook(new File("C:/Users/Liq/Downloads/liuss.xls"));
//取得excel的第一页
Sheet sheet = workbook.getSheet(0);
int rowNum = sheet.getRows(); // --获取一共有多少行
int colNum = sheet.getColumns(); //--获取列的方法名具体忘了,你Eclipse里敲下就知道了
for(int i=0; i <rowNum;i++){
Weibous weibous = new Weibous();
Cell cell1 = sheet.getCell(1,i); // ---获取每一个单元格
String content1 = cell1.getContents(); //--获取单元格的内容
weibous.setUsername(content1);
Cell cell2 = sheet.getCell(2,i); // ---获取每一个单元格
String content2 = cell2.getContents(); //--获取单元格的内容
weibous.setPassword(content2);
weibouserManager.save(weibous);
}
return"list";
}