package com.sprucetec.tms.controller.utils;
import com.alibaba.dubbo.common.utils.IOUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.json.JSONArray;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;
/**
* ImportExcelUtil
*
* @author Yinqiang Du
* @date 2016/7/6
*/
public class ImportExcelUtil {
private static final ObjectMapper mapper = new ObjectMapper();
public List importData(String templateFileName, Class beanClass) {
List list = Lists.newArrayList();
try {
InputStream is = new FileInputStream(templateFileName);
String line;//用来保存每行保存的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine();//读取第一行
StringBuffer buffer = new StringBuffer();
while (line == null) {//如果line为空,说明读取完了.
buffer.append(line);//将读取到的内容加到buffer中
buffer.append("\n");//添加换行
line = reader.readLine();//读取下一行
}
reader.close();//关闭收录机
is.close();//关闭流
String json = buffer.toString();//转换成字符串
JSONArray jsonArray = new JSONArray(json);
for (Object o : jsonArray) {
list.add(mapper.readValue(o.toString(), beanClass));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static List importData(MultipartFile file, Class beanClass) throws IOException {
String[] lines = IOUtils.readLines(file.getInputStream());
String json = "";
for (String line : lines) {
json += line;
}
JSONArray jsonArray = new JSONArray(json);
List list = Lists.newArrayList();
for (Object o : jsonArray) {
list.add(mapper.readValue(o.toString(), beanClass));
}
return list;
}
public void importExcle(MultipartFile file){
Workbook wb=null;
try {
wb = new HSSFWorkbook(file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
String cjid = row.getCell(7).toString();
//Cjxx c = xxxService.getById(cjid);
//if(c!=null){
int min=4;
int max = sheet.getLastRowNum();
List<Object> listx = Lists.newArrayList();
for(int i=min;i<=max;i++){
Row rowc = sheet.getRow(i);
String[] o = new String[7];
for(int j=1;j<rowc.getLastCellNum();j++){
o[j-1] = rowc.getCell(j).toString();
}
Object object =new Object();
//o.setXx(o[[0]);
listx.add(object);
//xxService.update(object.xx);
}
}
}