经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#调用js库的方法
来源:cnblogs  作者:0611163  时间:2023/1/18 8:43:31  对本文有异议

前言

用.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中的方法实现功能,文件内容如下:

  1. function calc(lng, lat, polygonStr) {
  2. var point = turf.point([lng, lat]);
  3. var polygonPoints = JSON.parse(polygonStr);
  4. var polygon = turf.polygon(polygonPoints);
  5. var bl = turf.booleanPointInPolygon(point, polygon);
  6. return bl;
  7. }
  8. function correct(lng, lat) {
  9. var newPoint = new CoordConvertor().gcj02_To_gps84(lng, lat);
  10. return newPoint;
  11. }

创建V8ScriptEngine对象

  1. private V8ScriptEngine _engine = new V8ScriptEngine();

通过js引擎加载js文件

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

  1. _engine.AddHostType("Console", typeof(Console));
  2. string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v6.5.0.min.js";
  3. string js;
  4. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  5. {
  6. byte[] bArr = new byte[fs.Length];
  7. await fs.ReadAsync(bArr, 0, bArr.Length);
  8. js = ASCIIEncoding.UTF8.GetString(bArr);
  9. }
  10. _engine.Execute(js);
  11. fileName = AppDomain.CurrentDomain.BaseDirectory + "calc.js";
  12. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  13. {
  14. byte[] bArr = new byte[fs.Length];
  15. await fs.ReadAsync(bArr, 0, bArr.Length);
  16. js = ASCIIEncoding.UTF8.GetString(bArr);
  17. }
  18. _engine.Execute(js);
  19. fileName = AppDomain.CurrentDomain.BaseDirectory + "leaflet.mapCorrection.js";
  20. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  21. {
  22. byte[] bArr = new byte[fs.Length];
  23. await fs.ReadAsync(bArr, 0, bArr.Length);
  24. js = ASCIIEncoding.UTF8.GetString(bArr);
  25. }
  26. _engine.Execute(js);

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

  1. double lng = Convert.ToDouble(lnglat[0]);
  2. double lat = Convert.ToDouble(lnglat[1]);
  3. //坐标纠偏
  4. dynamic newPoint = _engine.Invoke("correct", new object[] { lng, lat });
  5. lng = newPoint.lng;
  6. lat = newPoint.lat;

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

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

程序开发完成后发布


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

