java 中Excel轉(zhuǎn)shape file的實例詳解
java 中Excel轉(zhuǎn)shape file的實例詳解
概述:
本文講述如何結(jié)合geotools和POI實現(xiàn)Excel到shp的轉(zhuǎn)換,再結(jié)合前文shp到geojson數(shù)據(jù)的轉(zhuǎn)換,即可實現(xiàn)用戶上傳excel數(shù)據(jù)并在web端的展示功能。
截圖:
原始Excel文件
運行耗時
運行結(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文件對象 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("共耗時" + (System.currentTimeMillis() - start) + "ms"); } }
說明:
1、轉(zhuǎn)換僅限點對象的轉(zhuǎn)換;
2、保留所有excel相關(guān)的屬性,lon、lat字段是必須要有的;
3、對于中文字段,做了取首字母的處理;
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- java生成餅圖svg及JFreeChart生成svg圖表
- Java基于Spire Cloud Excel把Excel轉(zhuǎn)換成PDF
- Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法
- java實現(xiàn)在線預(yù)覽--poi實現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
- java 讀取excel文件轉(zhuǎn)換成json格式的實例代碼
- Java實現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能
- Java實現(xiàn)excel表格轉(zhuǎn)成json的方法
- java實現(xiàn)excel和txt文件互轉(zhuǎn)
- java POI解析Excel 之數(shù)據(jù)轉(zhuǎn)換公用方法(推薦)
- Java實現(xiàn)把excel xls中數(shù)據(jù)轉(zhuǎn)為可直接插入數(shù)據(jù)庫的sql文件
- Java 將Excel轉(zhuǎn)為SVG的方法
相關(guān)文章
基于spring?@Cacheable?注解的spel表達式解析執(zhí)行邏輯
這篇文章主要介紹了spring?@Cacheable?注解的spel表達式解析執(zhí)行邏輯,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法
Java語言的Calendar(日歷),Date(日期),和DateFormat(日期格式)組成了Java標準的一個基本但是非常重要的部分,下面這篇文章主要給大家介紹了關(guān)于java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法的相關(guān)資料,需要的朋友可以參考下2023-12-12java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解
這篇文章主要介紹了java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09關(guān)于Java中try finally return語句的執(zhí)行順序淺析
這篇文章主要介紹了關(guān)于Java中try finally return語句的執(zhí)行順序淺析,需要的朋友可以參考下2017-08-08java 序列化對象 serializable 讀寫數(shù)據(jù)的實例
java 序列化對象 serializable 讀寫數(shù)據(jù)的實例,需要的朋友可以參考一下2013-03-03