本文主要实现两部分内容:
1、实现WGS84坐标到高德火星坐标的转换;中间设计调取高德提供的WEb 坐标转换API,将返回的json转为字符串,然后解析字符串
2、实现读取EXCEl的内容并且写EXCEl。实现读写!
这里添加一下高德坐标转换的页面
http://lbs.amap.com/api/webservice/guide/api/
一下是源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace 高德坐标转换
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//string url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key=07c9d2e7c963daed09de8ca3e43bcc7c&locations=116.481499,39.990475&coordsys=gps";
string a = "116.481499";
string b = "39.990475";
MessageBox.Show(Tran(a,b)[0].ToString());
MessageBox.Show(Tran(a, b)[1].ToString());
}
private string[] Tran(string x, string y)
{
string url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key=07c9d2e7c963daed09de8ca3e43bcc7c&locations="+x+","+y+"&coordsys=gps";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
string responseText = sr.ReadToEnd();
sr.Close();
sr.Dispose();
responseStream.Close();
string jsonData = responseText;
int index = jsonData.IndexOf("locations");
string ext = jsonData.Substring(index+12);
//MessageBox.Show(ext);
int index2 = ext.IndexOf("\"");
string ext2 = ext.Substring(0, index2);
// MessageBox.Show(ext2);
int index3=ext2.IndexOf(",");
string ext3=ext2.Substring(0,index3);
string ext4=ext2.Substring(index3+1);
string[] arrayXY = new string[2];
arrayXY[0] = ext3;
arrayXY[1] = ext4;
return arrayXY;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter="excel文件|*.xlsx";
if (openFileDialog.ShowDialog()==DialogResult.OK)
{
string Path = openFileDialog.FileName;
#region 用数据集的方式打开excel
//string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此連接可以操作.xls與.xlsx文件
//OleDbConnection conn = new OleDbConnection(strConn);
//conn.Open();
//string str_sql = "select * from [Sheet1$]";
//OleDbDataAdapter oda = new OleDbDataAdapter(str_sql, conn);
//DataSet ds = new DataSet();
//oda.Fill(ds);
#endregion
//打开这个excel文件
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
object missing = Missing.Value;
// 打开模板文件,得到WorkBook对象
Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(Path, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
// 得到WorkSheet对象
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets.get_Item(1);
//重命名
//将Excel显示出来
excel.Visible = false;
int iRowCount = workSheet.UsedRange.Rows.Count;
int iColCount = workSheet.UsedRange.Columns.Count;
//循环处理坐标转换
string xx = "";
string yy = "";
for (int i = 2; i <= iRowCount; i++)
{
string x = ((Range)workSheet.Cells[i, 1]).Text;
string y = ((Range)workSheet.Cells[i, 2]).Text;
xx = Tran(x, y)[0];
yy = Tran(x, y)[1];
workSheet.Cells[i, 3] = xx;
workSheet.Cells[i, 4] = yy;
}
//保存修改
workBook.Save();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
}
MessageBox.Show("ok ");
}
根據Excel物理路徑獲取Excel文件中所有表名
//public static String[] GetExcelSheetNames(string excelFile)
//{
// OleDbConnection objConn = null;
// System.Data.DataTable dt = null;
// try
// {
// //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此連接只能操作Excel2007之前(.xls)文件
// string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此連接可以操作.xls與.xlsx文件
// objConn = new OleDbConnection(strConn);
// objConn.Open();
// dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// if (dt == null)
// {
// return null;
// }
// String[] excelSheets = new String[dt.Rows.Count];
// int i = 0;
// foreach (DataRow row in dt.Rows)
// {
// excelSheets[i] = row["TABLE_NAME"].ToString();
// i++;
// }
// return excelSheets;
// }
// catch
// {
// return null;
// }
// finally
// {
// if (objConn != null)
// {
// objConn.Close();
// objConn.Dispose();
// }
// if (dt != null)
// {
// dt.Dispose();
// }
// }
//}
}
}