Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim xlApp As Object = Nothing
Dim xlBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlSheets As Microsoft.Office.Interop.Excel.Sheets = Nothing
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim myrange As Microsoft.Office.Interop.Excel.Range = Nothing
' Dim strFilePath As String = "C:\Users\18608\Desktop\text.xlsx"
xlApp = New Microsoft.Office.Interop.Excel.Application()
xlBooks = xlApp.Workbooks
xlBook = xlBooks.Open("C:\Users\18608\Desktop\text.xlsx")
xlSheets = xlBook.Worksheets
xlSheet = xlSheets("Sheet1") '这里是你要读入数据的那个sheet页
TextBox2.Text = xlSheet.UsedRange.Columns.Count '获取Excel有效区域列数
rows1 = xlSheet.UsedRange.Rows.Count '获取Excel有效区域的行数
TextBox1.Text = xlSheets.Application.WorksheetFunction.Sum(xlSheet.Range("c1:c1000")) '利用sum函数 求和
xlBook.Close(True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
End Sub
条件求和
https://zhidao.baidu.com/question/429596474.html
1、示例代码:
Sub test1()
Dim x As Long
x = WorksheetFunction.SumIf(Range(“A2:A9”), Range(“C2”).Value, Range(“B2:B9”))
Range(“D2”).Value = x
End Sub
2、如下图:
3、以上代码的作用是:调用工作表函数SUMIF,求A2:A9范围内符合C2单元格条件,对应的B2:B9范围内数值之和,结果输出到D2单元格。
jshe = xlSheets.Application.WorksheetFunction.SumIf(xlSheet.Range("g2:g" & rows1), xlSheet.Range("h2"), xlSheet.Range("c2:c" & rows1)) '求和
TextBox1.Text = xlSheet.Range("h2").Value
MsgBox(jshe)