我目前正在开发一个应用程序,可以在输入数据时添加到现有的excel电子表格中。简单对吧?我在查找有关工作表格式(边框,创建超链接,背景颜色等)的信息时遇到了问题。我已经搜遍了所有地方,我找到的大部分内容都不适合我。我正在运行Visual Studio Pro 2013.由于某种原因,智能感似乎不适用于我的计算机上的大多数Excel内容,是否有网站或列出功能以及如何访问它们?我引用了参考管理器的COM部分中的Microsoft Excel 15.0对象库以及程序集中的Microsoft Office工具Excel。
这是我的代码:
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Tools.Excel
Imports Microsoft.Office.Core
Imports System.Reflection
Imports System.IO
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
If jobnumtext.Text.Length > 2 And provincetext.Text.Length > 0 And contractnumbertext.Text.Length > 2 And clienttext.Text.Length > 0 Then
thisdate = DateTime.Now
thismonth = Month(thisdate)
name = MonthName(thismonth, True)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open("\\IP Address\qa_qc\003. Quality Management\005. Project Management\Project Logs\Job Set Up\Job Set Up Log_Current.xlsx")
oSheet = oBook.Worksheets(name.ToString())
Dim sheet As Worksheet = oExcel.ThisWorkbook.Worksheets(0)
Try
With oSheet
If oExcel.WorksheetFunction.CountA(.Cells) <> 0 Then
lrow = .Cells.Find(What:="*",
After:=.Range("A1"),
LookAt:=Excel.XlLookAt.xlPart,
LookIn:=Excel.XlFindLookIn.xlFormulas,
SearchOrder:=Excel.XlSearchOrder.xlByRows,
SearchDirection:=Excel.XlSearchDirection.xlPrevious,
MatchCase:=False).Row
Else
lrow = 1
End If
End With
oSheet.Range("A" & lrow + 1).Value = dateissued.Text
oSheet.Range("B" & lrow + 1).Value = contractnumbertext.Text
oSheet.Range("C" & lrow + 1).Value = clienttext.Text
oSheet.Range("D" & lrow + 1).Value = provincetext.Text
oSheet.Range("E" & lrow + 1).Value = locationtext.Text
oSheet.Range("F" & lrow + 1).Value = attentiontext.Text
oSheet.Range("G" & lrow + 1).Value = foremantext.Text
oSheet.Range("H" & lrow + 1).Value = peopletext.Text
oSheet.Range("I" & lrow + 1).Value = setupbytext.Text
oSheet.Range("J" & lrow + 1).Value = startdate.Text
oSheet.Range("K" & lrow + 1).Value = enddate.Text
oSheet.Range("L" & lrow + 1).Value = Pipeline.ToString()
oSheet.Range("M" & lrow + 1).Value = facility.ToString()
oSheet.Range("N" & lrow + 1).Value = hourly.ToString()
oSheet.Range("O" & lrow + 1).Value = holdback.ToString() & " " & holdbackstext.Text & b.ToString()
oSheet.Range("P" & lrow + 1).Value = Discounts.ToString() & " " & discountstext.Text & dis.ToString()
oSheet.Range("Q" & lrow + 1).Value = LOA.ToString() & " " & lo.ToString() & loatext.Text
oSheet.Range("R" & lrow + 1).Value = RateSchedule.ToString() & " " & rateschedtext.Text
oSheet.Range("S" & lrow + 1).Value = WPS.ToString() & " " & wpstext.Text
oSheet.Range("T" & lrow + 1).Value = projectdesctext.Text
oSheet.Range("U" & lrow + 1).Value = filesname.ToString()
oSheet.Columns("A:T").WrapText = True
oSheet.Columns("A:T").AutoFit()
oExcel.ActiveSheet.Columns("A:A").ColumnWidth = 10
oExcel.ActiveSheet.Columns("B:B").ColumnWidth = 17
oExcel.ActiveSheet.Columns("C:C").ColumnWidth = 20
oExcel.ActiveSheet.Columns("D:D").ColumnWidth = 17
oExcel.ActiveSheet.Columns("E:E").ColumnWidth = 16
oExcel.ActiveSheet.Columns("F:F").ColumnWidth = 12
oExcel.ActiveSheet.Columns("G:G").ColumnWidth = 12
oExcel.ActiveSheet.Columns("H:H").ColumnWidth = 10
oExcel.ActiveSheet.Columns("I:I").ColumnWidth = 12
oExcel.ActiveSheet.Columns("J:J").ColumnWidth = 10
oExcel.ActiveSheet.Columns("K:K").ColumnWidth = 10
oExcel.ActiveSheet.Columns("L:L").ColumnWidth = 8
oExcel.ActiveSheet.Columns("M:M").ColumnWidth = 8
oExcel.ActiveSheet.Columns("N:N").ColumnWidth = 15
oExcel.ActiveSheet.Columns("O:O").ColumnWidth = 15
oExcel.ActiveSheet.Columns("P:P").ColumnWidth = 15
oExcel.ActiveSheet.Columns("Q:Q").ColumnWidth = 15
oExcel.ActiveSheet.Columns("R:R").ColumnWidth = 15
oExcel.ActiveSheet.Columns("S:S").ColumnWidth = 20
oExcel.ActiveSheet.Columns("T:T").ColumnWidth = 80
oExcel.ActiveSheet.Columns("U:U").ColumnWidth = 20
oBook.Save()
oBook.Close()
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
Catch ex As Exception
MsgBox(ex.ToString())
oExcel.Quit()
GC.Collect()
End Try
Else : MessageBox.Show("Sorry you must input all information to Continue!", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
我想创建一个指向“U”列的超链接,绝对不知道如何... 另外,我想为文档中的每个新条目添加边框。
我不确定excel中对库的引用是如何工作的,任何信息帮助或方向都会非常感激!
谢谢大家!
答案 0 :(得分:1)
教导一个人钓鱼......
由于您具有创建对工作表和内容的引用的基础知识,因此这很容易。我所做的是在MS Excel中记录一个宏,看看VBA是什么。然后在我的VB代码中做同样的事情。使用这个概念,你可以做任何你想做的事情,而不必在网上搜索你甚至不知道的关键词。
答案 1 :(得分:0)
这些方面的东西应该可以解决问题。我刚刚在我的PC上运行VS2013测试了这个。 这将在列E中将超链接添加到同一行中的列U.
Private Sub Sheet1_Change( Target As Microsoft.Office.Interop.Excel.Range) Handles Me.Change
Dim activeSheet As Excel.Worksheet = CType(Me.Application.ActiveWorkbook.Sheets(1), Excel.Worksheet)
Dim jobnum As Int32 = CType(Range("A" + Target.Row.ToString()).Value, Int32)
If (jobnum > 1)
Application.EnableEvents = False
activeSheet.Hyperlinks.Add(activeSheet.Range("E" + Target.Row.ToString()), "", "U" + Target.Row.ToString())
Application.EnableEvents = True
End If
End Sub
答案 2 :(得分:-1)
很抱歉发布此答案,因为它太大而无法放入评论部分 现在我得到了:
System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception..... at Microsoft.Office.Interop.Excel.Hyperlinks.Add(Object Anchor, String Address, Object SubAddress, Object SreenTip, Object TextToDisplay)
我只想澄清一件事......我正在使用:
Dim activesheet as Excel.Worksheet = CType(oExcel.Application.ActiveWorkbook.Sheets(name.ToString()), Excel.Worksheet)
With oExcel.Worksheets(name.ToString())
Dim rng as String = oExcel.Range("U10").Select()
activesheet.Hyperlinks.Add(Anchor:=rng, Address:="File Location")
End With
Range.Select()
方法返回True或False,这对我来说似乎不正确......
我尝试了Excel宏录音机,这就是它给我的原因:
Range("U1").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection,
Address:="File Location",
TextToDisplay:=_
现在我可以指点一下这个方法吗?要显示Intellisense Activesheet
我必须使用oExcel.ActiveSheet
这是相同的吗?我之前在代码中分配了oExcel as Excel.Application
。我为不理解这一点而道歉,因为某些原因我真的很困惑......