最近做了一些读取shp属性表另存为excel表格和写入word文档的小事情,把思路和代码记下来,以备以后查看,各位大神看到请绕道,高抬贵手
条件如下:必备一个word文档,且里面必须有一张空表,假如我只需要读取shp属性表的两个字段:City(市名)和affcountyN(受灾县数量)
具体代码如下:
public class Helper
{
public bool Execute(string excelPath, string docPath, string shpPath, out string message)
{
return ExecuteEX(excelPath, docPath, shpPath, out message);
}
///
/// 执行函数
///
/// excel路径
/// 文档路径
/// shp文件路径
///
private bool ExecuteEX(string excelPath, string docPath, string shpPath, out string message)
{
try
{
if (!judgeInOrOutFile(excelPath, docPath, shpPath))
{
message = “文件输入不正确!”;
return false;
}
//判断excel文件是否存在,若存在删除
if (File.Exists(excelPath))
{
File.Delete(excelPath);
}
string docResultPath = Path.Combine(Path.GetDirectoryName(docPath), “data.doc”);
//判断doc文件是否存在,若存在删除
if (File.Exists(docResultPath))
{
File.Delete(docResultPath);
}
//拷贝一份word文档
File.Copy(docPath, docResultPath);
//打开shp
string folder = Path.GetDirectoryName(shpPath);
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(folder, 0);
IFeatureClass pFeatureClass = pWS.OpenFeatureClass(Path.GetFileNameWithoutExtension(shpPath));
//获得shp属性表并创建dataTable
IFeatureCursor featureCursor = pFeatureClass.Search(null, false);
IFeature feature = featureCursor.NextFeature();
DataTable dt = NewDataTable();
string value = null;
while (feature != null)
{
//新建行
DataRow dr = dt.NewRow();
//从shp属性表中获得city属性
value = feature.get_Value(pFeatureClass.FindField(“City”)).ToString();
//转换为汉字
string strvalue = GetVlue(value);
//赋值
dr[“City”] = strvalue;
value = feature.get_Value(pFeatureClass.FindField(“affcountyN”)).ToString();
dr[“affcountyN”] = Math.Round(TODouble(value), 2);
//datatable添加此行
dt.Rows.Add(dr);
feature = featureCursor.NextFeature();
}
//创建一个wordApplication
WordOper wordOper = new WordOper();
wordOper.OpenAndActive(docResultPath, false, false);
//表格赋值
wordOper.TableValue(dt);
wordOper.Save();
wordOper.Close();
//改变表格列名
dt.Columns[“City”].ColumnName = “地区”;
dt.Columns[“affcountyN”].ColumnName = “直接经济损失(万元) “;
//另存表格
ExcelOper excel = new ExcelOper();
Hashtable hashTable = new Hashtable();
hashTable.Add(“直接经济损失分布产品”, dt);
bool issucess = false;
if (excel.WriteExcel(excelPath, hashTable, out message))
{
message = “数据生成成功!”;
issucess = true;
}
else
{
message = “统计数据生成失败!”;
issucess = false;
}
return issucess;
}
catch (Exception ex)
{
message = “失败!”;
return false;
}
}
///
/// 新建datatable
///
///
private DataTable NewDataTable()
{
//新建datatable
DataTable dt = new DataTable();
//新建列
DataColumn dCol = null;
dCol = new DataColumn();
// 指定列名和类型
dCol.ColumnName = “City”;
dCol.DataType = typeof(string);
dt.Columns.Add(dCol);
dCol = new DataColumn();
dCol.ColumnName = “affcountyN”;
dCol.DataType = typeof(double);
// datatable添加列
dt.Columns.Add(dCol);
return dt;
}
///
/// 判断输入输出格式是否正确
///
/// excel路径
/// 文档路径
/// shp文件
///
private bool judgeInOrOutFile(string excelPath, string docPath, string shpPath)
{
//判断excel文件名称是否正确,不正确则运行失败
if (!excelPath.EndsWith(“.xls”) && !excelPath.EndsWith(“.xlsx”))
{
MessageBox.Show(excelPath + “表格名称输入不正确!”);
return false;
}
//判断doc文件名称是否正确,不正确则运行失败
if (!File.Exists(docPath))
{
MessageBox.Show(docPath + “不存在!”);
return false;
}
//判断shp文件是否存在,不存在则运行失败
if (!File.Exists(shpPath))
{
MessageBox.Show(shpPath + “不存在!”);
return false;
}
return true;
}
///
/// string 转换为double
///
/// string 值
///
private double TODouble(string value)
{
try
{
double number = Convert.ToDouble(value);
return number;
}
catch (Exception ex)
{
return 0;
}
}
///
/// 转换为汉字
///
///
///
private string GetVlue(string name)
{
byte[] temp = Encoding.GetEncoding(“ISO8859-1”).GetBytes(name);
string value2 = Encoding.Default.GetString(temp);
return value2;
}
}
word文档代码:
public class WordOper
{
#region 私有成员
private Microsoft.Office.Interop.Word.ApplicationClass _wordApplication;
private Microsoft.Office.Interop.Word.Document _wordDocument;
object missing = System.Reflection.Missing.Value;
#endregion
#region 公开属性
///
/// ApplciationClass
///
public Microsoft.Office.Interop.Word.ApplicationClass WordApplication
{
get
{
return _wordApplication;
}
}
///
/// Document
///
public Microsoft.Office.Interop.Word.Document WordDocument
{
get
{
return _wordDocument;
}
}
#endregion
#region 构造函数
public WordOper()
{
_wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
}
public WordOper(Microsoft.Office.Interop.Word.ApplicationClass wordApplication)
{
_wordApplication = wordApplication;
}
#endregion
#region 基本操作(新建、打开、保存、关闭)
///
/// 打开指定文件
///
/// 文件名(包含路径)
/// 打开后是否只读
/// 打开后是否可视
///
public bool OpenAndActive(string FileName, bool IsReadOnly, bool IsVisibleWin)
{
if (string.IsNullOrEmpty(FileName))
{
return false;
}
try
{
_wordDocument = OpenOneDocument(FileName, missing, IsReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, IsVisibleWin, missing, missing, missing, missing);
_wordDocument.Activate();
return true;
}
catch
{
return false;
}
}
///
/// 关闭
/// Closes the specified document or documents.
///
public void Close()
{
if (_wordDocument != null)
{
//垃圾回收
_wordDocument.Close(ref missing, ref missing, ref missing);
((Microsoft.Office.Interop.Word._Application)_wordApplication).Application.Quit(ref missing, ref missing, ref missing);
GC.Collect();
}
}
///
/// 保存
///
public void Save()
{
if (_wordDocument == null)
{
_wordDocument = _wordApplication.ActiveDocument;
}
_wordDocument.Save();
}
///
/// 打开一个已有文档
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public Microsoft.Office.Interop.Word.Document OpenOneDocument(object FileName, object ConfirmConversions, object ReadOnly,
object AddToRecentFiles, object PasswordDocument, object PasswordTemplate, object Revert,
object WritePasswordDocument, object WritePasswordTemplate, object Format, object Encoding,
object Visible, object OpenAndRepair, object DocumentDirection, object NoEncodingDialog, object XMLTransform)
{
try
{
return _wordApplication.Documents.Open(ref FileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles,
ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,
ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
}
catch
{
return null;
}
}
#endregion
#region 查找、替换
///
/// 在文档中写表格
///
///
///
public bool TableValue(System.Data.DataTable dt)
{
try
{
Table table = _wordDocument.Tables[1];
if (table.Rows.Count == 1)
table.Rows.Add(missing);
for (int col = 1; col
#endregion
}
excel相关代码:
public class ExcelOper
{
private Application _excelApp = null;
public ExcelOper()
{
}
#region 写Excel
///
/// 创建Excel进程
///
///
private Application GetExcelApp()
{
Application excelApp = new Application();
excelApp.Application.Workbooks.Add(true);
_excelApp = excelApp;
return excelApp;
}
///
/// 把DataTable的内容写入Excel
///
/// excel文件的路径
/// key:sheetName,value:DataTable
///
public bool WriteExcel(string strExcelPath, Hashtable htDataTable, out string message)
{
if (htDataTable == null || htDataTable.Count == 0)
{
message = “数据表为空”;
return false;
}
bool writeRst = false;
try
{
if (_excelApp == null)
{
GetExcelApp();
}
//依次写入Sheet页
int countNum = 1;
foreach (DictionaryEntry de in htDataTable)
{
string sheetName = de.Key.ToString();
System.Data.DataTable dtTable = (System.Data.DataTable)de.Value;
Worksheet excelSheet = null;
if (countNum == 1)
{
excelSheet = (Worksheet)_excelApp.Worksheets[countNum];
}
else
{
excelSheet = (Worksheet)_excelApp.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
excelSheet.Name = sheetName;
bool sheetRst = writeSheet(excelSheet, dtTable);
if (!sheetRst)
{
throw new Exception(sheetName + “创建失败!”);
}
countNum++;
}
//保存
_excelApp.Visible = false;
_excelApp.DisplayAlerts = false;
_excelApp.AlertBeforeOverwriting = false;
_excelApp.ActiveWorkbook.SaveAs(strExcelPath, Type.Missing, null, null, false, false,
XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
message = “数据输出成功!”;
writeRst = true;
}
catch (Exception ex)
{
message = ex.Message;
//LogHelper.Error.Append(ex);
writeRst = false;
}
finally
{
//关闭Excel进程
object missing = System.Reflection.Missing.Value;
_excelApp.ActiveWorkbook.Close(missing, missing, missing);
_excelApp.Quit();
_excelApp = null;
//垃圾回收
GC.Collect();
}
return writeRst;
}
///
/// 写Sheet页
///
///
///
///
private bool writeSheet(Worksheet excelSheet, System.Data.DataTable dtTable)
{
if (excelSheet == null || dtTable == null)
{
return false;
}
//列名
for (int i = 0; i
DataColumn dtColumn = dtTable.Columns[i];
string caption = dtColumn.Caption;
excelSheet.Cells[1, i + 1] = caption;
}
//写入值
for (int i = 0; i
for (int j = 0; j
object objValue = dtTable.Rows[i][j];
excelSheet.Cells[2 + i, j + 1] = objValue;
}
}
excelSheet.Columns.AutoFit();
return true;
}
#endregion
}