C#調(diào)用js庫(kù)的方法小結(jié)
前言
用.net6開發(fā)一個(gè)Winform程序,處理Excel文件,并把結(jié)果導(dǎo)出Excel文件。
要用到兩個(gè)算法,一是turf.js庫(kù)的booleanPointInPolygon方法,判斷經(jīng)緯度坐標(biāo)是否在區(qū)域內(nèi);二是經(jīng)緯度糾偏算法,因?yàn)閷?duì)方給的區(qū)域坐標(biāo)集合有偏移,需要糾偏。
這兩個(gè)算法,網(wǎng)上找C#的實(shí)現(xiàn),一是不好找;二是找來(lái)的不信任,我還要測(cè)試以確保沒(méi)有問(wèn)題。我之前做電子地圖使用過(guò)turf.js庫(kù)和js版本的糾偏算法,比較信任,確定沒(méi)有問(wèn)題。
所以我就打算通過(guò)C#調(diào)用js庫(kù)的方法,來(lái)實(shí)現(xiàn)數(shù)據(jù)處理。
安裝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放入工程中,右擊屬性設(shè)置復(fù)制到輸出目錄:如果較新則復(fù)制。
calc.js通過(guò)調(diào)用leaflet.mapCorrection.js和turf.v6.5.0.min.js中的方法實(shí)現(xiàn)功能,文件內(nèi)容如下:
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().gcj02_To_gps84(lng, lat);
return newPoint;
}創(chuàng)建V8ScriptEngine對(duì)象
private V8ScriptEngine _engine = new V8ScriptEngine();
通過(guò)js引擎加載js文件
在Form1_Load方法中添加如下代碼:
_engine.AddHostType("Console", typeof(Console));
string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v6.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.GetString(bArr);
}
_engine.Execute(js);C#調(diào)用js方法實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏
double lng = Convert.ToDouble(lnglat[0]);
double lat = Convert.ToDouble(lnglat[1]);
//坐標(biāo)糾偏
dynamic newPoint = _engine.Invoke("correct", new object[] { lng, lat });
lng = newPoint.lng;
lat = newPoint.lat;C#調(diào)用js方法判斷經(jīng)緯度點(diǎn)位是否在多邊形內(nèi)
//_selectedRegionPoints是多邊形坐標(biāo)點(diǎn)位集合json字符串
bool bl = (bool)_engine.Invoke("calc", new object[] { lng, lat, _selectedRegionPoints });程序開發(fā)完成后發(fā)布

發(fā)布后文件夾拷貝到用戶的win10系統(tǒng)中可以直接使用,不需要安裝.net6環(huán)境。我自己的很老的win7 sp1虛擬機(jī)上跑不起來(lái),ClearScriptV8.win-x64.dll無(wú)法加載成功,暫不知道為什么。
Form1.cs完整代碼如下:
當(dāng)時(shí)程序?qū)懙募?,?dāng)然,程序還可以優(yōu)化,不過(guò)沒(méi)必要,要處理的數(shù)據(jù)量不大,功能沒(méi)問(wèn)題就行。
using Models;
using Newtonsoft.Json;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.Reflection;
using System.Windows.Forms;
using NPOI.Util;
namespace 點(diǎn)位
{
public partial class Form1 : Form
{
private Regions _regions;
private List<CameraInfo> _cameraList = new List<CameraInfo>();
private V8ScriptEngine _engine = new V8ScriptEngine();
private string _selectedRegionPoints;
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
//通過(guò)js引擎加載js文件
_engine.AddHostType("Console", typeof(Console));
string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v6.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.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, 0, bArr.Length);
js = ASCIIEncoding.UTF8.GetString(bArr);
}
_engine.Execute(js);
//行政區(qū)劃下拉列表初始化
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, 0, bArr.Length);
string json = ASCIIEncoding.UTF8.GetString(bArr);
_regions = JsonConvert.DeserializeObject<Regions>(json);
}
List<Records> citys = _regions.RECORDS.ToList().FindAll(a => a.civilcode.Length == 4);
cbxCity.DataSource = citys;
cbxCity.DisplayMember = "civilname";
cbxCity.ValueMember = "civilcode";
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "選擇要處理的Excel文件";
openFileDialog1.Filter = "Excel文件(*.xlsx)|*.xlsx";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = new FileStream(openFileDialog1.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 > 4 && a.civilcode.Substring(0, 4) == record.civilcode);
citys.Insert(0, new Records() { civilcode = null, civilname = "==請(qǐng)選擇==" });
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[1].Value.TrimStart('(').TrimEnd(')');
string[] lnglatArr = value.Split(',');
bool first = true;
if (sb.Length > 0)
{
sb.Append(",");
}
sb.Append("[[");
foreach (string lnglatStr in lnglatArr)
{
string[] lnglat = lnglatStr.Trim().Split(' ');
double lng = Convert.ToDouble(lnglat[0]);
double lat = Convert.ToDouble(lnglat[1]);
//坐標(biāo)糾偏
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 openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
await Task.Delay(10);
//讀取Excel
_cameraList = new List<CameraInfo>();
using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XSSFWorkbook workbook = new XSSFWorkbook(fs);
ISheet sheet = workbook.GetSheetAt(0);
for (int i = 1; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
CameraInfo cameraInfo = new CameraInfo();
cameraInfo.CameraNo = row.GetCell(1).StringCellValue.Trim();
cameraInfo.City = row.GetCell(2).StringCellValue.Trim();
cameraInfo.County = row.GetCell(3).StringCellValue.Trim();
cameraInfo.CameraName = row.GetCell(4).StringCellValue.Trim();
cameraInfo.Lng = row.GetCell(5).StringCellValue.Trim();
cameraInfo.Lat = row.GetCell(6).StringCellValue.Trim();
cameraInfo.CameraFunType = row.GetCell(7).StringCellValue.Trim();
cameraInfo.Region = row.GetCell(8).StringCellValue.Trim();
cameraInfo.Type = row.GetCell(9).StringCellValue.Trim();
cameraInfo.Status = row.GetCell(10).StringCellValue.Trim();
cameraInfo.Mac = row.GetCell(11).StringCellValue.Trim();
cameraInfo.Ip = row.GetCell(12).StringCellValue.Trim();
_cameraList.Add(cameraInfo);
}
}
//過(guò)濾數(shù)據(jù)
_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) //區(qū)域內(nèi)
{
return false;
}
else //區(qū)域外
{
return true;
}
}
else
{
return false;
}
});
saveFileDialog1.Title = "選擇處理結(jié)果要保存的位置及文件名";
saveFileDialog1.Filter = "Excel文件(*.xlsx)|*.xlsx";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (File.Exists(saveFileDialog1.FileName))
{
File.Delete(saveFileDialog1.FileName);
}
string template = AppDomain.CurrentDomain.BaseDirectory + "點(diǎn)位模板.xlsx";
XSSFWorkbook workbook;
using (FileStream fs = new FileStream(template, FileMode.Open, FileAccess.Read, FileShare.Read))
{
workbook = new XSSFWorkbook(fs);
using (FileStream fs2 = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
ISheet sheet = workbook.GetSheetAt(0);
sheet.RemoveRow(sheet.GetRow(1));
sheet.RemoveRow(sheet.GetRow(2));
Dictionary<int, ICellStyle> cellStyles = GetCellStyles(sheet);
int i = 1;
foreach (CameraInfo cameraInfo in _cameraList)
{
IRow row = sheet.CreateRow(i);
ICell cell1 = row.CreateCell(1, CellType.String);
ICell cell2 = row.CreateCell(2, CellType.String);
ICell cell3 = row.CreateCell(3, CellType.String);
ICell cell4 = row.CreateCell(4, CellType.String);
ICell cell5 = row.CreateCell(5, CellType.String);
ICell cell6 = row.CreateCell(6, CellType.String);
ICell cell7 = row.CreateCell(7, CellType.String);
ICell cell8 = row.CreateCell(8, CellType.String);
ICell cell9 = row.CreateCell(9, CellType.String);
ICell cell10 = row.CreateCell(10, CellType.String);
ICell cell11 = row.CreateCell(11, CellType.String);
ICell cell12 = row.CreateCell(12, CellType.String);
SetCellStyles(row, cellStyles);
cell1.SetCellValue(cameraInfo.CameraNo);
cell2.SetCellValue(cameraInfo.City);
cell3.SetCellValue(cameraInfo.County);
cell4.SetCellValue(cameraInfo.CameraName);
cell5.SetCellValue(cameraInfo.Lng);
cell6.SetCellValue(cameraInfo.Lat);
cell7.SetCellValue(cameraInfo.CameraFunType);
cell8.SetCellValue(cameraInfo.Region);
cell9.SetCellValue(cameraInfo.Type);
cell10.SetCellValue(cameraInfo.Status);
cell11.SetCellValue(cameraInfo.Mac);
cell12.SetCellValue(cameraInfo.Ip);
i++;
}
workbook.Write(fs2);
}
MessageBox.Show("完成");
}
}
}
private Dictionary<int, ICellStyle> GetCellStyles(ISheet sheet)
{
var styleRow = sheet.GetRow(5);
Dictionary<int, ICellStyle> result = new Dictionary<int, ICellStyle>();
for (int i = 1; i <= 12; i++)
{
result.Add(i, styleRow.GetCell(i).CellStyle);
}
return result;
}
private void SetCellStyles(IRow row, Dictionary<int, ICellStyle> styles)
{
for (int i = 1; i <= 12; i++)
{
row.GetCell(i).CellStyle = styles[i];
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_engine.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.Description = "選擇模板文件保存位置";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string template = AppDomain.CurrentDomain.BaseDirectory + "點(diǎn)位模板.xlsx";
string filePath = Path.Combine(folderBrowserDialog1.SelectedPath, "點(diǎn)位模板.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("下載完成");
}
}
}
}
}到此這篇關(guān)于C#調(diào)用js庫(kù)的方法小結(jié)的文章就介紹到這了,更多相關(guān)C#調(diào)用js內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#觀察者模式(Observer Pattern)實(shí)例教程
這篇文章主要介紹了C#觀察者模式(Observer Pattern),主要以一個(gè)實(shí)例的形式講述了C#觀察者模式的實(shí)現(xiàn)過(guò)程,詳細(xì)講述了接口的定義、通知及動(dòng)作的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-09-09
C#中調(diào)用Windows API的技術(shù)要點(diǎn)說(shuō)明
本篇文章主要是對(duì)C#中調(diào)用Windows API的技術(shù)要點(diǎn)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
c#實(shí)現(xiàn)用SQL池,多線程定時(shí)批量執(zhí)行SQL語(yǔ)句的方法
構(gòu)建SQL池,分離業(yè)務(wù)邏輯層和數(shù)據(jù)訪問(wèn)層,讓業(yè)務(wù)邏輯層從低效的數(shù)據(jù)庫(kù)操作解脫,以提高系統(tǒng)整體性能2013-10-10
C#讀取多條數(shù)據(jù)記錄導(dǎo)出到Word之圖片輸出改造
這篇文章主要為大家詳細(xì)介紹了C#讀取多條數(shù)據(jù)記錄并導(dǎo)出到Word標(biāo)簽?zāi)0逯械膱D片輸出問(wèn)題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
C#計(jì)時(shí)器的三種實(shí)現(xiàn)方法
這篇文章主要介紹了C#計(jì)時(shí)器的三種實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
C#如何將List<string>轉(zhuǎn)換為L(zhǎng)ist<double>
這篇文章主要介紹了C#如何將List<string>轉(zhuǎn)換為L(zhǎng)ist<double>問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