Form1.cs完整代码如下:

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

  1. using Models;
  2. using Newtonsoft.Json;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Microsoft.ClearScript.JavaScript;
  7. using Microsoft.ClearScript.V8;
  8. using NPOI.HSSF.UserModel;
  9. using NPOI.XSSF.UserModel;
  10. using NPOI.SS.UserModel;
  11. using System.Reflection;
  12. using System.Windows.Forms;
  13. using NPOI.Util;
  14. namespace 点位
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private Regions _regions;
  19. private List<CameraInfo> _cameraList = new List<CameraInfo>();
  20. private V8ScriptEngine _engine = new V8ScriptEngine();
  21. private string _selectedRegionPoints;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private async void Form1_Load(object sender, EventArgs e)
  27. {
  28. //通过js引擎加载js文件
  29. _engine.AddHostType("Console", typeof(Console));
  30. string fileName = AppDomain.CurrentDomain.BaseDirectory + "turf.v6.5.0.min.js";
  31. string js;
  32. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  33. {
  34. byte[] bArr = new byte[fs.Length];
  35. await fs.ReadAsync(bArr, 0, bArr.Length);
  36. js = ASCIIEncoding.UTF8.GetString(bArr);
  37. }
  38. _engine.Execute(js);
  39. fileName = AppDomain.CurrentDomain.BaseDirectory + "calc.js";
  40. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  41. {
  42. byte[] bArr = new byte[fs.Length];
  43. await fs.ReadAsync(bArr, 0, bArr.Length);
  44. js = ASCIIEncoding.UTF8.GetString(bArr);
  45. }
  46. _engine.Execute(js);
  47. fileName = AppDomain.CurrentDomain.BaseDirectory + "leaflet.mapCorrection.js";
  48. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  49. {
  50. byte[] bArr = new byte[fs.Length];
  51. await fs.ReadAsync(bArr, 0, bArr.Length);
  52. js = ASCIIEncoding.UTF8.GetString(bArr);
  53. }
  54. _engine.Execute(js);
  55. //行政区划下拉列表初始化
  56. fileName = AppDomain.CurrentDomain.BaseDirectory + "安徽.json";
  57. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  58. {
  59. byte[] bArr = new byte[fs.Length];
  60. await fs.ReadAsync(bArr, 0, bArr.Length);
  61. string json = ASCIIEncoding.UTF8.GetString(bArr);
  62. _regions = JsonConvert.DeserializeObject<Regions>(json);
  63. }
  64. List<Records> citys = _regions.RECORDS.ToList().FindAll(a => a.civilcode.Length == 4);
  65. cbxCity.DataSource = citys;
  66. cbxCity.DisplayMember = "civilname";
  67. cbxCity.ValueMember = "civilcode";
  68. }
  69. private void button1_Click(object sender, EventArgs e)
  70. {
  71. openFileDialog1.Title = "选择要处理的Excel文件";
  72. openFileDialog1.Filter = "Excel文件(*.xlsx)|*.xlsx";
  73. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  74. {
  75. using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  76. {
  77. }
  78. }
  79. }
  80. private void cbxCity_SelectedIndexChanged(object sender, EventArgs e)
  81. {
  82. Records record = cbxCity.SelectedItem as Records;
  83. List<Records> citys = _regions.RECORDS.ToList().FindAll(a => a.civilcode.Length > 4 && a.civilcode.Substring(0, 4) == record.civilcode);
  84. citys.Insert(0, new Records() { civilcode = null, civilname = "==请选择==" });
  85. cbxCounty.DataSource = citys;
  86. cbxCounty.DisplayMember = "civilname";
  87. cbxCounty.ValueMember = "civilcode";
  88. }
  89. private void cbxCounty_SelectedIndexChanged(object sender, EventArgs e)
  90. {
  91. Records record = cbxCounty.SelectedItem as Records;
  92. if (record.civilcode == null)
  93. {
  94. record = cbxCity.SelectedItem as Records;
  95. }
  96. Regex regex = new Regex(@"^POLYGON\((\(.*\),?)*\)$");
  97. var mc = regex.Matches(record.polygongeo);
  98. StringBuilder sb = new StringBuilder();
  99. foreach (Match m in mc)
  100. {
  101. string value = m.Groups[1].Value.TrimStart('(').TrimEnd(')');
  102. string[] lnglatArr = value.Split(',');
  103. bool first = true;
  104. if (sb.Length > 0)
  105. {
  106. sb.Append(",");
  107. }
  108. sb.Append("[[");
  109. foreach (string lnglatStr in lnglatArr)
  110. {
  111. string[] lnglat = lnglatStr.Trim().Split(' ');
  112. double lng = Convert.ToDouble(lnglat[0]);
  113. double lat = Convert.ToDouble(lnglat[1]);
  114. //坐标纠偏
  115. dynamic newPoint = _engine.Invoke("correct", new object[] { lng, lat });
  116. lng = newPoint.lng;
  117. lat = newPoint.lat;
  118. if (first)
  119. {
  120. first = false;
  121. sb.AppendFormat($"[{lng}, {lat}]");
  122. }
  123. else
  124. {
  125. sb.AppendFormat($",[{lng}, {lat}]");
  126. }
  127. }
  128. sb.Append("]]");
  129. }
  130. _selectedRegionPoints = sb.ToString();
  131. }
  132. private async void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
  133. {
  134. await Task.Delay(10);
  135. //读取Excel
  136. _cameraList = new List<CameraInfo>();
  137. using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  138. {
  139. XSSFWorkbook workbook = new XSSFWorkbook(fs);
  140. ISheet sheet = workbook.GetSheetAt(0);
  141. for (int i = 1; i <= sheet.LastRowNum; i++)
  142. {
  143. IRow row = sheet.GetRow(i);
  144. CameraInfo cameraInfo = new CameraInfo();
  145. cameraInfo.CameraNo = row.GetCell(1).StringCellValue.Trim();
  146. cameraInfo.City = row.GetCell(2).StringCellValue.Trim();
  147. cameraInfo.County = row.GetCell(3).StringCellValue.Trim();
  148. cameraInfo.CameraName = row.GetCell(4).StringCellValue.Trim();
  149. cameraInfo.Lng = row.GetCell(5).StringCellValue.Trim();
  150. cameraInfo.Lat = row.GetCell(6).StringCellValue.Trim();
  151. cameraInfo.CameraFunType = row.GetCell(7).StringCellValue.Trim();
  152. cameraInfo.Region = row.GetCell(8).StringCellValue.Trim();
  153. cameraInfo.Type = row.GetCell(9).StringCellValue.Trim();
  154. cameraInfo.Status = row.GetCell(10).StringCellValue.Trim();
  155. cameraInfo.Mac = row.GetCell(11).StringCellValue.Trim();
  156. cameraInfo.Ip = row.GetCell(12).StringCellValue.Trim();
  157. _cameraList.Add(cameraInfo);
  158. }
  159. }
  160. //过滤数据
  161. _cameraList = _cameraList.FindAll(cameraInfo =>
  162. {
  163. if (!string.IsNullOrWhiteSpace(cameraInfo.Lng) && !string.IsNullOrWhiteSpace(cameraInfo.Lat))
  164. {
  165. double lng = Convert.ToDouble(cameraInfo.Lng);
  166. double lat = Convert.ToDouble(cameraInfo.Lat);
  167. bool bl = (bool)_engine.Invoke("calc", new object[] { lng, lat, _selectedRegionPoints });
  168. if (bl) //区域内
  169. {
  170. return false;
  171. }
  172. else //区域外
  173. {
  174. return true;
  175. }
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. });
  182. saveFileDialog1.Title = "选择处理结果要保存的位置及文件名";
  183. saveFileDialog1.Filter = "Excel文件(*.xlsx)|*.xlsx";
  184. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  185. {
  186. if (File.Exists(saveFileDialog1.FileName))
  187. {
  188. File.Delete(saveFileDialog1.FileName);
  189. }
  190. string template = AppDomain.CurrentDomain.BaseDirectory + "点位模板.xlsx";
  191. XSSFWorkbook workbook;
  192. using (FileStream fs = new FileStream(template, FileMode.Open, FileAccess.Read, FileShare.Read))
  193. {
  194. workbook = new XSSFWorkbook(fs);
  195. using (FileStream fs2 = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  196. {
  197. ISheet sheet = workbook.GetSheetAt(0);
  198. sheet.RemoveRow(sheet.GetRow(1));
  199. sheet.RemoveRow(sheet.GetRow(2));
  200. Dictionary<int, ICellStyle> cellStyles = GetCellStyles(sheet);
  201. int i = 1;
  202. foreach (CameraInfo cameraInfo in _cameraList)
  203. {
  204. IRow row = sheet.CreateRow(i);
  205. ICell cell1 = row.CreateCell(1, CellType.String);
  206. ICell cell2 = row.CreateCell(2, CellType.String);
  207. ICell cell3 = row.CreateCell(3, CellType.String);
  208. ICell cell4 = row.CreateCell(4, CellType.String);
  209. ICell cell5 = row.CreateCell(5, CellType.String);
  210. ICell cell6 = row.CreateCell(6, CellType.String);
  211. ICell cell7 = row.CreateCell(7, CellType.String);
  212. ICell cell8 = row.CreateCell(8, CellType.String);
  213. ICell cell9 = row.CreateCell(9, CellType.String);
  214. ICell cell10 = row.CreateCell(10, CellType.String);
  215. ICell cell11 = row.CreateCell(11, CellType.String);
  216. ICell cell12 = row.CreateCell(12, CellType.String);
  217. SetCellStyles(row, cellStyles);
  218. cell1.SetCellValue(cameraInfo.CameraNo);
  219. cell2.SetCellValue(cameraInfo.City);
  220. cell3.SetCellValue(cameraInfo.County);
  221. cell4.SetCellValue(cameraInfo.CameraName);
  222. cell5.SetCellValue(cameraInfo.Lng);
  223. cell6.SetCellValue(cameraInfo.Lat);
  224. cell7.SetCellValue(cameraInfo.CameraFunType);
  225. cell8.SetCellValue(cameraInfo.Region);
  226. cell9.SetCellValue(cameraInfo.Type);
  227. cell10.SetCellValue(cameraInfo.Status);
  228. cell11.SetCellValue(cameraInfo.Mac);
  229. cell12.SetCellValue(cameraInfo.Ip);
  230. i++;
  231. }
  232. workbook.Write(fs2);
  233. }
  234. MessageBox.Show("完成");
  235. }
  236. }
  237. }
  238. private Dictionary<int, ICellStyle> GetCellStyles(ISheet sheet)
  239. {
  240. var styleRow = sheet.GetRow(5);
  241. Dictionary<int, ICellStyle> result = new Dictionary<int, ICellStyle>();
  242. for (int i = 1; i <= 12; i++)
  243. {
  244. result.Add(i, styleRow.GetCell(i).CellStyle);
  245. }
  246. return result;
  247. }
  248. private void SetCellStyles(IRow row, Dictionary<int, ICellStyle> styles)
  249. {
  250. for (int i = 1; i <= 12; i++)
  251. {
  252. row.GetCell(i).CellStyle = styles[i];
  253. }
  254. }
  255. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  256. {
  257. _engine.Dispose();
  258. }
  259. private void button2_Click(object sender, EventArgs e)
  260. {
  261. folderBrowserDialog1.Description = "选择模板文件保存位置";
  262. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  263. {
  264. string template = AppDomain.CurrentDomain.BaseDirectory + "点位模板.xlsx";
  265. string filePath = Path.Combine(folderBrowserDialog1.SelectedPath, "点位模板.xlsx");
  266. if (File.Exists(filePath))
  267. {
  268. if (MessageBox.Show("模板文件已存在,是否覆盖?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
  269. {
  270. File.Copy(template, filePath, true);
  271. MessageBox.Show("下载完成");
  272. }
  273. }
  274. else
  275. {
  276. File.Copy(template, filePath, true);
  277. MessageBox.Show("下载完成");
  278. }
  279. }
  280. }
  281. }
  282. }

原文链接:https://www.cnblogs.com/s0611163/p/17056909.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号