java 中Excel轉(zhuǎn)shape file的實(shí)例詳解
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é)果
代碼:
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");
}
}
說(shuō)明:
1、轉(zhuǎn)換僅限點(diǎn)對(duì)象的轉(zhuǎn)換;
2、保留所有excel相關(guān)的屬性,lon、lat字段是必須要有的;
3、對(duì)于中文字段,做了取首字母的處理;
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java生成餅圖svg及JFreeChart生成svg圖表
- Java基于Spire Cloud Excel把Excel轉(zhuǎn)換成PDF
- Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法
- java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
- java 讀取excel文件轉(zhuǎn)換成json格式的實(shí)例代碼
- Java實(shí)現(xiàn)的Excel列號(hào)數(shù)字與字母互相轉(zhuǎn)換功能
- Java實(shí)現(xiàn)excel表格轉(zhuǎn)成json的方法
- java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)
- java POI解析Excel 之?dāng)?shù)據(jù)轉(zhuǎn)換公用方法(推薦)
- Java實(shí)現(xiàn)把excel xls中數(shù)據(jù)轉(zhuǎn)為可直接插入數(shù)據(jù)庫(kù)的sql文件
- Java 將Excel轉(zhuǎn)為SVG的方法
相關(guān)文章
SpringBoot注冊(cè)Servlet的三種方法詳解
這篇文章主要介紹了SpringBoot注冊(cè)Servlet的三種方法詳解,教你如何Spring Boot 注冊(cè) Servlet、Filter、Listener,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作
這篇文章主要介紹了Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
簡(jiǎn)單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析
這篇文章主要介紹了Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java并發(fā)容器ConcurrentLinkedQueue解析
這篇文章主要介紹了Java并發(fā)容器ConcurrentLinkedQueue解析,2023-12-12
Java語(yǔ)言----三種循環(huán)語(yǔ)句的區(qū)別介紹
下面小編就為大家?guī)?lái)一篇Java語(yǔ)言----三種循環(huán)語(yǔ)句的區(qū)別介紹。小編舉得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
java教程之java程序編譯運(yùn)行圖解(java程序運(yùn)行)
最近重新復(fù)習(xí)了一下java基礎(chǔ),在使用javap的過(guò)程中遇到了一些問(wèn)題,這里便講講對(duì)于一個(gè)類文件如何編譯、運(yùn)行、反編譯的。也讓自己加深一下印象2014-03-03
將JSON字符串?dāng)?shù)組轉(zhuǎn)對(duì)象集合方法步驟
這篇文章主要給大家介紹了關(guān)于將JSON字符串?dāng)?shù)組轉(zhuǎn)對(duì)象集合的方法步驟,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08

