Advertisement
SythsGod

Excel Form File

May 22nd, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.45 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. ' made by Andro (Robin en Dries)
  4. ' Version 2.3
  5.  
  6. Imports Excel = Microsoft.Office.Interop.Excel 'Size 454; 121
  7.  
  8. Public Class Form1
  9.     Private ds As New DataSet("AllData")
  10.  
  11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.         ds = DatabaseRepository.RetrieveAll
  13.  
  14.         'txtFile.Text = Path.GetFullPath( _
  15.         '    Path.Combine(Application.StartupPath, "..\..")) & _
  16.         '    "\Items.xlsx"
  17.     End Sub
  18.  
  19.     ' Write into the Excel workbook.
  20.     Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
  21.         ' Get the Excel application object.
  22.         Dim excel_app As New Excel.ApplicationClass()
  23.  
  24.         ' Make Excel visible (optional).
  25.         excel_app.Visible = True
  26.  
  27.         'File.Create("C:\Users\driesh\Documents\VbToExcel\items.xlsx")
  28.         ' Open the workbook.
  29.         Dim workbook As Excel.Workbook = _
  30.         excel_app.Workbooks.Open(Filename:="C:\Users\driesh\Documents\VbToExcel\items.xlsx")
  31.  
  32.         ' See if the worksheet already exists.
  33.         Dim sheet_name As String = DateTime.Now.ToString("MM-dd-yy")
  34.  
  35.         Dim sheet As Excel.Worksheet = FindSheet(workbook, sheet_name)
  36.         If (sheet Is Nothing) Then
  37.             ' Add the worksheet at the end.
  38.             sheet = DirectCast(workbook.Sheets.Add( _
  39.                 After:=workbook.Sheets(workbook.Sheets.Count), _
  40.                 Count:=1, _
  41.                 Type:=Excel.XlSheetType.xlWorksheet), Excel.Worksheet)
  42.             sheet.Name = sheet_name
  43.         End If
  44.  
  45.         ' Add some data to individual cells.
  46.         sheet.Cells(1, 1) = "Test 1"
  47.         sheet.Cells(1, 2) = "Test 2"
  48.         sheet.Cells(1, 3) = "Test 3"
  49.  
  50.         Dim jj As Integer = 0
  51.  
  52.         For i = 1 To ds.Tables.Count
  53.             If i = 17 Then Exit For
  54.             Dim table As DataTable = ds.Tables(i - 1)
  55.             For j = 0 To table.Rows.Count - 1
  56.                 For k = 0 To table.Columns.Count - 1
  57.                     sheet.Cells((j + 1) + jj, (k + 1)) = table.Rows(j)(k).ToString
  58.                 Next
  59.             Next
  60.             jj += table.Rows.Count + 1
  61.         Next
  62.  
  63.         ' Make that range of cells bold and red.
  64.         'Dim header_range As Excel.Range = sheet.Range("A1", "C1")
  65.         'header_range.Font.Bold = True
  66.         'header_range.Font.Color = _
  67.         '    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
  68.         'header_range.Interior.Color = _
  69.         '    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Pink)
  70.  
  71.         ' Add some data to a range of cells.
  72.         'Dim values(,) As Integer = _
  73.         '{ _
  74.         '    {2, 4, 6}, _
  75.         '    {3, 6, 9}, _
  76.         '    {4, 8, 12}, _
  77.         '    {5, 10, 15} _
  78.         '}
  79.         'Dim value_range As Excel.Range = sheet.Range("A2", "C5")
  80.         'value_range.Value2 = values
  81.  
  82.         ' Save the changes and close the workbook.
  83.         'workbook.Close(SaveChanges:=True)
  84.  
  85.         ' Close the Excel server.
  86.         'excel_app.Quit()
  87.  
  88.         MessageBox.Show("Done")
  89.     End Sub
  90.  
  91.     ' Return the worksheet with the given name.
  92.     Private Function FindSheet(ByVal workbook As Excel.Workbook, ByVal sheet_name As String) As Excel.Worksheet
  93.         For Each sheet As Excel.Worksheet In workbook.Sheets
  94.             If (sheet.Name = sheet_name) Then Return sheet
  95.         Next sheet
  96.  
  97.         Return Nothing
  98.     End Function
  99. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement