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)文章
基于spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯
這篇文章主要介紹了spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
淺談?dòng)肧pringBoot實(shí)現(xiàn)策略模式
本文主要介紹了SpringBoot實(shí)現(xiàn)策略模式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
JavaWeb的監(jiān)聽(tīng)器和過(guò)濾器你了解嗎
這篇文章主要為大家詳細(xì)介紹了JavaWeb的監(jiān)聽(tīng)器和過(guò)濾器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法
Java語(yǔ)言的Calendar(日歷),Date(日期),和DateFormat(日期格式)組成了Java標(biāo)準(zhǔn)的一個(gè)基本但是非常重要的部分,下面這篇文章主要給大家介紹了關(guān)于java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法的相關(guān)資料,需要的朋友可以參考下2023-12-12
java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解
這篇文章主要介紹了java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
關(guān)于Java中try finally return語(yǔ)句的執(zhí)行順序淺析
這篇文章主要介紹了關(guān)于Java中try finally return語(yǔ)句的執(zhí)行順序淺析,需要的朋友可以參考下2017-08-08
java 序列化對(duì)象 serializable 讀寫數(shù)據(jù)的實(shí)例
java 序列化對(duì)象 serializable 讀寫數(shù)據(jù)的實(shí)例,需要的朋友可以參考一下2013-03-03

