package com.bx.test.services.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.bx.test.bean.Users;
import com.bx.test.dao.UserDao;
import com.bx.test.dao.impl.UserDaoImpl;
import com.bx.test.services.UserServices;
public class UserServicesImpl implements UserServices {
private UserDao userDao= new UserDaoImpl();
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<Users> QueryAll() {
return this.userDao.QueryAll();
}
public boolean delInfo(int id) {
return this.userDao.delInfo(id);
}
public Users findById(int id) {
return this.userDao.findById(id);
}
public boolean saveInfo(Users users) {
return this.userDao.saveInfo(users);
}
public boolean updateInfo(Users users) {
return this.userDao.updateInfo(users);
}
/**
* 为生成的excel文件导出,获得输入流
*
* @return InputStream
*/
public InputStream getInputStream() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("编号");
cell = row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("用户名");
cell = row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("密码");
List<Users> list = this.QueryAll();
for(int i=0; i < list.size(); i++ ) {
Users user = list.get(i);
row = sheet.createRow(i+1);
cell = row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i + 1);
cell = row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getUsername());
cell = row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getPassword());
}
File file = new File("test.xsl");
try {
OutputStream os = new FileOutputStream(file);
wb.write(os);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return is;
}
/**
* 新式for循环写法测试
*/
void getUsersName() {
Users user1 = new Users();
user1.setId(71);
user1.setUsername("simens");
Users user2 = new Users();
user2.setId(81);
user2.setUsername("nokia");
Users user3 = new Users();
user3.setId(91);
user3.setUsername("moto");
List<Users> list = new ArrayList<Users>();
list.add(user1);
list.add(user2);
list.add(user3);
int i = 1;
for(Users user : list) {
System.out.println("第"+i+"个 : "+user.getUsername());
i++;
}
}
public static void main(String[] args) {
new UserServicesImpl().getUsersName();
}
}