java parsing excel table data

package com.hans.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hans.pojo.Orderinfo;

@Controller
@RequestMapping("/xcel")
public class Excel_xls {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
@RequestMapping("/findexcel")
public String excel(HttpServletRequest request) throws Exception
{

/*File excelFile = new File("C:\fakepath\123.xlsx"); //替换你文档地址
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(excelFile));
} catch (IOException e) {
e.printStackTrace();
}
int numberOfSheets = wb.getNumberOfSheets();
String str = "";*/
MultipartHttpServletRequest mult=(MultipartHttpServletRequest) request;

MultipartFile file=mult.getFile("pic");
if(file.isEmpty()){
System.out.println("文件为空");
}else{
System.out.println("文件不为空");
}
InputStream in = null;
in = file.getInputStream();
List list= getBankListByExcelTitle(in, file.getOriginalFilename(), 14);
for( int i=0;i<list.size();i++){
//System.out.println(list.get(i));
List one=(List) list.get(i);
/*for(int j=0;j<one.size();j++){
System.out.println(one.get(j));
}*/
Orderinfo orderinfo=new Orderinfo();

String first=(String) one.get(0);

orderinfo.setContractName(first);
}
return null;
}

/**
* Description: Get the data in the IO stream and assemble it into a List<List<Object>> object
* @param in,fileName
* @return
* @throws IOException
*/
public static List<List<Object>> getBankListByExcelTitle(InputStream in,String fileName,Integer lastCellNum) throws Exception{
List<List<Object>> list = null;

//Create Excel Workbook Technical Department
Workbook work = getWorkbook(in,fileName);
if(null == work){
throw new Exception("Create Excel worksheet is empty!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;

list = new LinkedList<List<Object>>();
// Traverse all sheets in Excel
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;}

//遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum(); j < sheet.getPhysicalNumberOfRows(); j++) {
row = sheet.getRow(j);
/*if(row==null||row.getFirstCellNum()==j){continue;}*/

//遍历所有的列
List<Object> link = new LinkedList<Object>();

int xx1=row.getLastCellNum();
int xx2=0;
if(lastCellNum == null) {
lastCellNum = Integer.valueOf(row.getLastCellNum());
}
for (int y = row.getFirstCellNum(); y < row.getLastCellNum() && y < lastCellNum; y++) {
cell = row.getCell(y);
Object val=getCellValue(cell);
if(cell==null || val==null || val.equals("")|| val.equals(" ")){
xx2++;
}
link.add(val);
}
if(xx1==xx2 || xx2 == lastCellNum){
continue;
}
list.add(link);
}
}
return list;
}
/**
* Description: According to the file suffix, adapt the version of the uploaded file
* @param inStr,fileName
* @return
* @throws Exception
*/
public static Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if( excel2003L.equals(fileType)) { wb = new HSSFWorkbook(inStr
); //2003-
}else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}else{
throw new Exception ("The parsed file is in the wrong format!");
}
return wb;
}

/**
* Description: Format the value in the table
* @param cell
* @return
*/
public static Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //Format Convert number String characters
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //Date formatting
DecimalFormat df2 = new DecimalFormat("0.00"); //Format numbers

if(cell != null){
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString().trim();
break;
case Cell.CELL_TYPE_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString())){
String str=cell.getNumericCellValue()+"";
String[] strs=str.split("\\.");
double str1=Double.parseDouble(strs[1]);
if(str1>0){
value = df2.format(cell.getNumericCellValue()).trim();
}else{
value = df.format(cell.getNumericCellValue()).trim();
}
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue()).trim();
}else{
value = df2.format(cell.getNumericCellValue()).trim();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
}

return value;
}
}

 

 

 

 

 

 

html page

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function show()
{
var c=document.getElementById("pic").value;

}
</script>
<body>
<form action="/SSM/xcel/findexcel" method="post" enctype="multipart/form-data">
<input type="file" name="pic" id="pic" />
<input type="submit" onclick="show()" value="提交">
</form>
</body>
</html>

 

 

 

 

 

springMVC remembers configuration file upload

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819082&siteId=291194637