目錄
1.Intro
偶爾翻到了去年上半年寫的項目,覺得這個方法不錯,拿出來共享一下。爲了便於屬性提取,寫了個導出成Excel的函數,用到了NPOI控件的方法,具體實現很簡單,先獲取要導出屬性表的要素類,然後將要素類的屬性錶轉換爲DataTable,再導出就方便很多了。
2.Details
首先將要素類的屬性錶轉化爲DataTable類型的數據存儲,這裏使用了DevExpress的GridControl控件進行數據源關聯,
// dataTable爲要素類屬性錶轉化的DataTable類型的對象
this.gridControl.DataSource = dataTable;
然後就可以顯示屬性表,比微軟自帶的DataGridView方便很多,功能也強大很多,同時可以在屬性表中自定義右鍵事件,能夠刪除列和行(不會對原始的屬性數據造成修改),從而便捷的定義要導出的數據。注意,在綁定數據源之後,由於GridControl的數據源是和DataTable對象雙向關聯的,所以對任何一個造成修改,都會影響另一個。
導出效果。
3.Environment
Environment:Windows 7及以上
Language:C#
IDE:Visual Studio 2012
SDK:ArcGIS Engine 10.2
4.Source
(1) 將要素類存儲爲DataTable
/// <summary>
/// 獲取要素屬性表
/// </summary>
/// <param name="pFeatureClass">要素類對象</param>
/// <returns>返回DataTable對象</returns>
public static DataTable Get_AttributesTable(IFeatureClass pFeatureClass)
{
string geometryType = string.Empty;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
geometryType = "點";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint)
{
geometryType = "點集";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
geometryType = "折線";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
geometryType = "面";
}
// 字段集合
IFields pFields = pFeatureClass.Fields;
int fieldsCount = pFields.FieldCount;
// 寫入字段名
DataTable dataTable = new DataTable();
for (int i = 0; i < fieldsCount; i++)
{
dataTable.Columns.Add(pFields.get_Field(i).Name);
}
// 要素遊標
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return dataTable;
}
// 獲取MZ值
IMAware pMAware = pFeature.Shape as IMAware;
IZAware pZAware = pFeature.Shape as IZAware;
if (pMAware.MAware)
{
geometryType += " M";
}
if (pZAware.ZAware)
{
geometryType += "Z";
}
// 寫入字段值
while (pFeature != null)
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < fieldsCount; i++)
{
if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
dataRow[i] = geometryType;
}
else
{
dataRow[i] = pFeature.get_Value(i).ToString();
}
}
dataTable.Rows.Add(dataRow);
pFeature = pFeatureCursor.NextFeature();
}
// 釋放遊標
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
return dataTable;
}
(2) 生成Excel文檔
/// <summary>
/// 生成Excel文件
/// </summary>
/// <param name="dataTable">數據表對象</param>
/// <param name="filePath">輸出路徑</param>
private void CreateExcelFile(DataTable dataTable, string filePath)
{
NPOI.SS.UserModel.IWorkbook workbook = null;
if (System.IO.Path.GetExtension(filePath) == ".xls")
{
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
}
else
{
workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
}
// 創建行
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("屬性表");
NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
NPOI.SS.UserModel.ICell cell = null;
// 寫入列名
int columnCount = dataTable.Columns.Count;
for (int i = 0; i < columnCount; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(dataTable.Columns[i].ColumnName);
}
// 寫入列值
int rowCount = dataTable.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
row = sheet.CreateRow(i + 1);
for (int j = 0; j < columnCount; j++)
{
cell = row.CreateCell(j);
cell.SetCellValue(dataTable.Rows[i][j].ToString());
}
}
// 寫入文件
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
workbook.Write(fileStream);
fileStream.Close();
}
5.Conclusion
NPOI 2.0.1.0 破解版
百度網盤鏈接:https://pan.baidu.com/s/104FAoGPGjoYSFFXm57fzwg
提取碼:7hry