上传excel中的内容到服务器
来源:互联网 发布:java垃圾回收方法 编辑:程序博客网 时间:2024/06/06 11:00
@RequestMapping("/importData")
public View importData(@RequestParam("files") UploadedFile[] files,
@RequestParam("cityCode") String cityCode){
List<White> whiteList=new ArrayList<White>();
if(!files[0].getPath().endsWith(".xls") && !files[0].getPath().endsWith(".xlsx")){
return new JsonView("文件格式错误!");
}
try {
String str = batchUpload(files[0]);
if(str != null &&! str.isEmpty()){
whiteList=readXls(str);
}
} catch (IOException e) {
logger.debug(e.toString());
}
int successNum=0;
for(White white: whiteList){
if(!StringUtils.isNullOrEmpty(cityCode)){
white.setCityCode(cityCode);
}
iWhiteHandler.addWhite(white);
successNum++;
}
String response="成功导入"+successNum+"条数据!";
Remoting<?> remoting = ContextHolder.holder.getLastRemoting();
AjaxResult<String> result = AjaxResult.create(remoting.isDone(), response, remoting.getLastErrMsg());
iSysLogHandler.Log(SysLogMode.MU_White.value,SysLogType.logImport.value,"");
return new JsonView(result);
}
//上传
public String batchUpload(UploadedFile files) throws IOException{
String newFilePath = "";
if(files !=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String prefix = sdf.format(new Date());
int i = 0;
String filePath = files.getPath(); //路径
String fileName = String.format("%s-%s", prefix,(++i)); //文件名
String ext = filePath.substring(filePath.lastIndexOf(".")+1); //文件后缀名
// String name1 = fileName +"."+ ext;
String dir = Paths.get(filePath).getParent().toString(); //盘符
String newName = dir + File.separator + fileName +"."+ ext; //拼接路径
File excelFile = new File(files.getPath());
File newFile = new File(newName);
// 新建文件
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
if (newFile.exists()) {
newFile.delete();
}
FileUtils.copyFile(excelFile,newFile); //复制文件
newFilePath = newFile.getPath();
}
return newFilePath;
}
//解析并添加到white对象
public List <White> readXls(String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
List<White> list = new ArrayList<White>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
White white=new White();
HSSFCell carNo = hssfRow.getCell(0);
HSSFCell unit = hssfRow.getCell(1);
HSSFCell person = hssfRow.getCell(2);
HSSFCell validationTime = hssfRow.getCell(3);
HSSFCell remark = hssfRow.getCell(4);
white.setVeNumber(carNo.getStringCellValue());
white.setBestowUnit(unit.getStringCellValue());
white.setBestowPeople(person.getStringCellValue());
white.setUsefuLlife(validationTime.getDateCellValue());
white.setRemark(remark.getStringCellValue());
list.add(white);
}
}
}
return list;
}
public View importData(@RequestParam("files") UploadedFile[] files,
@RequestParam("cityCode") String cityCode){
List<White> whiteList=new ArrayList<White>();
if(!files[0].getPath().endsWith(".xls") && !files[0].getPath().endsWith(".xlsx")){
return new JsonView("文件格式错误!");
}
try {
String str = batchUpload(files[0]);
if(str != null &&! str.isEmpty()){
whiteList=readXls(str);
}
} catch (IOException e) {
logger.debug(e.toString());
}
int successNum=0;
for(White white: whiteList){
if(!StringUtils.isNullOrEmpty(cityCode)){
white.setCityCode(cityCode);
}
iWhiteHandler.addWhite(white);
successNum++;
}
String response="成功导入"+successNum+"条数据!";
Remoting<?> remoting = ContextHolder.holder.getLastRemoting();
AjaxResult<String> result = AjaxResult.create(remoting.isDone(), response, remoting.getLastErrMsg());
iSysLogHandler.Log(SysLogMode.MU_White.value,SysLogType.logImport.value,"");
return new JsonView(result);
}
//上传
public String batchUpload(UploadedFile files) throws IOException{
String newFilePath = "";
if(files !=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String prefix = sdf.format(new Date());
int i = 0;
String filePath = files.getPath(); //路径
String fileName = String.format("%s-%s", prefix,(++i)); //文件名
String ext = filePath.substring(filePath.lastIndexOf(".")+1); //文件后缀名
// String name1 = fileName +"."+ ext;
String dir = Paths.get(filePath).getParent().toString(); //盘符
String newName = dir + File.separator + fileName +"."+ ext; //拼接路径
File excelFile = new File(files.getPath());
File newFile = new File(newName);
// 新建文件
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
if (newFile.exists()) {
newFile.delete();
}
FileUtils.copyFile(excelFile,newFile); //复制文件
newFilePath = newFile.getPath();
}
return newFilePath;
}
//解析并添加到white对象
public List <White> readXls(String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
List<White> list = new ArrayList<White>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
White white=new White();
HSSFCell carNo = hssfRow.getCell(0);
HSSFCell unit = hssfRow.getCell(1);
HSSFCell person = hssfRow.getCell(2);
HSSFCell validationTime = hssfRow.getCell(3);
HSSFCell remark = hssfRow.getCell(4);
white.setVeNumber(carNo.getStringCellValue());
white.setBestowUnit(unit.getStringCellValue());
white.setBestowPeople(person.getStringCellValue());
white.setUsefuLlife(validationTime.getDateCellValue());
white.setRemark(remark.getStringCellValue());
list.add(white);
}
}
}
return list;
}
0 0
- 上传excel中的内容到服务器
- 上传excel文件到服务器
- excel上传的两种方式(保存到服务器并读取/直接读取内容)
- ASP.NET通过存储过程上传Excel中的内容到Oracle数据库
- flex上传excel文件到服务器上
- 关于如何把canvas中的内容转换为图片上传到服务器的操作过程
- 用FileUpload将excel中的数据上传到服务器的数据库
- VC6.0 ADO 读取Excel 2003内容,并将读取的数据上传到远程服务器SQL2008表中
- Gridview中的内容导出到Excel
- Gridview中的内容导出到Excel
- 一、Gridview中的内容导出到Excel
- Gridview中的内容导出到Excel
- Gridview中的内容导出到Excel
- Gridview中的内容导出到Excel
- Gridview中的内容导出到Excel.
- Gridview中的内容导出到Excel
- 将数据库中的内容导入到excel
- 将DATASET中的内容导出到EXCEL
- 299. Bulls and Cows [easy] (Python)
- Struts2中redirect基本的经验之谈
- 【已解决】Linux下安装MySQL数据库
- nginx gzip 的使用
- java-使用字符串输出对象
- 上传excel中的内容到服务器
- kvm 虚拟化 virt-install 安装虚拟机
- Centos 6.5 - Red Hat 6.5 时区更改
- ruby on rails ,extjs框架下的ajax使用
- iOS开发 ☞ 各种CG结构体
- 相对路径与绝对路径
- Linux死锁调试之softlockup
- Fragment和Activity之间的数据传递(两种:Acitvity-->Fragment;Fragment---->Activity)
- JVM学习总结--体系结构、内存管理和垃圾回收