1.文件上传发布目录+数据库保存路径
(1):下面三个属性struts自动装填
private File * (文件类)
private String *ContentType; (文件类型)
private String *FileName; (文件名,包括扩展名)
private File icon;
private String iconContentType;
private String iconFileName;
(2):获取指定工程目录的绝对路径方法(o.o爽)
String filePath =
ServletActionContext.getServletContext().getRealPath("upload/user");
(3):设置保存文件名
UUID保证文件名不会重复,每个User对应一个文件名
substring方法获得包括指定下标往后的字符串(这里是获取扩展名)
结果:UUID+.扩展名
String fileName =
UUID.randomUUID().toString()
+iconFileName.substring(iconFileName.lastIndexOf("."));
(4):FileUtil初识与应用(复制指定文件到指定目录)
FileUtils.copyFile(文件类, new File(目标目录,目标文件名));
FileUtils.copyFile(icon, new File(filePath, fileName));
2.Excel文件导出导入
(1):查找出userList
(2):POI工具使用(基础)
jar包:
导出:
//创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = workbook.createSheet("hallo world");
//创建行
HSSFRow row = sheet.createRow(3);
//创建单元格
HSSFCell cell = row.createCell(3);
cell.setCellValue("Hello world");
//利用输出流导出
FileOutputStream out = new FileOutputStream("D:/filetest/test.xls");
workbook.write(out);
workbook.close();
out.close();
导入:
//输入流获取工作簿
FileInputStream in = new FileInputStream("D:\\filetest\\test.xls");
HSSFWorkbook workbook = new HSSFWorkbook(in);
//获取工作簿
HSSFSheet sheet = workbook.getSheetAt(0);
//获取行
HSSFRow row = sheet.getRow(3);
//获取单元格
HSSFCell cell = row.getCell(3);
//控制台输出单元格信息
System.out.println(cell.getStringCellValue());
workbook.close();
(3)根据需求导入+后缀名判断excel类型
导入:
try {
FileInputStream in = new FileInputStream(userExcel);
//根据文件名判断excel版本返回是否为03/xls的布尔值
boolean is03 = userExcelFileName.matches("^.+\\.(?i)(xls)$");
//利用接口类实现根据返回值创建相应类型的工作簿
Workbook workbook = is03 ? new HSSFWorkbook(in) : new XSSFWorkbook(in);
//创建以下所有
Sheet sheet = workbook.getSheetAt(0);
//判断有属性值时
if(sheet.getPhysicalNumberOfRows()>2){
User u;
//根据数据量循环获取行并给user赋值,每次循环新建一个对象并调用业务保存方法
for(int i=2;i<sheet.getPhysicalNumberOfRows();i++){
Row row = sheet.getRow(i);
u = new User();
u.setName(row.getCell(0).getStringCellValue());
u.setAccount(row.getCell(1).getStringCellValue());
u.setDept(row.getCell(2).getStringCellValue());
//数据库类型布尔值,excel表示String时的获取(0.0机智啊)
u.setGender(row.getCell(3).getStringCellValue().equals("男"));
u.setEmail(row.getCell(4).getStringCellValue());
u.setPassword("123456");
u.setState("1");
userDao.save(u);
}
}
//记得关闭流~
in.close();
workbook.close();
导出:
public static void exportUserExcel(List<User> userList, ServletOutputStream out) {
// TODO Auto-generated method stub
try {
//一系列创建
HSSFWorkbook workbook = new HSSFWorkbook();
//创建样式
HSSFCellStyle headStyle = creatStyle(workbook,(short)16);
HSSFCellStyle columnStyle = creatStyle(workbook, (short)13);
//一系列创建
HSSFSheet sheet = workbook.createSheet();
//合并首行单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
//设置列宽
sheet.setDefaultColumnWidth(20);
//一系列创建
HSSFRow headrow = sheet.createRow(0);
//创建首行
HSSFCell head = headrow.createCell(0);
//设置首行单元格内容+添加样式
head.setCellStyle(headStyle);
head.setCellValue("用户列表");
//创建列行
HSSFRow row1 = sheet.createRow(1);
//新建数组for遍历添加每列头信息
String[] columns = {"用户名","账号","所属部门","性别","电子邮箱"};
for(int i=0;i<columns.length;i++){
HSSFCell column = row1.createCell(i);
column.setCellStyle(columnStyle);
column.setCellValue(columns[i]);
}
//list不为空的话开始为后行添加list.size()行信息
if(userList != null){
for(int j=0;j<userList.size();j++){
HSSFRow contentRow = sheet.createRow(2+j);
HSSFCell cell1 = contentRow.createCell(0);
cell1.setCellValue(userList.get(j).getName());
HSSFCell cell2 = contentRow.createCell(1);
cell2.setCellValue(userList.get(j).getAccount());
HSSFCell cell3 = contentRow.createCell(2);
cell3.setCellValue(userList.get(j).getDept());
HSSFCell cell4 = contentRow.createCell(3);
cell4.setCellValue(userList.get(j).getGender()?"男":"女");
HSSFCell cell5 = contentRow.createCell(4);
cell5.setCellValue(userList.get(j).getEmail());
}
}
//关闭流~
workbook.write(out);
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}