C#调用js库的方法示例代码

.NET
314
0
0
2023-07-23
标签   C#
目录
  • 前言
  • 安装ClearScript
  • 引入js文件
  • 创建V8ScriptEngine对象
  • 通过js引擎加载js文件
  • C#调用js方法实现经纬度坐标纠偏
  • C#调用js方法判断经纬度点位是否在多边形内
  • 程序开发完成后发布
  • Form1.cs完整代码如下:

前言

用.net6开发一个Winform程序,处理Excel文件,并把结果导出Excel文件。

要用到两个算法,一是turf.js库的booleanPointInPolygon方法,判断经纬度坐标是否在区域内;二是经纬度纠偏算法,因为对方给的区域坐标集合有偏移,需要纠偏。

这两个算法,网上找C#的实现,一是不好找;二是找来的不信任,我还要测试以确保没有问题。我之前做电子地图使用过turf.js库和js版本的纠偏算法,比较信任,确定没有问题。

所以我就打算通过C#调用js库的方法,来实现数据处理。

安装ClearScript

ClearScript是微软开源的js引擎,支持windows、linux、mac。

NuGet搜索安装:

Microsoft.ClearScript.Core

Microsoft.ClearScript.V8

Microsoft.ClearScript.V8.Native.win-x64

引入js文件

把leaflet.mapCorrection.js、turf.v6.5.0.min.js和自己写的calc.js放入工程中,右击属性设置复制到输出目录:如果较新则复制。

calc.js通过调用leaflet.mapCorrection.js和turf.v6.5.0.min.js中的方法实现功能,文件内容如下:

function calc(lng, lat, polygonStr) {
    var point = turf.point([lng, lat]);
    var polygonPoints = JSON.parse(polygonStr);
    var polygon = turf.polygon(polygonPoints);

    var bl = turf.booleanPointInPolygon(point, polygon);
    return bl;
}

function correct(lng, lat) {
    var newPoint = new CoordConvertor().gcj_To_gps84(lng, lat);
    return newPoint;
}

创建V8ScriptEngine对象

private VScriptEngine _engine = new V8ScriptEngine();

通过js引擎加载js文件

在Form1_Load方法中添加如下代码:

_engine.AddHostType("Console", typeof(Console));
string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v.5.0.min.js";
string js;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    byte[] bArr = new byte[fs.Length];
    await fs.ReadAsync(bArr,, bArr.Length);
    js = ASCIIEncoding.UTF.GetString(bArr);
}
_engine.Execute(js);
fileName = AppDomain.CurrentDomain.BaseDirectory + "calc.js";
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    byte[] bArr = new byte[fs.Length];
    await fs.ReadAsync(bArr,, bArr.Length);
    js = ASCIIEncoding.UTF.GetString(bArr);
}
_engine.Execute(js);
fileName = AppDomain.CurrentDomain.BaseDirectory + "leaflet.mapCorrection.js";
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    byte[] bArr = new byte[fs.Length];
    await fs.ReadAsync(bArr,, bArr.Length);
    js = ASCIIEncoding.UTF.GetString(bArr);
}
_engine.Execute(js);

C#调用js方法实现经纬度坐标纠偏

double lng = Convert.ToDouble(lnglat[]);
double lat = Convert.ToDouble(lnglat[]);

//坐标纠偏
dynamic newPoint = _engine.Invoke("correct", new object[] { lng, lat });
lng = newPoint.lng;
lat = newPoint.lat;

C#调用js方法判断经纬度点位是否在多边形内

//_selectedRegionPoints是多边形坐标点位集合json字符串
bool bl = (bool)_engine.Invoke("calc", new object[] { lng, lat, _selectedRegionPoints });

程序开发完成后发布

发布后文件夹拷贝到用户的win10系统中可以直接使用,不需要安装.net6环境。我自己的很老的win7 sp1虚拟机上跑不起来,ClearScriptV8.win-x64.dll无法加载成功,暂不知道为什么。

Form1.cs完整代码如下:

当时程序写的急,当然,程序还可以优化,不过没必要,要处理的数据量不大,功能没问题就行。

using Models;
using Newtonsoft.Json;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.Reflection;
using System.Windows.Forms;
using NPOI.Util;

namespace 点位
{
    public partial class Form : Form
    {
        private Regions _regions;
        private List<CameraInfo> _cameraList = new List<CameraInfo>();
        private VScriptEngine _engine = new V8ScriptEngine();
        private string _selectedRegionPoints;

