0

I would like to setup a webpage that users can paste work orders into a textbox, click submit, and it writes it to .xlsx on a server.

I've gotten StreamWriter to save it to a .txt, but doesn't work/isn't compatible with .xlsx.

Could someone point me in the right direction? I'm not looking for someone to do it for me, but I am new to coding, so I've got tons of stupid questions.

lolok
  • 1
  • 1
  • This is a little hacky, but you can write the file, via streamwriter, as tab delimited and save it with the `.xls` extension. Excel will complain a bit when you try to open it, but you just answer "yes" when it does and it will open just fine. That would require very little effort to change your existing code. If you want something more proper then you'll have to create it using an excel library all proper like [kind of like this](https://stackoverflow.com/questions/16536372/how-to-save-excel-file-in-vb-net) – JNevill Apr 02 '18 at 18:51
  • i recommend grid too, if look/feel isn't important - i'd go the CSV route. That way you have no dependencies - see example post - https://stackoverflow.com/questions/9943787/exporting-datagridview-to-csv-file – Ctznkane525 Apr 02 '18 at 23:06

1 Answers1

0

If you had the users input the work orders into a datagrid instead of a textbox, your export to excel would be easier and look better.

    Function EXPTOEXLS()

    Dim rowsTotal, colsTotal As Short
    Dim I, j, iC As Short
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
    Dim xlApp As New Excel.Application 'need to add reference to "Microsoft Excel xx object library" then add  |  IMPORTS microsoft.office.interop  (add to top)

    Try
        Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
        Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
        xlApp.Visible = True
        rowsTotal = DataGridView1.RowCount - 1
        colsTotal = DataGridView1.Columns.Count - 1

        With excelWorksheet
            .Cells.Select()
            .Cells.Delete()
            For iC = 0 To colsTotal
                .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
            Next

            For I = 0 To rowsTotal
                For j = 0 To colsTotal
                    .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
                Next j
            Next I

            .Rows("1:1").Font.FontStyle = "Bold"
            .Rows("1:1").Font.Size = 10
            .Cells.Columns.AutoFit()
            .Cells.Select()
            .Cells.EntireColumn.AutoFit()
            .Cells(1, 1).Select()
        End With

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        'RELEASE ALLOACTED RESOURCES
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
        xlApp = Nothing
    End Try
End Function