SSH项目--国税(一)

 1.头像上传
在用户管理的添加、编辑页面中,可以上传用户的头像;页面中对头像字段引用type=file的文本域。
<input type="file" name="headImg"/> 
 之后UserAction接收文件需要配置的3个基本属性变量:
private File headImg;
private String headImgContentType;
private String headImgFileName;
//保存新增
public String add(){
try {
if(user != null){
//处理头像
if(headImg != null){
//1、保存头像到upload/user
//获取保存路径的绝对地址
String filePath =ServletActionContext.getServletContext().getRealPath("upload/user");
String fileName = UUID.randomUUID().toString().replaceAll("-","") +headImgFileName.substring(headImgFileName.lastIndexOf("."));
//复制文件
FileUtils.copyFile(headImg, new File(filePath,fileName));
//2、设置用户头像路径
user.setHeadImg("user/" + fileName);
}
userService.save(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}


将头像文件保存在服务器的upload/user目录下,文件的名称以uuid重命名。(必须重命名:①重名问题②特殊字符在显示时的问题)


2.批量导入、导出
03,07导入导出测试:
public class TestPOI2Excel {
@Test
public void testWrite03Excel() throwsException {
//1、创建工作簿
HSSFWorkbook workbook = newHSSFWorkbook();
//2、创建工作表
HSSFSheet sheet = workbook.createSheet("helloworld");//指定工作表名
//3、创建行;创建第3行
HSSFRow row = sheet.createRow(2);
//4、创建单元格;创建第3行第3列
HSSFCell cell = row.createCell(2);
cell.setCellValue("Hello World");
 
//输出到硬盘
FileOutputStream outputStream = newFileOutputStream("F:\\A\\测试.xls");
//把excel输出到具体的地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
 
@Test
public void testWrite07Excel() throwsException {
//1、创建工作簿
XSSFWorkbook workbook = newXSSFWorkbook();
//2、创建工作表
XSSFSheet sheet = workbook.createSheet("helloworld");//指定工作表名
//3、创建行;创建第3行
XSSFRow row = sheet.createRow(2);
//4、创建单元格;创建第3行第3列
XSSFCell cell = row.createCell(2);
cell.setCellValue("Hello World");
 
//输出到硬盘
FileOutputStream outputStream = newFileOutputStream("F:\\A\\测试.xlsx");
//把excel输出到具体的地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
@Test
public void testRead03Excel() throws Exception{
FileInputStream inputStream = newFileInputStream("F:\\A\\测试.xls");
//1、读取工作簿
HSSFWorkbook workbook = newHSSFWorkbook(inputStream);
//2、读取第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
//3、读取行;读取第3行
HSSFRow row = sheet.getRow(2);
//4、读取单元格;读取第3行第3列
HSSFCell cell = row.getCell(2);
System.out.println("第3行第3列单元格的内容为:" +cell.getStringCellValue());
 
workbook.close();
inputStream.close();
}
 
@Test
public void testRead07Excel() throws Exception{
FileInputStream inputStream = newFileInputStream("F:\\A\\测试.xlsx");
//1、读取工作簿
XSSFWorkbook workbook = newXSSFWorkbook(inputStream);
//2、读取第一个工作表
XSSFSheet sheet = workbook.getSheetAt(0);
//3、读取行;读取第3行
XSSFRow row = sheet.getRow(2);
//4、读取单元格;读取第3行第3列
XSSFCell cell = row.getCell(2);
System.out.println("第3行第3列单元格的内容为:" +cell.getStringCellValue());
 
workbook.close();
inputStream.close();
}
 
@Test
public void testRead03And07Excel() throwsException {
String fileName = "F:\\A\\测试.xlsx";
if(fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){//判断是否excel文档
 
boolean is03Excel =fileName.matches("^.+\\.(?i)(xls)$");
 
FileInputStream inputStream = newFileInputStream(fileName);
 
//1、读取工作簿
Workbook workbook = is03Excel ?newHSSFWorkbook(inputStream):new XSSFWorkbook(inputStream);
//2、读取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
//3、读取行;读取第3行
Row row = sheet.getRow(2);
//4、读取单元格;读取第3行第3列
Cell cell = row.getCell(2);
System.out.println("第3行第3列单元格的内容为:" +cell.getStringCellValue());
 
workbook.close();
inputStream.close();
}
}
@Test
public void testExcelStyle() throws Exception{
//1、创建工作簿
HSSFWorkbook workbook = newHSSFWorkbook();
//1.1、创建合并单元格对象;合并第3行的第3列到第5列
CellRangeAddress cellRangeAddress = newCellRangeAddress(2, 2, 2, 4);//起始行号,结束行号,起始列号,结束列号
//1.2、创建单元格样式
HSSFCellStyle style =workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//1.3、创建字体
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
font.setFontHeightInPoints((short)16);//设置字体大小
//加载字体
style.setFont(font);
 
//单元格背景
//设置背景填充模式
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置填充背景色
style.setFillBackgroundColor(HSSFColor.YELLOW.index);
//设置填充前景色
style.setFillForegroundColor(HSSFColor.RED.index);
//2、创建工作表
HSSFSheet sheet = workbook.createSheet("HelloWorld");//指定工作表名
//2.1、加载合并单元格对象
sheet.addMergedRegion(cellRangeAddress);
//3、创建行;创建第3行
HSSFRow row = sheet.createRow(2);
//4、创建单元格;创建第3行第3列
HSSFCell cell = row.createCell(2);
//加载样式
cell.setCellStyle(style);
cell.setCellValue("Hello World!");
//输出到硬盘
FileOutputStream outputStream = newFileOutputStream("F:\\A\\测试.xls");
//把excel输出到具体的地址
workbook.write(outputStream);
workbook.close();
outputStream.close();
}}


查阅<POI操作excel.doc>先了解POI,使用POI工具类导出用户数据到excel中。
 
导出:在用户管理页面中点击“导出”,将所有用户记录导出到excel中。
//导出用户列表
public void exportExcel(){
try {
//1、查找用户列表
userList = userService.findObjects();
//2、导出
HttpServletResponse response =ServletActionContext.getResponse();
response.setContentType("application/x-execl");
response.setHeader("Content-Disposition","attachment;filename=" + new String("用户列表.xls".getBytes(),"ISO-8859-1"));
ServletOutputStream outputStream =response.getOutputStream();
userService.exportExcel(userList,outputStream);
if(outputStream != null){
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}


在userService中将调用导出工具类ExcelUtil导出数据。
public class ExcelUtil {
 
public static void exportUserExcel(ListuserList, ServletOutputStream outputStream) {
try {
//1、创建工作簿
HSSFWorkbook workbook = newHSSFWorkbook();
//1.1、创建合并单元格对象
CellRangeAddress cellRangeAddress = newCellRangeAddress(0, 0, 0, 4);//起始行号,结束行号,起始列号,结束列号
 
//1.2、头标题样式
HSSFCellStyle style1 =createCellStyle(workbook, (short)16);
 
//1.3、列标题样式
HSSFCellStyle style2 =createCellStyle(workbook, (short)13);
 
//2、创建工作表
HSSFSheet sheet =workbook.createSheet("用户列表");
//2.1、加载合并单元格对象
sheet.addMergedRegion(cellRangeAddress);
//设置默认列宽
sheet.setDefaultColumnWidth(25);
 
//3、创建行
//3.1、创建头标题行;并且设置头标题
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell1 = row1.createCell(0);
//加载单元格样式
cell1.setCellStyle(style1);
cell1.setCellValue("用户列表");
 
//3.2、创建列标题行;并且设置列标题
HSSFRow row2 = sheet.createRow(1);
String[] titles = {"用户名","帐号", "所属部门", "性别","电子邮箱"};
for(int i = 0; i < titles.length; i++){
HSSFCell cell2 = row2.createCell(i);
//加载单元格样式
cell2.setCellStyle(style2);
cell2.setCellValue(titles[i]);
}
 
//4、操作单元格;将用户列表写入excel
if(userList != null){
for(int j = 0; j < userList.size();j++){
HSSFRow row = sheet.createRow(j+2);
HSSFCell cell11 = row.createCell(0);
cell11.setCellValue(userList.get(j).getName());
HSSFCell cell12 = row.createCell(1);
cell12.setCellValue(userList.get(j).getAccount());
HSSFCell cell13 = row.createCell(2);
cell13.setCellValue(userList.get(j).getDept());
HSSFCell cell14 = row.createCell(3);
cell14.setCellValue(userList.get(j).isGender()?"男":"女");
HSSFCell cell15 = row.createCell(4);
cell15.setCellValue(userList.get(j).getEmail());
}
}
//5、输出
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
 
private static HSSFCellStylecreateCellStyle(HSSFWorkbook workbook, short fontSize) {
HSSFCellStyle style =workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//创建字体
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
font.setFontHeightInPoints(fontSize);
//加载字体
style.setFont(font);
return style;
}
}


添加选择文件按钮:
<input name="userExcel" type="file"/>
<input type="button" value="导入"class="s_button" οnclick="doImportExcel()"/>
      //导入
     function doImportExcel(){
     document.forms[0].action ="${basePath}nsfw/user_importExcel.action";
     document.forms[0].submit();
     }
导入:在用户管理页面中,上传包含用户列表的excel文档,然后点击“导入”将excel中的用户记录插入的用户表中,并显示在列表上。
① UserAction 中接受导入文件
//导入用户列表
public String importExcel(){
//1、获取excel文件
if(userExcel != null){
//是否是excel
if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
//2、导入
userService.importExcel(userExcel, userExcelFileName);
}
}
return "list";
}
② UserServiceImpl 中具体处理导入文件
public void importExcel(File userExcel, StringuserExcelFileName) {
try {
FileInputStream fileInputStream = newFileInputStream(userExcel);
boolean is03Excel =userExcelFileName.matches("^.+\\.(?i)(xls)$");
//1、读取工作簿
Workbook workbook = is03Excel ? newHSSFWorkbook(fileInputStream):newXSSFWorkbook(fileInputStream);
//2、读取工作表
Sheet sheet = workbook.getSheetAt(0);
//3、读取行
if(sheet.getPhysicalNumberOfRows() > 2){
User user = null;
for(int k = 2; k < sheet.getPhysicalNumberOfRows();k++){
//4、读取单元格
Row row = sheet.getRow(k);
user = new User();
//用户名
Cell cell0 = row.getCell(0);
user.setName(cell0.getStringCellValue());
//帐号
Cell cell1 = row.getCell(1);
user.setAccount(cell1.getStringCellValue());
//所属部门
Cell cell2 = row.getCell(2);
user.setDept(cell2.getStringCellValue());
//性别
Cell cell3 = row.getCell(3);
user.setGender(cell3.getStringCellValue().equals("男"));
//手机号
String mobile = "";
Cell cell4 = row.getCell(4);
try {
mobile = cell4.getStringCellValue();
} catch (Exception e) {
double dMobile = cell4.getNumericCellValue();
mobile = BigDecimal.valueOf(dMobile).toString();
}
user.setMobile(mobile);
//电子邮箱
Cell cell5 = row.getCell(5);
user.setEmail(cell5.getStringCellValue());
//生日
Cell cell6 = row.getCell(6);
if(cell6.getDateCellValue() != null){
user.setBirthday(cell6.getDateCellValue());
}
//默认用户密码为 123456
user.setPassword("123456");
//默认用户状态为 有效
user.setState(User.USER_STATE_VALID);
//5、保存用户
save(user);
}
}
workbook.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值