        public Form()
        {
            InitializeComponent();
        }

        private async void Form_Load(object sender, EventArgs e)
        {
            //通过js引擎加载js文件
            _engine.AddHostType("Console", typeof(Console));
            string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v.5.0.min.js";
            string js;
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] bArr = new byte[fs.Length];
                await fs.ReadAsync(bArr,, bArr.Length);
                js = ASCIIEncoding.UTF.GetString(bArr);
            }
            _engine.Execute(js);
            fileName = AppDomain.CurrentDomain.BaseDirectory + "calc.js";
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] bArr = new byte[fs.Length];
                await fs.ReadAsync(bArr,, bArr.Length);
                js = ASCIIEncoding.UTF.GetString(bArr);
            }
            _engine.Execute(js);
            fileName = AppDomain.CurrentDomain.BaseDirectory + "leaflet.mapCorrection.js";
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] bArr = new byte[fs.Length];
                await fs.ReadAsync(bArr,, bArr.Length);
                js = ASCIIEncoding.UTF.GetString(bArr);
            }
            _engine.Execute(js);

            //行政区划下拉列表初始化
            fileName = AppDomain.CurrentDomain.BaseDirectory + "安徽.json";
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] bArr = new byte[fs.Length];
                await fs.ReadAsync(bArr,, bArr.Length);
                string json = ASCIIEncoding.UTF.GetString(bArr);
                _regions = JsonConvert.DeserializeObject<Regions>(json);
            }

            List<Records> citys = _regions.RECORDS.ToList().FindAll(a => a.civilcode.Length ==);
            cbxCity.DataSource = citys;
            cbxCity.DisplayMember = "civilname";
            cbxCity.ValueMember = "civilcode";
        }

        private void button_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "选择要处理的Excel文件";
            openFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {

                }
            }
        }


        private void cbxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            Records record = cbxCity.SelectedItem as Records;
            List<Records> citys = _regions.RECORDS.ToList().FindAll(a => a.civilcode.Length > && a.civilcode.Substring(0, 4) == record.civilcode);
            citys.Insert(, new Records() { civilcode = null, civilname = "==请选择==" });
            cbxCounty.DataSource = citys;
            cbxCounty.DisplayMember = "civilname";
            cbxCounty.ValueMember = "civilcode";
        }

        private void cbxCounty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Records record = cbxCounty.SelectedItem as Records;
            if (record.civilcode == null)
            {
                record = cbxCity.SelectedItem as Records;
            }
            Regex regex = new Regex(@"^POLYGON\((\(.*\),?)*\)$");
            var mc = regex.Matches(record.polygongeo);
            StringBuilder sb = new StringBuilder();
            foreach (Match m in mc)
            {
                string value = m.Groups[].Value.TrimStart('(').TrimEnd(')');
                string[] lnglatArr = value.Split(',');
                bool first = true;
                if (sb.Length >)
                {
                    sb.Append(",");
                }
                sb.Append("[[");
                foreach (string lnglatStr in lnglatArr)
                {
                    string[] lnglat = lnglatStr.Trim().Split(' ');
                    double lng = Convert.ToDouble(lnglat[]);
                    double lat = Convert.ToDouble(lnglat[]);

                    //坐标纠偏
                    dynamic newPoint = _engine.Invoke("correct", new object[] { lng, lat });
                    lng = newPoint.lng;
                    lat = newPoint.lat;

                    if (first)
                    {
                        first = false;
                        sb.AppendFormat($"[{lng}, {lat}]");
                    }
                    else
                    {
                        sb.AppendFormat($",[{lng}, {lat}]");
                    }
                }
                sb.Append("]]");
            }
            _selectedRegionPoints = sb.ToString();
        }

        private async void openFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            await Task.Delay();

            //读取Excel
            _cameraList = new List<CameraInfo>();
            using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                XSSFWorkbook workbook = new XSSFWorkbook(fs);
                ISheet sheet = workbook.GetSheetAt();
                for (int i =; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    CameraInfo cameraInfo = new CameraInfo();
                    cameraInfo.CameraNo = row.GetCell().StringCellValue.Trim();
                    cameraInfo.City = row.GetCell().StringCellValue.Trim();
                    cameraInfo.County = row.GetCell().StringCellValue.Trim();
                    cameraInfo.CameraName = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Lng = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Lat = row.GetCell().StringCellValue.Trim();
                    cameraInfo.CameraFunType = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Region = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Type = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Status = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Mac = row.GetCell().StringCellValue.Trim();
                    cameraInfo.Ip = row.GetCell().StringCellValue.Trim();
                    _cameraList.Add(cameraInfo);
                }
            }

            //过滤数据
            _cameraList = _cameraList.FindAll(cameraInfo =>
            {
                if (!string.IsNullOrWhiteSpace(cameraInfo.Lng) && !string.IsNullOrWhiteSpace(cameraInfo.Lat))
                {
                    double lng = Convert.ToDouble(cameraInfo.Lng);
                    double lat = Convert.ToDouble(cameraInfo.Lat);
                    bool bl = (bool)_engine.Invoke("calc", new object[] { lng, lat, _selectedRegionPoints });
                    if (bl) //区域内
                    {
                        return false;
                    }
                    else //区域外
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            });

            saveFileDialog.Title = "选择处理结果要保存的位置及文件名";
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(saveFileDialog.FileName))
                {
                    File.Delete(saveFileDialog.FileName);
                }
                string template = AppDomain.CurrentDomain.BaseDirectory + "点位模板.xlsx";
                XSSFWorkbook workbook;
                using (FileStream fs = new FileStream(template, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    workbook = new XSSFWorkbook(fs);

                    using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        ISheet sheet = workbook.GetSheetAt();
                        sheet.RemoveRow(sheet.GetRow());
                        sheet.RemoveRow(sheet.GetRow());
                        Dictionary<int, ICellStyle> cellStyles = GetCellStyles(sheet);
                        int i =;
                        foreach (CameraInfo cameraInfo in _cameraList)
                        {
                            IRow row = sheet.CreateRow(i);
                            ICell cell = row.CreateCell(1, CellType.String);
                            ICell cell = row.CreateCell(2, CellType.String);
                            ICell cell = row.CreateCell(3, CellType.String);
                            ICell cell = row.CreateCell(4, CellType.String);
                            ICell cell = row.CreateCell(5, CellType.String);
                            ICell cell = row.CreateCell(6, CellType.String);
                            ICell cell = row.CreateCell(7, CellType.String);
                            ICell cell = row.CreateCell(8, CellType.String);
                            ICell cell = row.CreateCell(9, CellType.String);
                            ICell cell = row.CreateCell(10, CellType.String);
                            ICell cell = row.CreateCell(11, CellType.String);
                            ICell cell = row.CreateCell(12, CellType.String);
                            SetCellStyles(row, cellStyles);
                            cell.SetCellValue(cameraInfo.CameraNo);
                            cell.SetCellValue(cameraInfo.City);
                            cell.SetCellValue(cameraInfo.County);
                            cell.SetCellValue(cameraInfo.CameraName);
                            cell.SetCellValue(cameraInfo.Lng);
                            cell.SetCellValue(cameraInfo.Lat);
                            cell.SetCellValue(cameraInfo.CameraFunType);
                            cell.SetCellValue(cameraInfo.Region);
                            cell.SetCellValue(cameraInfo.Type);
                            cell.SetCellValue(cameraInfo.Status);
                            cell.SetCellValue(cameraInfo.Mac);
                            cell.SetCellValue(cameraInfo.Ip);
                            i++;
                        }
                        workbook.Write(fs);
                    }
                    MessageBox.Show("完成");
                }
            }
        }

        private Dictionary<int, ICellStyle> GetCellStyles(ISheet sheet)
        {
            var styleRow = sheet.GetRow();
            Dictionary<int, ICellStyle> result = new Dictionary<int, ICellStyle>();
            for (int i =; i <= 12; i++)
            {
                result.Add(i, styleRow.GetCell(i).CellStyle);
            }
            return result;
        }

        private void SetCellStyles(IRow row, Dictionary<int, ICellStyle> styles)
        {
            for (int i =; i <= 12; i++)
            {
                row.GetCell(i).CellStyle = styles[i];
            }
        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            _engine.Dispose();
        }

        private void button_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "选择模板文件保存位置";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string template = AppDomain.CurrentDomain.BaseDirectory + "点位模板.xlsx";
                string filePath = Path.Combine(folderBrowserDialog.SelectedPath, "点位模板.xlsx");
                if (File.Exists(filePath))
                {
                    if (MessageBox.Show("模板文件已存在,是否覆盖?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        File.Copy(template, filePath, true);
                        MessageBox.Show("下载完成");
                    }
                }
                else
                {
                    File.Copy(template, filePath, true);
                    MessageBox.Show("下载完成");
                }
            }
        }
    }
}