欢迎大家来到IT世界,在知识的湖畔探索吧!
1.反射部分代码
package business.platform.controller.gzff;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.lang.reflect.Field;
import java.math.BigDecimal;
public class Test1 {
public Object aa( Class className,Row row) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
Field[] declaredFields = className.getDeclaredFields();
Object obj = className.newInstance();
for (int i = 0; i < 35; i++) {
Cell cell = row.getCell(i);
//获取字段名称
Field declaredField = className.getDeclaredField(declaredFields[i].getName());
//关闭jdk校验
declaredField.setAccessible(true);
//获取字段类型
if (declaredField.getType().getName().equals("java.lang.String")) {
if (cell == null || cell.equals("")) {
declaredField.set(obj, "");
} else {
declaredField.set(obj, cell.toString());
}
} else if (declaredField.getType().getName().equals("java.math.BigDecimal")) {
if (cell == null || cell.equals("")) {
declaredField.set(obj, BigDecimal.valueOf(0));
} else {
declaredField.set(obj, BigDecimal.valueOf(Double.parseDouble(cell.toString())));
}
}
}
return obj;
}
}
欢迎大家来到IT世界,在知识的湖畔探索吧!
2.导入部分代码
欢迎大家来到IT世界,在知识的湖畔探索吧! @RequestMapping("importData")
@ResponseBody
public ResponseVO importData(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException, IllegalAccessException, NoSuchFieldException, InstantiationException, InterruptedException {
MultipartParamUtils mu = new MultipartParamUtils(request, 1024 * 1024 * 100);
InputStream isFile = mu.getParameterData("fileforload");
String strFileType = new String(mu.getParameter("fileforload").getBytes(), "UTF-8");
////得到工作簿
Workbook wookbook = new HSSFWorkbook(isFile);
//获取第一个sheet页的数据
Sheet sheet = wookbook.getSheetAt(0);
//获取最后一行
int lastRowNum = sheet.getLastRowNum();
//从第五行开始-前四行都标题
int numHang = 4;
List<Gongzffxx> gongzffxxes = new ArrayList<>();
Gongzffxx gzff = new Gongzffxx();
Row row = sheet.getRow(numHang);
Test1 test1 = new Test1();
for (int i = 0; i < lastRowNum - 5 ; i++) {
sheet.getRow(numHang);
gzff = (Gongzffxx)test1.aa(Gongzffxx.class, row);
//主键id
UUID uuid = UUID.randomUUID();
gzff.setId(uuid.toString());
gongzffxxes.add(gzff);
numHang++;
}
return ResponseVO.success().put("code","1");
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/17975.html