java 中excel轉(zhuǎn)shape file的實(shí)例詳解
概述:
本文講述如何結(jié)合geotools和poi實(shí)現(xiàn)excel到shp的轉(zhuǎn)換,再結(jié)合前文shp到geojson數(shù)據(jù)的轉(zhuǎn)換,即可實(shí)現(xiàn)用戶上傳excel數(shù)據(jù)并在web端的展示功能。
截圖:
原始excel文件
運(yùn)行耗時(shí)
運(yùn)行結(jié)果
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package com.lzugis.geotools; import com.lzugis.commonmethod; import com.vividsolutions.jts.geom.coordinate; import com.vividsolutions.jts.geom.geometryfactory; import com.vividsolutions.jts.geom.point; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.poifs.filesystem.poifsfilesystem; import org.geotools.data.featurewriter; import org.geotools.data.transaction; import org.geotools.data.shapefile.shapefiledatastore; import org.geotools.data.shapefile.shapefiledatastorefactory; import org.geotools.feature.simple.simplefeaturetypebuilder; import org.geotools.referencing.crs.defaultgeographiccrs; import org.opengis.feature.simple.simplefeature; import org.opengis.feature.simple.simplefeaturetype; import java.io.file; import java.io.fileinputstream; import java.io.inputstream; import java.io.serializable; import java.nio.charset.charset; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; /** * created by admin on 2017/9/6. */ public class xls2shape { static xls2shape xls2shp = new xls2shape(); private static string rootpath = system.getproperty( "user.dir" ); private commonmethod cm = new commonmethod(); private hssfsheet sheet; private class getcelltype(hssfcell cell) { if (cell.getcelltype() == hssfcell.cell_type_string) { return string. class ; } else if (cell.getcelltype() == hssfcell.cell_type_numeric) { return double . class ; } else { return string. class ; } } private object getcellvalue(hssfcell cell) { if (cell.getcelltype() == hssfcell.cell_type_string) { return cell.getrichstringcellvalue().getstring(); } else if (cell.getcelltype() == hssfcell.cell_type_numeric) { return cell.getnumericcellvalue(); } else { return "" ; } } private list<map<string, object>> getexcelheader() { list<map<string, object>> list = new arraylist(); hssfrow header = sheet.getrow( 0 ); hssfrow value = sheet.getrow( 1 ); //獲取總列數(shù) int colnum = header.getphysicalnumberofcells(); for ( int i = 0 ; i < colnum; i++) { hssfcell cellfield = header.getcell(i); hssfcell cellvalue = value.getcell(i); string fieldname = cellfield.getrichstringcellvalue().getstring(); fieldname = cm.getpinyinheadchar(fieldname); class fieldtype = getcelltype(cellvalue); map<string, object> map = new hashmap<string, object>(); map.put( "name" , fieldname); map.put( "type" , fieldtype); list.add(map); } return list; } public void excel2shape(string xlsfile, string shppath) { poifsfilesystem fs; hssfworkbook wb; hssfrow row; try { inputstream is = new fileinputstream(xlsfile); fs = new poifsfilesystem(is); wb = new hssfworkbook(fs); sheet = wb.getsheetat( 0 ); //獲取總列數(shù) int colnum = sheet.getrow( 0 ).getphysicalnumberofcells(); // 得到總行數(shù) int rownum = sheet.getlastrownum(); list list = getexcelheader(); //創(chuàng)建shape文件對(duì)象 file file = new file(shppath); map<string, serializable> params = new hashmap<string, serializable>(); params.put(shapefiledatastorefactory.urlp.key, file.touri().tourl()); shapefiledatastore ds = (shapefiledatastore) new shapefiledatastorefactory().createnewdatastore(params); //定義圖形信息和屬性信息 simplefeaturetypebuilder tb = new simplefeaturetypebuilder(); tb.setcrs(defaultgeographiccrs.wgs84); tb.setname( "shapefile" ); tb.add( "the_geom" , point. class ); for ( int i = 0 ; i < list.size(); i++) { map<string, object> map = (map<string, object>) list.get(i); tb.add(map.get( "name" ).tostring(), ( class ) map.get( "type" )); } ds.createschema(tb.buildfeaturetype()); //設(shè)置編碼 charset charset = charset.forname( "gbk" ); ds.setcharset(charset); //設(shè)置writer featurewriter<simplefeaturetype, simplefeature> writer = ds.getfeaturewriter(ds.gettypenames()[ 0 ], transaction.auto_commit); //寫下一條 simplefeature feature = null ; for ( int i = 1 ; i < rownum; i++) { row = sheet.getrow(i); feature = writer.next(); map maplonlat = new hashmap(); for ( int j = 0 ; j < colnum; j++) { hssfcell cell = row.getcell(j); map<string, object> mapfields = (map<string, object>) list.get(j); string fieldname = mapfields.get( "name" ).tostring(); feature.setattribute(fieldname, getcellvalue(cell)); if (fieldname.tolowercase().equals( "lon" ) || fieldname.tolowercase().equals( "lat" )) { maplonlat.put(fieldname, getcellvalue(cell)); } } feature.setattribute( "the_geom" , new geometryfactory().createpoint( new coordinate(( double ) maplonlat.get( "lon" ), ( double ) maplonlat.get( "lat" )))); } writer.write(); writer.close(); ds.dispose(); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { long start = system.currenttimemillis(); string xlspath = rootpath + "/data/xls/capital.xls" , shppath = rootpath + "/out/capital.shp" ; xls2shp.excel2shape(xlspath, shppath); system.out.println( "共耗時(shí)" + (system.currenttimemillis() - start) + "ms" ); } } |
說明:
1、轉(zhuǎn)換僅限點(diǎn)對(duì)象的轉(zhuǎn)換;
2、保留所有excel相關(guān)的屬性,lon、lat字段是必須要有的;
3、對(duì)于中文字段,做了取首字母的處理;
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/gisshixisheng/article/details/77877031