自己写的Excel

来源:互联网 发布:淘宝1元门 编辑:程序博客网 时间:2024/05/06 05:56
  1. #Region "エクセルを出力" 
  2.     Private beforeTime As DateTime
  3.     Private afterTime As DateTime
  4.     ''' <summary> 
  5.     '''新規エクセル  
  6.     ''' </summary> 
  7.     ''' <param name="Application">エクセルのアプリケーション</param> 
  8.     ''' <param name="newWorkbook">エクセルのワークブック</param> 
  9.     ''' <param name="newWorksheet">エクセルのシート</param> 
  10.     ''' <remarks></remarks> 
  11.     Private Function newExcel(ByVal Application As Excel.Application, ByVal newWorkbook As Excel.Workbook, _
  12.                         ByVal newWorksheet As Excel.Worksheet) As Hashtable
  13.         Dim excelHashTbale As New Hashtable
  14.         Application = New Excel.Application
  15.         '新規ワークブック 
  16.         newWorkbook = Application.Workbooks.Add()
  17.         Application.Visible = False
  18.         Application.DisplayAlerts = False
  19.         '新規シート 
  20.         'newWorksheet = newWorkbook.Worksheets.Add(DBNull.Value, DBNull.Value, 3) 
  21.         'newWorksheet = CType(newWorkbook.Worksheets.Add(), Excel.Worksheet) 
  22.         newWorksheet = newWorkbook.Worksheets.Item("sheet1")
  23.         excelHashTbale.Add("App", Application)
  24.         excelHashTbale.Add("workbook", newWorkbook)
  25.         excelHashTbale.Add("worksheet", newWorksheet)
  26.         Return excelHashTbale
  27.     End Function
  28.     ''' <summary> 
  29.     ''' 保存エクセル 
  30.     ''' </summary> 
  31.     ''' <param name="newWorkbook">エクセルのワークブック</param> 
  32.     ''' <param name="strPath">エクセル保存のパス</param> 
  33.     ''' <param name="strExcelName">エクセルの名称</param> 
  34.     ''' <remarks></remarks> 
  35.     Private Sub saveExcel(ByVal newWorkbook As Excel.Workbook, ByVal strPath As String, _
  36.                             ByVal strExcelName As String)
  37.         newWorkbook.SaveAs(strPath & strExcelName)
  38.     End Sub
  39.     ''' <summary> 
  40.     ''' エクセルを閉じる 
  41.     ''' </summary> 
  42.     ''' <param name="newWorkbook">エクセルのワークブック</param> 
  43.     ''' <param name="Application">エクセルのアプリケーション</param> 
  44.     ''' <remarks></remarks> 
  45.     Private Sub closeExcel(ByVal newWorkbook As Excel.Workbook, ByVal Application As Excel.Application)
  46.         Dim processlist() As Process = Process.GetProcessesByName("EXCEL")
  47.         Dim startTime As Date
  48.         If Not IsNothing(newWorkbook) Then newWorkbook.Close()
  49.         'プロセスの終了 
  50.         If Not IsNothing(Application) Then
  51.             Application.Quit()
  52.             System.Runtime.InteropServices.Marshal.ReleaseComObject(Application)
  53.             Application = Nothing
  54.         End If
  55.         For Each kp As Process In processlist
  56.             startTime = kp.StartTime
  57.             If startTime > beforeTime AndAlso startTime < afterTime Then
  58.                 kp.Kill()
  59.             End If
  60.         Next
  61.         GC.Collect()
  62.     End Sub
  63.     ''' <summary> 
  64.     ''' エクセルを操作 
  65.     ''' </summary> 
  66.     ''' <param name="newWorkbook">エクセルのワークブック</param> 
  67.     ''' <param name="newWorksheet">エクセルのシート</param> 
  68.     ''' <remarks></remarks> 
  69.     Private Sub operationExcel(ByVal Application As Excel.Application, ByVal newWorkbook As Excel.Workbook, _
  70.                                 ByVal newWorksheet As Excel.Worksheet)
  71.         Dim strDefaultPath As String
  72.         Dim intAppReFile As Integer = Application.RecentFiles.Count
  73.         Dim strFileName(intAppReFile) As String
  74.         Dim i As Integer
  75.         newWorksheet.Cells(1, 1) = "Hello Word!"
  76.         'ワークブックを電子メールの添付ファイルにして発送します。 
  77.         newWorkbook.SendMail(Recipients:="someone@example.com", Subject:="July Sales Figures")
  78.         '黙認のパス 
  79.         strDefaultPath = Application.DefaultFilePath
  80.         '最近使うワークブックを並べます 
  81.         For i = 0 To intAppReFile - 1
  82.             strFileName(i) = Application.RecentFiles(i + 1).Name
  83.         Next
  84.         
  85.     End Sub
  86.     Private Sub Button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button2.Click
  87.         Dim Application As Excel.Application = Nothing
  88.         Dim newWorkbook As Excel.Workbook = Nothing
  89.         Dim newWorksheet As Excel.Worksheet = Nothing
  90.         Dim excelHashTable As New Hashtable
  91.         Try
  92.             beforeTime = Date.Now
  93.             '新規 
  94.             excelHashTable = newExcel(Application, newWorkbook, newWorksheet)
  95.             afterTime = Date.Now
  96.             'エクセルを操作 
  97.             operationExcel(excelHashTable("App"), excelHashTable("workbook"), excelHashTable("worksheet"))
  98.             '保存エクセル 
  99.             saveExcel(excelHashTable("workbook"), "D:/", "New")
  100.             MsgBox("出力成功!")
  101.         Catch ex As Exception
  102.             MsgBox(ex.Message)
  103.         Finally
  104.             'エクセルを閉じる 
  105.             closeExcel(excelHashTable("workbook"), excelHashTable("App"))
  106.         End Try
  107.     End Sub
  108. #End Region 
  109. #Region "エクセルを読取" 
  110.     Private Sub Button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button3.Click
  111.         Dim aExcel As Excel.Application = Nothing
  112.         Dim newWorkBook As Excel.Workbook = Nothing
  113.         Dim strPath As String = "D:/"
  114.         Dim strFileName As String = "New"
  115.         Try
  116.             beforeTime = Date.Now
  117.             readExcel(aExcel, newWorkBook, strPath, strFileName)
  118.             afterTime = Date.Now
  119.             MsgBox("読取成功!")
  120.         Catch ex As Exception
  121.             MsgBox(ex.Message)
  122.         Finally
  123.             closeExcel(newWorkBook, aExcel)
  124.         End Try
  125.     End Sub
  126.     ''' <summary> 
  127.     ''' エクセルを読取 
  128.     ''' </summary> 
  129.     ''' <param name="aExcel">エクセルのアプリケーション</param> 
  130.     ''' <param name="newWorkBook">エクセルのワークブック</param> 
  131.     ''' <param name="strPath">エクセル保存のパス</param> 
  132.     ''' <param name="strFileName">エクセルの名称</param> 
  133.     ''' <remarks></remarks> 
  134.     Private Sub readExcel(ByVal aExcel As Excel.Application, ByVal newWorkBook As Excel.Workbook, _
  135.                             ByVal strPath As StringByVal strFileName As String)
  136.         Dim newWorkSheet As Excel.Worksheet
  137.         aExcel = New Excel.Application
  138.         newWorkBook = aExcel.Workbooks.Open(strPath & strFileName)
  139.         newWorkSheet = newWorkBook.Sheets("sheet1")
  140.         Dim s As String = newWorkSheet.Cells(1, 1).value2
  141.         MsgBox(s)
  142.     End Sub
  143. #End Region
  144. #Region "XML"
  145.     Dim xmlWriter As New Xml.XmlTextWriter("New", System.Text.Encoding.UTF8)
  146.     Private Sub writeXML()
  147.         Dim xml As New Xml.XmlDocument
  148.         xmlWriter.Formatting = System.Xml.Formatting.Indented
  149.         xmlWriter.Indentation = 2
  150.         xmlWriter.QuoteChar = """"c
  151.         xmlWriter.WriteStartDocument(True)
  152.         xmlWriter.WriteStartElement("localName")
  153.     End Sub
  154.     Public Sub SetXmlElement(ByVal localName As StringByVal value As String)
  155.         xmlWriter.WriteElementString(localName, value)
  156.     End Sub
  157.     Public Sub SetXmlStartElement(ByVal localName As String)
  158.         xmlWriter.WriteStartElement(localName)
  159.     End Sub
  160.     Public Sub SetXmlAttributeString(ByVal localName As StringByVal value As String)
  161.         xmlWriter.WriteAttributeString(localName, value)
  162.     End Sub
  163.     Public Sub WriteEndElement()
  164.         xmlWriter.WriteEndElement()
  165.     End Sub
  166.     Public Sub WriteEndDocument()
  167.         xmlWriter.WriteEndDocument()
  168.     End Sub
  169.     Public Sub xmlClose()
  170.         xmlWriter.Close()
  171.     End Sub
  172. #End Region