Lat和long值VB.NET [英] Lat and long value VB.NET

查看:126
本文介绍了Lat和long值VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取值并保存在excel文件中

i try to fetch values and save in excel file

推荐答案

你这是一个非常奇怪的方式。

You are going about this a very strange way.
Dim s1 As String = "" 
...
For Each p1 As PointLatLng In lLines3
    s1 += p1.Lat + "," + p1.Lng
Next



字符串是不可变的。换句话说,它们无法改变。实际上,您正在为该循环的每次迭代获取一个全新的字符串 - 效率不高。如果要构建字符串,请使用 StringBuilder类 [ ^ ]

现在你有一个以逗号分隔的某些值列表,你用一个格式字符串从剪贴板中获取数据?


Strings are immutable. In other words they cannot be changed. You are actually getting a brand new string for every iteration of that loop - not very efficient. If you are going to build strings up then use the StringBuilder Class[^]
So now you have a comma-separated list of some values, which you use a a format string to get data from the clipboard?

Clipboard.GetData(s1)

我认为你真的不想这样做。也许你正试图将s1放入剪贴板...

I don't think you really mean to do that. Perhaps you are trying to put s1 into the clipboard...

Clipboard.SetText(s1)

这导致我你的下一个问题

Which leads me to your next problem

oBook.Worksheets(1).Paste()



我怀疑你认为你将从A,B,C,D,E,F列中获取上面的值。 ..等等你不是。您将在单元格A1中获得以逗号分隔的值列表作为字符串。 (如果你修复了上面的剪贴板问题,那就是你会得到的,否则你会得到你最后复制到剪贴板中的任何东西,如果有的话)。



然后尝试将相同的数据插入同一个单元格(再次!)...


I suspect that you think you are going to get the values from above in columns A,B,C,D,E,F... etc. You are not. You will get a comma-separated list of values as a string in Cell A1. (Well that's what you will get if you fix the Clipboard problem above, otherwise you will get whatever, if anything, you last copied into the Clipboard).

You then attempt to insert the same data into the same cell (again!) ...

oSheet.Range("A1").Value = s1

但是你尚未初始化 oSheet 。你需要先给它一个值才能使用它,例如

But you have not initialised oSheet. You need to give it a value before you can use it e.g.

oSheet = oBook.Worksheets(1)



接下来的两行问题不是那么明显


The problems are not so obvious with the next two lines

oBook.SaveAs("C:\Book1.xls")

在大多数系统中,您必须具有管理员权限才能写入到C:驱动器的根目录。您也不希望根文件夹变得太杂乱......将文件粘贴到文件夹中 - 例如C:\Temp。

您还应该给它一个合理的名称,而不是使用Excel默认名称 - 您不太可能覆盖您可能想要保留的文件,如果它更容易生活,如果你需要自己整理一下。

In most systems you have to have administrator rights to be able to write to the root of the C: drive. You also don't want the root folder to become too cluttered ... stick the file into a folder - e.g. C:\Temp.
You should also give it a sensible name rather than using the Excel default name - you are less likely to overwrite a file you might have wanted to keep and it makes life easier if you need to tidy up after yourself.

oExcel.Quit()



本身没有问题只是像这样退出excel但偶尔会出现错误,表明Excel在关闭时遇到问题。尝试先关闭本书以克服


There isn't a problem per se with just quitting excel like this but occasionally you might get an error suggesting that Excel is having problems closing down. Try closing the book first to overcome that

oBook.Close()





以下代码是我的意思除了我没有处理任何错误处理,检查文件是否已经存在,安全检查值等等。



The code that follows is how I would have done this except that I haven't bothered with any error handling, checking if the file already exists, safety checks on values etc etc.

Dim oExcel As Excel.Application = New Excel.Application()
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet

oBook = oExcel.Workbooks.Add
If (oBook.Worksheets.Count = 0) Then
    oBook.Worksheets.Add()
End If

oSheet = oBook.Worksheets(1)

Dim i As Integer = 0 'NB 0 (zero) is correct, this should not be 1
For Each p1 As PointLatLng In lLines3
    oSheet.Range("A1").Offset(0, i).Value2 = p1.lat
    oSheet.Range("A1").Offset(0, i + 1).Value2 = p1.lng
    i += 2
Next

oBook.SaveAs("C:\Temp\PointLatLng.xls")
oBook.Close()
oExcel.Quit()


这篇关于Lat和long值VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