TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Carmelo La Monica
When:
6 Jul 2013 8:06 AM
Last revision by
Carmelo La Monica
When:
5 Aug 2013 2:36 PM
Revisions:
3
Comments:
0
Options
Subscribe to Article (RSS)
Share this
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
VB NET Eseguire la somma dei dati di una colonna su foglio Excel da Vb Net.(it-IT)
VB NET Eseguire la somma dei dati di una colonna su foglio Excel da Vb Net.(it-IT)
Article
History
VB NET Eseguire la somma dei dati di una colonna su foglio Excel da Vb Net.(it-IT)
A volte capita la necessità di dover eseguire dei calcoli con dei dati presi da altri file , in questo esempio vedremo come poter eseguire la somma dei dati di una colonna da un file excel.
Prendiamo in considerazione questo esempio di codice.
Private Sub Button1Click(sender As Object, e As EventArgs) Handles btnCalcolaSomma.Click
Const query As String = "SELECT SUM(TOTAL) FROM [Foglio1$]"
Using myConnection As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; " & _
"data source=E:\demo.XLSx; " & _
"Extended Properties=Excel 12.0;")
myConnection.Open()
Using myCommand As New OleDbCommand(query, myConnection)
Dim result = myCommand.ExecuteScalar()
If Not result.Equals(DBNull.Value) Then
TextBox1.Text = result.ToString()
End If
End Using
End Using
End Sub
Prima cosa , creiamo una query Select con il nome della colonna del file excel tra parentesi tonde e il nome del foglio presente nel file excel su cui andare a prelevare le informazioni , assicurarsi che entrambi quindi nome colonna e nome foglio siano uguali a quelli contenuti nel file excel , onde evitare errori a runtime.
Utilizzeremo la classe
OleDbConnection
passando come argomento la stringa di connessione alla fonte dati , in questo esempio E:\demo.xlsx , successivamente la classe
OleDbCommand
, la quale dispone del metodo
ExecuteScalar
, questo metodo restituisce l
a prima colonna della prima riga nel gruppo di risultati, o un riferimento Null se il gruppo di risultati è vuoto.
Poichè il metodo può restituire un valore null e necessario eseguirne un confronto e scartare tutti i valori null ed inoltre per visualizzare il risultato della somma all'interno del controllo TextBox e necessaria una conversione del valore da Object a stringa di caratteri , in quanto il valore restituito dal metodo ExecuteScalar e di tipo Object.
In conclusione supponendo di avere un file chiamato demo.xlsx con all'interno una colonna chiamata TOTAL con 10 righe tutte con valore 1 dobbiamo avere coma risultato della somma 10 come mostrato in figura.
Dopo la somma avremo invece questo risultato nell'applicazione Vb Net.
Carmelo La Monica
,
Microsoft Excel
,
VB.NET
[Edit tags]
Leave a Comment
Please add 5 and 1 and type the answer here:
Post
Wiki - Revision Comment List(Revision Comment)
Wikis - Comment List