Here is a good example that if you have issues working thru my project. What the follow code will do is open the file, modify it, pause via MessageBox which at this time go open the file in Explorer and examine the values, close the file, back in the app press OK, next time the dialog appears press OK. Open the file in explorer and note one value is different (well actually everything is different as per the Dictionary used). Nothing will pop up indicating the file is read-only and if it does there is something else going on as I fully tested this before suggestion it.
Place a new button on the main for with the following code for the click event, change the following line in regards to path and file name OpenExcelWriteData2("C:\Dotnet2010\MSDN\Excel\Basics_1\bin\Debug\KSG1.xlsx", "Sheet1", D)
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyDicts As New List(Of Dictionary(Of String, String)) From
{
New Dictionary(Of String, String) From
{
{"A1", "Month"},
{"A2", "January"},
{"A3", "February"},
{"A4", "March"},
{"A5", "April"},
{"B1", "Money Spent"},
{"B2", "1000.00"},
{"B3", "1500.00"},
{"B4", "1200.00"},
{"B5", "1100.00"}
},
New Dictionary(Of String, String) From
{
{"A1", "Month"},
{"A2", "January"},
{"A3", "February"},
{"A4", "March"},
{"A5", "April"},
{"B1", "Money Spent"},
{"B2", "1000.00"},
{"B3", "1500.00"},
{"B4", "1200.00"},
{"B5", "555.00"}
}
}
For Each D In MyDicts
OpenExcelWriteData2("C:\Dotnet2010\MSDN\Excel\Basics_1\bin\Debug\KSG1.xlsx", "Sheet1", D)
MessageBox.Show("Do it")
Next
End Sub
Add the following code to OpenWorkSheets.vb
Code:
Public Sub OpenExcelWriteData2(ByVal FileName As String, ByVal SheetName As String, ByVal DictCellData As Dictionary(Of String, String))
Dim Proceed As Boolean = False
Dim xlApp As Excel.Application = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing
Dim xlWorkSheets As Excel.Sheets = Nothing
Dim xlRange1 As Excel.Range = Nothing
Dim xlInterior As Excel.Interior = Nothing
Dim xlColumns As Excel.Range = Nothing
xlApp = New Excel.Application
xlApp.DisplayAlerts = False
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(FileName)
xlApp.Visible = False
xlWorkSheets = xlWorkBook.Sheets
For x As Integer = 1 To xlWorkSheets.Count
xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
If xlWorkSheet.Name = SheetName Then
Proceed = True
Exit For
End If
Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing
Next
If Proceed Then
' Write cell, dispose object, repeat...
For Each Item In DictCellData
xlRange1 = xlWorkSheet.Range(Item.Key)
xlRange1.Value = Item.Value
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
Next
xlRange1 = xlWorkSheet.Range("A6")
xlRange1.Value = "Total"
Try
'
' Delete comment if exists
'
If xlRange1.Comment IsNot Nothing Then
xlRange1.Comment.Delete()
End If
xlRange1.AddComment("Total")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
xlRange1 = xlWorkSheet.Range("A7")
xlRange1.Value = "Average Expense"
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
xlRange1 = xlWorkSheet.Range("B6")
xlRange1.Formula = "=Sum(B2:B5)"
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
xlRange1 = xlWorkSheet.Range("B7")
xlRange1.Formula = "=Average(B2:B5)"
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
xlRange1 = xlWorkSheet.Range("A1:B1,A6:A7")
xlInterior = xlRange1.Interior
xlInterior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black)
Dim TheFont = xlRange1.Font
TheFont.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White)
TheFont.Name = "Tahoma"
TheFont.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleSingle
TheFont.Bold = True
Marshal.FinalReleaseComObject(TheFont)
TheFont = Nothing
xlRange1 = xlWorkSheet.Range("B2:B7")
xlRange1.NumberFormat = "$#,##0.00"
' ******************************************************************
' An example of moving past tunneling and only calling the GC
' for one object. This can be avoided but wanted to show one
' example of calling the GC surrounded by other objects that
' need not call the GC.
' ******************************************************************
xlColumns = CType(xlRange1.Columns("A:B"), Excel.Range)
xlColumns.EntireColumn.AutoFit()
releaseObject(xlColumns, True)
Marshal.FinalReleaseComObject(xlRange1)
xlRange1 = Nothing
xlWorkSheet.SaveAs(FileName)
Else
' IMPORTANT NOTE
' For production throw an exception, for demoing a message
' This demo the only way the sheet does not exists if someone
' really tried to mess with this code outside with MS-Excel.
'
MessageBox.Show(SheetName & " not located.")
End If
xlWorkBook.Close()
xlApp.UserControl = True
xlApp.Quit()
ReleaseComObject(xlInterior)
ReleaseComObject(xlRange1)
ReleaseComObject(xlWorkSheets)
ReleaseComObject(xlWorkSheet)
ReleaseComObject(xlWorkBook)
ReleaseComObject(xlWorkBooks)
ReleaseComObject(xlApp)
End Sub