java 形状,Excel中转文件的实例详解

  

<强> java 中Excel转形状文件的实例详解

  

<强>概述:
  

  

本文讲述如何结合geotools和POI实现Excel到轴马力的转换,再结合前文轴马力到geojson数据的转换,即可实现用户上传Excel数据并在web端的展示功能。

  

截的图:   

 java ,中Excel转形状文件的实例详解

  

,原始Excel文件

  

 java ,中Excel转形状文件的实例详解

  

运行耗时

  

 java ,中Excel转形状文件的实例详解

  

运行结果

  

代码:
  

        包com.lzugis.geotools;      进口com.lzugis.CommonMethod;   进口com.vividsolutions.jts.geom.Coordinate;   进口com.vividsolutions.jts.geom.GeometryFactory;   进口com.vividsolutions.jts.geom.Point;   进口org.apache.poi.hssf.usermodel.HSSFCell;   进口org.apache.poi.hssf.usermodel.HSSFRow;   进口org.apache.poi.hssf.usermodel.HSSFSheet;   进口org.apache.poi.hssf.usermodel.HSSFWorkbook;   进口org.apache.poi.poifs.filesystem.POIFSFileSystem;   进口org.geotools.data.FeatureWriter;   进口org.geotools.data.Transaction;   进口org.geotools.data.shapefile.ShapefileDataStore;   进口org.geotools.data.shapefile.ShapefileDataStoreFactory;   进口org.geotools.feature.simple.SimpleFeatureTypeBuilder;   进口org.geotools.referencing.crs.DefaultGeographicCRS;   进口org.opengis.feature.simple.SimpleFeature;   进口org.opengis.feature.simple.SimpleFeatureType;      进口java.io.File;   进口java.io.FileInputStream;   进口java.io.InputStream;   进口java.io.Serializable;   进口charset;   进口java.util.ArrayList;   进口java.util.HashMap;   进口并不知道;   进口java.util.Map;/* *   *由2017/9/6管理。   */公开课Xls2Shape {   静态Xls2Shape xls2Shp=new Xls2Shape ();   私有静态字符串rootPath=System.getProperty (“user.dir”);   私人CommonMethod厘米=new CommonMethod ();      私人HSSFSheet表;      私有类getCellType (HSSFCell细胞){   如果(cell.getCellType ()==HSSFCell.CELL_TYPE_STRING) {   返回String.class;   }else if (cell.getCellType ()==HSSFCell.CELL_TYPE_NUMERIC) {   返回Double.class;   其他}{   返回String.class;   }   }      私有对象getCellValue (HSSFCell细胞){   如果(cell.getCellType ()==HSSFCell.CELL_TYPE_STRING) {   .getString返回cell.getRichStringCellValue () ();   }else if (cell.getCellType ()==HSSFCell.CELL_TYPE_NUMERIC) {   返回cell.getNumericCellValue ();   其他}{   返回";   }   }      私人List比;getExcelHeader () {   List比;=new ArrayList列表();   HSSFRow头=sheet.getRow (0);   HSSFRow价值=https://www.yisu.com/zixun/sheet.getRow (1);//获取总列数   int colNum=header.getPhysicalNumberOfCells ();   for (int i=0;我=new HashMap <字符串、对象> ();   地图。把(“name”字段名);   地图。把(“类型”,fieldType);   list.add(地图);   }   返回列表;   }      xlsfile公共空excel2Shape(字符串,字符串shppath) {   POIFSFileSystem fs;   HSSFWorkbook白平衡;   HSSFRow行;   尝试{   InputStream是=new FileInputStream (xlsfile);   fs=new POIFSFileSystem(是);   世行=new HSSFWorkbook (fs);   表=wb.getSheetAt (0);//获取总列数   int colNum=sheet.getRow (0) .getPhysicalNumberOfCells ();//得到总行数   int rowNum=sheet.getLastRowNum ();      名单列表=getExcelHeader ();//创建形状文件对象   文件文件=新文件(shppath);   Serializable> Map<字符串;params=new HashMap<字符串,Serializable> ();   params.put (ShapefileDataStoreFactory.URLP。关键,file.toURI () .toURL ());   ShapefileDataStore ds=(ShapefileDataStore)新ShapefileDataStoreFactory () .createNewDataStore (params);//定义图形信息和属性信息   SimpleFeatureTypeBuilder结核?new SimpleFeatureTypeBuilder ();   tb.setCRS (DefaultGeographicCRS.WGS84);   tb.setName (“shapefile”);   结核病。add (“the_geom Point.class);   for (int i=0;我& lt;list.size ();我+ +){   Object> Map

java 形状,Excel中转文件的实例详解