作者:taco
最近在支持客户的过程中,有客户想要实现excel数据的导入,但是excel数据并没有单独的import方法进行导入,那么针对于该类型的数据应该如何进行导入呢?导入后的数据又如何创建点数据集来存储这个呢?本篇文章将介绍如何导入excel数据。
一、打开并读取excel数据
首先需要读取excel数据,我们直接通过OpenFileDialog方法拿到本地的文件即可。
string excelName = "";
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "打开excel数据";
fileDialog.Filter = "Excel数据文件(*.xlsx)|*.xlsx";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
excelName = fileDialog.FileName;
}
if (excelName == "")
{
return;
}
DataSet ds = GetExcelData(excelName);
同时我们构造一个GetExcelData方法用来读取excel中的数据,并返回我们需要的【Sheet1$】中的数据信息。根据实际情况可对读取方法进行更改。
private DataSet GetExcelData(string str)
{
try
{
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();
return myDataSet;
}
catch (System.Exception ex)
{
return null;
}
}
此时我们就完成了数据的读取。
二、创建矢量数据集
我们通过DatasetVectorInfo信息来构建一个点的矢量数据集。设置矢量类型为点类型的数据。
DatasetVectorInfo dvi = new DatasetVectorInfo();
dvi.Name = m_datasource.Datasets.GetAvailableDatasetName(System.IO.Path.GetFileNameWithoutExtension(excelName));
dvi.Type = DatasetType.Point;
DatasetVector dv = m_datasource.Datasets.Create(dvi);
矢量数据集创建好后,我们为矢量数据集构造一个字段集合的类。
FieldInfos fldInfos = dv.FieldInfos;
遍历我们刚才获取的excel表,将字段值添加至fieldinfos中。
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
DataColumn dc = ds.Tables[0].Columns[i];
FieldInfo fldInfo = new FieldInfo();
fldInfo.Name = dc.Caption; //字段名称
if (i == 0)//x
{
fldInfo.Type = FieldType.Text;
}
else if (i == 1)//y
{
fldInfo.Type = FieldType.Text;
}
else if (i == 2)
{
fldInfo.Type = FieldType.Text;
}
else
{
continue;
}
fldInfos.Add(fldInfo);
}
三、编辑矢量数据集
由于更新数量相对来说较多我们可以开启批量添加
//批量添加记录
// 获取记录集
Recordset recordset = dv.GetRecordset(false, CursorType.Dynamic);
// 获得记录集对应的批量更新对象
Recordset.BatchEditor editor = recordset.Batch;
// 设置批量更新每次提交的记录数目
editor.MaxRecordCount = 50;
// 批量更新到数据集中
editor.Begin();
接下来遍历excel中的记录将其存储值记录集中即可。首先我们找到点的经纬度信息
DataRow dr = ds.Tables[0].Rows[m];
String s_x = dr[0].ToString().Trim();
String s_y = dr[1].ToString().Trim();
这里构造了个GetPoint方法来创建GeoPoint,该方法可以根据double类型的点创建点数据集。如果有别的类型的话可以自行创建。通过度分秒来构建的点数据集可以在我的资源中下载完整实例。完整实例中提供了如何转换的方式。
private Geometry GetPoint(String s_x, String s_y)
{
try
{
Double dX = 0.0;
Double dY = 0.0;
Geometry gm = null;
dX = Double.Parse(s_x);
dY = Double.Parse(s_y);
GeoPoint gp = new GeoPoint(dX, dY);
gm = (Geometry)gp;
return gm;
}
catch (System.Exception ex)
{
return null;
}
}
上面构造了geometry的几何参数,那么属性应该如何传入呢?我们最终的传入方式是通过AddNew方法来进行传入的。传入的第一个即使我们上面一步拿到的geometry,第二步则需要构建字典将其传入。
dictionary = new Dictionary<string, object>();
for (Int32 j = 0; j < fldInfos.Count; j++)
{
FieldInfo fieldInfo = fldInfos[j];
if (!fieldInfo.IsSystemField)
{
if (fieldInfo.Name.Contains("SM")) { continue; }//避免存入SMUSERID字段
if (dr[n] == null) { continue; }
dictionary.Add(fieldInfo.Name, dr[n].ToString());
n++;
}
}
recordset.AddNew(geometry, dictionary);
此时注意如果fieldInfo.Name 并不存在于矢量数据集中的字段的话,则不能更新进去的哦!
将最后的结果统一提交就完成了。
editor.Update();
四、点添加到地图中
我们根据名称拿到创建好的点数据集,通过Map.layers.Add方法即可将其添加至地图中了。这里给点设置了一个新的符号风格。
DatasetVector dataset = m_datasource.Datasets[dvi.Name] as DatasetVector;
//设置风格并添加数据集
//Set the style and add the point dataset
Layer layer = null;
if (isWithStyle)
{
LayerSettingVector setting = new LayerSettingVector();
setting.Style.LineColor = Color.SeaGreen;
setting.Style.MarkerSize = new Size2D(4, 4);
setting.Style.MarkerSymbolID = 12;
layer = m_mapControl.Map.Layers.Add(dataset, setting, true);
}
else
{
layer = m_mapControl.Map.Layers.Add(dataset, true);
}
//全幅显示添加的图层
m_mapControl.Map.EnsureVisible(layer);
m_mapControl.Map.Refresh();
于是我们就完成了Excel导入点的操作。大家有什么基于功能需要实现的可以留言给我哈哈哈哈。完整代码可以在我的资源中下载(https://download.csdn.net/download/weixin_42532695/88595864)