protected override void OnClick()
{
MessageBox.Show("1");
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string excelFilePath = openFileDialog.FileName;
string mxdPath = ReadExcelCell(excelFilePath, "A1");
MessageBox.Show(mxdPath);
}
}
private string ReadExcelCell(string filePath, string cell)
{
string value = null;
string extension = System.IO.Path.GetExtension(filePath).ToLower();
string connStr = "";
if (extension == ".xls")
{
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;'";
}
else if (extension == ".xlsx")
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;'";
}
try
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand($"SELECT * FROM [Sheet1${cell}:{cell}] ", conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
value = dr[0].ToString();
}
dr.Close();
}
}
catch (Exception ex)
{
MessageBox.Show($"读取 Excel 文件失败:{ex.Message}");
}
return value;
}
protected override void OnUpdate()
{
}
}
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; // for xlsx
using NPOI.HSSF.UserModel; // for xls
namespace ArcMapAddin2
{
public class ArcGISAddin1 : ESRI.ArcGIS.Desktop.AddIns.Button
{
public ArcGISAddin1()
{
}
protected override void OnClick()
{
MessageBox.Show("1");
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string excelFilePath = openFileDialog.FileName;
// 获取用户输入的工作表名称、列名和行范围
string sheetName = Microsoft.VisualBasic.Interaction.InputBox("请输入工作表名称:", "工作表名称", "Sheet1");
string columnName = Microsoft.VisualBasic.Interaction.InputBox("请输入列名:", "列名", "Name");
string startRowStr = Microsoft.VisualBasic.Interaction.InputBox("请输入起始行号:", "起始行号", "1");
string endRowStr = Microsoft.VisualBasic.Interaction.InputBox("请输入结束行号:", "结束行号", "1");
int startRow = int.Parse(startRowStr);
int endRow = int.Parse(endRowStr);
string mxdPath = ReadExcelCell(excelFilePath, sheetName, columnName, startRow, endRow);
MessageBox.Show(mxdPath);
}
}
private string ReadExcelCell(string filePath, string sheetName, string columnName, int startRow, int endRow)
{
string value = null;
try
{
IWorkbook workbook;
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (Path.GetExtension(filePath).ToLower() == ".xls")
{
workbook = new HSSFWorkbook(file);
}
else
{
workbook = new XSSFWorkbook(file);
}
}
ISheet sheet = workbook.GetSheet(sheetName);
if (sheet != null)
{
// 查找列名对应的列索引
IRow headerRow = sheet.GetRow(0); // 假设列名在第一行
int columnIndex = -1;
for (int col = 0; col < headerRow.LastCellNum; col++)
{
var cellValue = headerRow.GetCell(col)?.ToString();
if (cellValue == columnName)
{
columnIndex = col;
break;
}
}
if (columnIndex == -1)
{
throw new Exception($"列名 '{columnName}' 未找到。");
}
// 读取指定行范围的数据
StringBuilder sb = new StringBuilder();
for (int row = startRow - 1; row < endRow; row++) // NPOI使用0-based索引
{
IRow dataRow = sheet.GetRow(row);
if (dataRow != null)
{
var cell = dataRow.GetCell(columnIndex);
var cellValue = cell?.ToString() ?? string.Empty;
sb.AppendLine(cellValue);
}
}
value = sb.ToString();
}
else
{
throw new Exception($"工作表 '{sheetName}' 未找到。");
}
}
catch (Exception ex)
{
MessageBox.Show($"读取 Excel 文件失败:{ex.Message}");
}
return value;
}
protected override void OnUpdate()
{
}
}
}