每张Excel的sheet有多少列是随机不确定的,每列头作为字段信息,通过获取Excel表sheet每列头信息,作为表的字段信息,动态创建临时表,并且获取临时表中数据。
一:
public interface ProjectInfoMapper extends Mapper<ProjectInfo> {
//动态创建临时表
void createTables(String[] cloums);
//查询临时表中的数据
List<String> selectResult();
//插入数据到临时表
void insetData(String[] name,Map<String,String> names);
}
二:
<mapper namespace="com.zhunda.dao.ProjectInfoMapper">
<update id="createTables" >
create TEMPORARY table tempTable (
<foreach collection="array" item="listItem" separator=",">
${listItem} varchar(30)
</foreach>
) ENGINE=InnoDB DEFAULT CHARSET=utf8
</update>
<insert id="insetData">
insert tempTable(
<foreach collection="name" item="listItem" separator=",">
${listItem}
</foreach>
)
values (
<foreach collection="names" item="listItems" separator=",">
' ${listItems} '
</foreach>
)
</insert>
<select id="selectResult" parameterType="map" resultType="java.util.Map">
select * from tempTable
</select>
</mapper>
三:
@RunWith(SpringRunner.class)
@SpringBootTest
public class AutoTableTest {
@Autowired
private ProjectInfoMapper projectInfoMapper;
@Test
public void Test(){
//假设解析Excel得到的map数据
Map<String,String> map=new HashMap<>();
map.put("a","aaaaaa");
map.put("b","bbbbbbbbbbb");
map.put("c","ccccccccccc");
//假设解析Excel得到的sheet列的头信息,一般头信息为中文,需处理
String[] s={"a","b","c"};
//动态创建临时表
projectInfoMapper.createTables(s);
//将解析数据插入临时表
projectInfoMapper.insetData(s,map);
//查询临时表中数据
List<String> strings = projectInfoMapper.selectResult();
System.out.println(strings);
}
}
四:
------------------->>>>>>>>解析Excel得到想要的数据<<<<<<<<<--------------------------------