Home » excel » vb.net – Excel is saving in C:\\MyDocuments Defaultly

vb.net – Excel is saving in C:\\MyDocuments Defaultly

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am exporting data to excel and try to save to a folder which is in my application…but the excel is saving defaultly in C:\Documents but i wanted to save in E:\Apllication\Reports

Here is my code to generate excel sheet

    If ComDset.Tables(0).Rows.Count > 0 Then

        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()

                Dim i As Integer = 1
                For col = 0 To ComDset.Tables(0).Columns.Count - 1
                    .cells(1, i).value = ComDset.Tables(0).Columns(col).ColumnName
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To ComDset.Tables(0).Columns.Count - 1
                    i = 2
                    For row = 0 To ComDset.Tables(0).Rows.Count - 1
                        .Cells(i, k).Value = ComDset.Tables(0).Rows(row).ItemArray(col)
                        i += 1
                    Next
                    k += 1
                Next
                filename = "ShiftReport" & Format(MdbDate, "dd-MM-yyyy") & ".xls"
                .ActiveCell.Worksheet.SaveAs(filename)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End If

and tell me how to assign colour to the header

Thanks in advance

How to&Answers:

Have you tried this?

.ActiveCell.Worksheet.SaveAs("E:\Apllication\Reports\" & filename)