Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - .NET

Страница: 1 |

 

  Вопрос: Excel b VB.NET Добавлено: 15.02.10 14:50  

Автор вопроса:  Michael Melchakov | ICQ: 302272185 
Подскажите, пожалуйста, как открыть книгу excel, и занести из нее данные в массив?

И если уж будет не сложно, то как из массива записать в данную книгу экселя?

P.S. как по одной ячейке делать - знаю.. но мне надо большое количество точек обрабатывать (порядка 5000)...

Ответить

  Ответы Всего ответов: 1  

Номер ответа: 1
Автор ответа:
 Fatty



Вопросов: 0
Ответов: 55
 Профиль | | #1 Добавлено: 17.02.10 09:56
Попробуй небольшой пример

'' Working example
Imports System.Runtime.InteropServices
Imports System.Globalization
Imports System.Threading
Imports Microsoft.Office.Interop.Excel
Imports Excel = Microsoft.Office.Interop.Excel
'' References -> COM -> Microsoft.Excel XX.0 Object Library
'' Create form and drop Button1 and ListView1 on it

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '' This line is very important!
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US";) '<-- change culture on whatever you need
        Dim miss As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer
        '' Open Excel
        Dim m_objExcel As Excel.Application = New Excel.Application
        m_objExcel.Visible = True
        Dim m_objBooks As Workbooks = m_objExcel.Workbooks
        Try
            '' Open workbook
            m_objBooks.Open("C:\Points.xls", False, False, miss, "", False, miss, Excel.XlPlatform.xlWindows, miss, True, miss, miss, miss, False)

            Dim m_objBook As Workbook = m_objBooks.Item(1)
            m_objBook.Activate() 'optional
            Dim m_objSheets As Sheets = m_objBook.Sheets
            Dim m_objSheet As Worksheet = m_objSheets.Item(2) '<--"Sheet2"
            m_objSheet.Cells.ClearContents() 'optional
            Dim m_objCells As Range = m_objSheet.Cells

            '' write data starting from cell "A1"
            Dim m_objRange As Range = Nothing
            For i = 1 To 101
                For j = 1 To 3
                    m_objRange = m_objSheet.Range(m_objCells(i, j), m_objCells(i, j))
                    m_objRange.Value = Rnd(5).ToString
                Next
            Next

            '' fill the listview with data
            ListView1.Clear()
            Dim columns() As String = New String() {"X coordinate", "Y coordinate", "Z coordinate"}

            For Each column As String In columns
                ListView1.Columns.Add(column, 96, HorizontalAlignment.Left)
            Next

            ListView1.View = View.Details
            ListView1.GridLines = True
            ListView1.FullRowSelect = True

            For row As Integer = 1 To i - 1

                Dim lvi As New ListViewItem(m_objSheet.Range(m_objCells(row, 1), m_objCells(row, 1)).Value.ToString, 0)
                For col As Integer = 2 To j - 1
                    lvi.SubItems.Add(m_objSheet.Range(m_objCells(row, col), m_objCells(row, col)).Value.ToString)
                Next
                Me.ListView1.Items.Add(lvi)
            Next

            '' Save the file in the typical workbook format
            m_objBook.SaveAs("C:\Points.xls", Excel.XlFileFormat.xlWorkbookNormal, "", "", False, False, XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss)
            '' close workbook and quit Excel
            m_objBook.Close(False, miss, miss)
            m_objExcel.Quit()
        Catch ex As System.Exception
            MessageBox.Show(ex.StackTrace)
        Finally
            '' release process if it's still active
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objExcel)
        End Try
    End Sub

End Class



~'J'~

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам