Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Trouble changing default StreamWriter filename

$
0
0

I've written the program below and it works fine, but now I would like to change the default built-in filename and nothing I do seems to work.  The line I would like to change, or think needs to change is:

Dim ExcelFileName As String = Path,Combine(Request,PhysicalApplicationPath, "a.xls")

I've tried everything I can think of including Dim ExcelFileName As String = "C:\Excel_Database.xls" but every attempt results in no output being generated.  The program appears to run fine, just doesn't output anything; no error messages.  The PhysicalApplicationPath is not where I would like production work to go!  Ideally, I would like for the individual user to select his/her own path and filename.  Any suggestions? Thanks!

        Try
            Dim rows As Integer = ds1.Tables(0).Rows.Count + 1
            Dim cols As Integer = ds1.Tables(0).Columns.Count
            Dim ExcelFileName As String = Path.Combine(Request.PhysicalApplicationPath, "a.xls")
            If File.Exists(ExcelFileName) Then
                File.Delete(ExcelFileName)
            End If
            Dim writer As New StreamWriter(ExcelFileName, False)
            writer.WriteLine("<?xml version=""1.0""?>")
            writer.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
            writer.WriteLine("<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""")
            writer.WriteLine(" xmlns:o=""urn:schemas-microsoft-com:office:office""")
            writer.WriteLine(" xmlns:x=""urn:schemas-microsoft-com:office:excel""")
            writer.WriteLine(" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""")
            writer.WriteLine(" xmlns:html=""http://www.w3.org/TR/REC-html40/"">")
            writer.WriteLine(" <DocumentProperties xmlns=""urn:schemas-microsoft-com:office:office"">;")
            writer.WriteLine("  <Author>Automated Report Generator Example</Author>")
            writer.WriteLine(String.Format("  <Created>{0}T{1}Z</Created>", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")))
            writer.WriteLine("  <Company>51aspx.com</Company>")
            writer.WriteLine("  <Version>11.6408</Version>")
            writer.WriteLine(" </DocumentProperties>")
            writer.WriteLine(" <ExcelWorkbook xmlns=""urn:schemas-microsoft-com:office:excel"">")
            writer.WriteLine("  <WindowHeight>8955</WindowHeight>")
            writer.WriteLine("  <WindowWidth>11355</WindowWidth>")
            writer.WriteLine("  <WindowTopX>480</WindowTopX>")
            writer.WriteLine("  <WindowTopY>15</WindowTopY>")
            writer.WriteLine("  <ProtectStructure>False</ProtectStructure>")
            writer.WriteLine("  <ProtectWindows>False</ProtectWindows>")
            writer.WriteLine(" </ExcelWorkbook>")
            writer.WriteLine(" <Styles>")
            writer.WriteLine("  <Style ss:ID=""Default"" ss:Name=""Normal"">")
            writer.WriteLine("   <Alignment ss:Vertical=""Bottom""/>")
            writer.WriteLine("   <Borders/>")
            writer.WriteLine("   <Font/>")
            writer.WriteLine("   <Interior/>")
            writer.WriteLine("   <Protection/>")
            writer.WriteLine("  </Style>")
            writer.WriteLine("  <Style ss:ID=""s21"">")
            writer.WriteLine("   <Alignment ss:Vertical=""Bottom"" ss:WrapText=""0""/>")
            writer.WriteLine("  </Style>")
            writer.WriteLine(" </Styles>")
            'Capacitors (1)
            writer.WriteLine(" <Worksheet ss:Name=""Capacitors"">")
            writer.WriteLine(String.Format("  <Table ss:ExpandedColumnCount=""{0}"" ss:ExpandedRowCount=""{1}"" x:FullColumns=""1""", cols.ToString(), rows.ToString()))
            writer.WriteLine("   x:FullRows=""1"">")
            'generate title
            writer.WriteLine("<Row>")
            For Each eachCloumn As DataColumn In ds1.Tables(0).Columns
                writer.Write("<Cell ss:StyleID=""s21""><Data ss:Type=""String"">")
                writer.Write(eachCloumn.ColumnName.ToString())
                writer.WriteLine("</Data></Cell>")
            Next
            writer.WriteLine("</Row>")
            'generate data
            For Each eachRow As DataRow In ds1.Tables(0).Rows
                writer.WriteLine("<Row>")
                Dim currentRow As Integer = 0
                While currentRow <> cols
                    writer.Write("<Cell ss:StyleID=""s21""><Data ss:Type=""String"">")
                    writer.Write(eachRow(currentRow).ToString())
                    writer.WriteLine("</Data></Cell>")
                    currentRow += 1
                End While
                writer.WriteLine("</Row>")
            Next
            writer.WriteLine("  </Table>")
            writer.WriteLine("  <WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel"">")
            writer.WriteLine("   <Selected/>")
            writer.WriteLine("   <Panes>")
            writer.WriteLine("    <Pane>")
            writer.WriteLine("     <Number>3</Number>")
            writer.WriteLine("     <ActiveRow>1</ActiveRow>")
            writer.WriteLine("    </Pane>")
            writer.WriteLine("   </Panes>")
            writer.WriteLine("   <ProtectObjects>False</ProtectObjects>")
            writer.WriteLine("   <ProtectScenarios>False</ProtectScenarios>")
            writer.WriteLine("  </WorksheetOptions>")
            writer.WriteLine(" </Worksheet>")
            'Finish
            writer.WriteLine("</Workbook>")
            writer.Close()
            Response.Write("<script language=""javascript"">" & "alert('" & "convert completed!')" & "</script>")
        Catch ex As Exception
            Response.Write(("<script language=""javascript"">" & "alert('" & "error! ") + ex.Message & "')" & "</script>")
        End Try


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>